From eaf62b1a3fb81933ed7540b46e3744c800a83b30 Mon Sep 17 00:00:00 2001 From: Amanda Peterson Date: Sat, 30 Sep 2023 03:18:28 +0000 Subject: [PATCH 01/18] section: what are tensors --- 03_tensors.Rmd | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/03_tensors.Rmd b/03_tensors.Rmd index 3d16e0d..1232170 100644 --- a/03_tensors.Rmd +++ b/03_tensors.Rmd @@ -4,10 +4,24 @@ - THESE ARE NICE TO HAVE BUT NOT ABSOLUTELY NECESSARY -## SLIDE 1 {-} +## What are tensors? {-} + +```{r} +library(torch) +t1 <- torch_tensor(2) +summary(t1) +t1$dtype +t1$device +t1$shape +t2 <- t1$to(dtype = torch_int()) +t2$dtype +t2 <- t1$to(device = "cuda") +t2$device +t3 <- t1$view(c(1,1)) +t3$shape +``` -- ADD SLIDES AS SECTIONS (`##`). -- TRY TO KEEP THEM RELATIVELY SLIDE-LIKE; THESE ARE NOTES, NOT THE BOOK ITSELF. +## Creating tensors ## Meeting Videos {-} From c4661b3b1cab73ee0ffa86e54e4d4aa86794816a Mon Sep 17 00:00:00 2001 From: Amanda Peterson Date: Mon, 2 Oct 2023 16:33:09 +0000 Subject: [PATCH 02/18] Added material for pages 9-17 of book. --- 03_tensors.Rmd | 90 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/03_tensors.Rmd b/03_tensors.Rmd index 1232170..99ffc26 100644 --- a/03_tensors.Rmd +++ b/03_tensors.Rmd @@ -6,23 +6,109 @@ ## What are tensors? {-} +> "*tensors* are 'just' multi-dimensional arrays optimized for fast computation - +not on the CPU only but also on specialized devices such as GPUs and TPUs. + +Create a tensor: + ```{r} library(torch) t1 <- torch_tensor(2) + +t3 <- t1$view(c(1,1)) +t3$shape +``` + +Parameters of `torch_tensor()` (see help file for this function): + +* `data` +* `dtype` +* `device` +* `requires_grad` +* `pin_memory` + +Look at the attributes of the tensor: + +```{r} summary(t1) t1$dtype t1$device t1$shape +``` + +Change attributes: + +```{r} +# to integer: t2 <- t1$to(dtype = torch_int()) t2$dtype + +# utilize GPU: t2 <- t1$to(device = "cuda") t2$device -t3 <- t1$view(c(1,1)) -t3$shape ``` + + + ## Creating tensors +### Tensors from values + +Defaults: long integer type and CPU device. + +```{r} +torch_tensor(1:5, dtype = torch_float(), device = "cuda") +``` + +Matrix: + +```{r} +torch_tensor(matrix(1:9, ncol = 3, byrow = TRUE)) +``` + +Higher dimensions: + +```{r} +torch_tensor(array(1:24, dim = c(4,3,2))) +``` + +### Tensors from specifications + +```{r} +#normal distribution: +torch_randn(3, 3) + +#unfiorm distribution +torch_rand(3, 3) +``` + +```{r} +torch_zeros(2,2) +torch_ones(3,3) +torch_eye(3,3) +torch_diag(c(1,2,3)) +``` + +See full list at https://torch.mlverse.org/docs/reference/#tensor-creation-utilities. + +### Tensors from datasets + +```{r} +JohnsonJohnson +``` + +```{r} +library(modeldata) # Need to install 0.1.1 to obtain okc data. https://cran.r-project.org/src/contrib/Archive/modeldata/ +library(tidyverse) + +data(okc) +okc |> glimpse() +``` + +If your data contains `NA`s, you'll need to convert them before training a neural network +(topic to be covered later). + ## Meeting Videos {-} ### Cohort 1 {-} From feef41ec4b9807ab531cdf27e557c8bb316d6114 Mon Sep 17 00:00:00 2001 From: Amanda Peterson Date: Wed, 4 Oct 2023 13:17:14 +0000 Subject: [PATCH 03/18] Added additional content. --- 03_tensors.Rmd | 284 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 272 insertions(+), 12 deletions(-) diff --git a/03_tensors.Rmd b/03_tensors.Rmd index 99ffc26..f2317c4 100644 --- a/03_tensors.Rmd +++ b/03_tensors.Rmd @@ -2,18 +2,34 @@ **Learning objectives:** -- THESE ARE NICE TO HAVE BUT NOT ABSOLUTELY NECESSARY +Learn how to: + +- Create and modify tensors +- Access parts of tensors +- Apply operations to tensors ## What are tensors? {-} -> "*tensors* are 'just' multi-dimensional arrays optimized for fast computation - +From the book: + +> "... *tensors* are 'just' multi-dimensional arrays optimized for fast computation - not on the CPU only but also on specialized devices such as GPUs and TPUs. +Similar to an R array object. + +Load libraries: + +```{r message = FALSE, warning=FALSE} +library(tidyverse) +library(torch) +library(modeldata) # Need to install 0.1.1 to obtain okc data. https://cran.r-project.org/src/contrib/Archive/modeldata/ +``` + Create a tensor: ```{r} -library(torch) t1 <- torch_tensor(2) +t1$shape t3 <- t1$view(c(1,1)) t3$shape @@ -49,8 +65,6 @@ t2$device ``` - - ## Creating tensors ### Tensors from values @@ -95,20 +109,266 @@ See full list at https://torch.mlverse.org/docs/reference/#tensor-creation-utili ### Tensors from datasets ```{r} -JohnsonJohnson +data(okc) # from modeldata library +okc |> glimpse() ``` ```{r} -library(modeldata) # Need to install 0.1.1 to obtain okc data. https://cran.r-project.org/src/contrib/Archive/modeldata/ -library(tidyverse) - -data(okc) -okc |> glimpse() +okc |> + mutate(diet = as.numeric(factor(diet)), + location = as.numeric(factor(location)), + Class = as.numeric(Class), + date = as.numeric(date)) |> + as.matrix() |> + torch_tensor() |> + print(5) ``` -If your data contains `NA`s, you'll need to convert them before training a neural network +Tip: Above transformation lost information. For example, you may instead want to +use lat/long for location instead of converting to factor. + +If your data contains `NA`'s, you'll need to convert them before training a neural network (topic to be covered later). +## Operations on Tensors + +```{r} +t1 <- torch_tensor(c(1,2)) +t2 <- torch_tensor(c(3,4)) +``` + +Add: + +```{r} +torch_add(t1, t2) # t1 is NOT modified +t1$add(t2) # t1 is NOT modified +t1$add_(t2) # t1 IS modified +t1 +``` + +In general: + +* underscore appended to operation indicates modification in-place. +* torch does not distinguish between row and column vectors. +* Other examples of operations: `torch_t()`, `torch_dot()`, `torch_matmul()`, and `torch_multiply()`. +See https://torch.mlverse.org/docs/reference/#mathematical-operations-on-tensors. + +```{r} +t1 <- torch_tensor(1:3) +t2 <- torch_tensor(4:6) +``` + +```{r} +t1$dot(t2) +``` + +### Summary operations + +```{r} +m <- outer(1:3, 1:6) +t <- torch_outer(torch_tensor(1:3), torch_tensor(1:6)) +``` + +```{r} +apply(m, 1, sum) # row sums (of R matrix) +t$sum(dim = 2) # row sums (of tensor) +``` + +* In R, we *group* by row (dimension 1) for row summaries and by columns (dimension 2) for column summaries +* In `torch, we *collapse* the columns (dimension 2) for row summaries and the rows (dimension 1) for column summaries. + +Time series example: Two features collected three times for four individuals. + +* Dimension 1: Runs over individuals +* Dimension 2: Runs over points in time +* Dimension 3: Runs over features + +```{r} +t <- torch_randn(4, 3, 2) +t +``` + +Obtain averages of features, independent of subject (dimension 1) and time (dimension 2): + +```{r} +t$mean(dim = c(1, 2)) +``` + + +## Accessing Parts of a Tensor (indexing and slicing) + +Indexing in `torch` is 1-based. Very similar to functionality in R. + +Example: + +```{r} +t <- torch_tensor(matrix(1:9, ncol = 3, byrow = TRUE)) +t +t[1, ] # row 1. +t[1, , drop = FALSE] # Same as above except that dimensionality is preserved +``` + +Slicing example: + +```{r} +t <- torch_rand(3, 3, 3) +t[1:2, 2:3, c(1, 3)] +``` + +### Beyond R + +Access last element: + +```{r} +t <- torch_tensor(matrix(1:4, ncol = 2, byrow = TRUE)) +t[-1, -1] +``` + +Compare to R: + +```{r} +# matrix: +m <- matrix(1:4, ncol = 2, byrow = TRUE) +m[-1, -1] + +# vector: +m <- 1:4 +m[-1] +``` + +Step pattern: + +```{r} +t <- torch_tensor(matrix(1:20, ncol = 10, byrow = TRUE)) +t +t[ , 1:8:2] # every other value in columns 1 through 8 +``` + +Use `..` to designate all dimensions not explicitly referenced. + +```{r} +t2 <- torch_randn(2, 2, 2) +t2 +t2[2, ..] # 2nd element of the 1st dimension +``` + +## Reshaping Tensors + +Two methods: + +* `view`: Zero-copy reshape (by changing tensor metadata). Will fail if zero-copy reshape is not possible. +* `reshape`: Uses zero-copy reshape (via metadata) when possible. If not, then will make a copy. + +```{r} +t <- torch_tensor(matrix(1:15, nrow = 3, byrow = TRUE)) +t +t$stride() + +# Change shape of t using view(): +t2 <- t$view(c(5,3)) +t2 +t2$stride() +``` + +Check memory location: + +```{r} +t$storage()$data_ptr() +t2$storage()$data_ptr() # same location +``` + + +View vs reshape: + +When two operations that change the stride are done in sequence, this will likely fail. +Below is an example of transpose, `t()` followed by `view()`. + +```{r eval = FALSE} +t$t()$view(15) # error +``` + +However, `reshape()` makes a copy of the underlying data and does not fail. + +```{r} +t3 <- t$t()$reshape(15) # no error +t3 +``` + + +Now the memory locations of t3 and the original data are different: + +```{r} +t$storage()$data_ptr() +t3$storage()$data_ptr() # different location +``` + +Two additional functions that are zero-copy operations: + +* `squeeze()`: Removes singleton dimensions +* `unsqueeze()`: Adds a singleton dimension + +```{r} +t <- torch_rand(3) # one dimensional tensor of shape 3 +t +t2 <- t$unsqueeze(1) # two dimensional tensor of shape {1,3} +t2 +``` +Inspect location: + +```{r} +t$storage()$data_ptr() +t2$storage()$data_ptr() # same location +``` + + +## Broadcasting + +Example: Want to add two tensors of shape 3x7x1 and 1x5 + +``` +t1 shape: 3 7 1 +t2 shape: 1 5 +``` + +Broadcast (from right to left): + +``` +t1 shape: 3 7 5 +t2 shape: 7 5 +``` + +Next, virtual expansion: + +``` +t1 shape: 3 7 5 +t2 shape: 1 7 5 +``` + +And, broadcast again: + +``` +t1 shape: 3 7 5 +t2 shape: 3 7 5 +``` + +Let's see it in action: + +```{r} +t1 <- torch_ones(3, 7, 1) +t2 <- torch_zeros(1, 5) +torch_add(t1, t2) +``` + +The following will NOT work: + +```{r eval = FALSE} +t1 <- torch_ones(3, 7, 1) +t2 <- torch_zeros(6, 5) +torch_add(t1, t2) +``` + + + ## Meeting Videos {-} ### Cohort 1 {-} From f92d14923002e236e2ed56107221e0abf3f6cfa8 Mon Sep 17 00:00:00 2001 From: Amanda Peterson Date: Wed, 4 Oct 2023 14:58:13 +0000 Subject: [PATCH 04/18] cleanup. added torch_tensor() example and stride definition. --- 03_tensors.Rmd | 53 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/03_tensors.Rmd b/03_tensors.Rmd index f2317c4..5fae80f 100644 --- a/03_tensors.Rmd +++ b/03_tensors.Rmd @@ -15,24 +15,23 @@ From the book: > "... *tensors* are 'just' multi-dimensional arrays optimized for fast computation - not on the CPU only but also on specialized devices such as GPUs and TPUs. -Similar to an R array object. - Load libraries: +Note: Need to install `modeldata` 0.1.1 to obtain `okc` data. Download package from https://cran.r-project.org/src/contrib/Archive/modeldata/. + ```{r message = FALSE, warning=FALSE} -library(tidyverse) library(torch) -library(modeldata) # Need to install 0.1.1 to obtain okc data. https://cran.r-project.org/src/contrib/Archive/modeldata/ +library(tidyverse) +library(modeldata) ``` + + Create a tensor: ```{r} t1 <- torch_tensor(2) t1$shape - -t3 <- t1$view(c(1,1)) -t3$shape ``` Parameters of `torch_tensor()` (see help file for this function): @@ -46,10 +45,10 @@ Parameters of `torch_tensor()` (see help file for this function): Look at the attributes of the tensor: ```{r} -summary(t1) t1$dtype t1$device t1$shape +summary(t1) ``` Change attributes: @@ -69,7 +68,28 @@ t2$device ### Tensors from values -Defaults: long integer type and CPU device. +**Defaults**: long integer type and CPU device. + +Let's test this: + +```{r} +torch_tensor(1:2) +``` + + +```{r} +torch_tensor(c(1,2)) +``` + +My guess is that the difference is due to the data types of `1:2` and `c(1,2)`: + +```{r hold=TRUE} +class(c(1,2)) +class(1:2) +``` + + +Set the type and device: ```{r} torch_tensor(1:5, dtype = torch_float(), device = "cuda") @@ -224,6 +244,11 @@ t <- torch_tensor(matrix(1:4, ncol = 2, byrow = TRUE)) t[-1, -1] ``` +```{r} +t <- torch_tensor(1:4) +t[-1] +``` + Compare to R: ```{r} @@ -263,13 +288,19 @@ Two methods: t <- torch_tensor(matrix(1:15, nrow = 3, byrow = TRUE)) t t$stride() +``` + +`$stride()` tells us the jump necessary to go from one element to the next in a single dimension (e.g. the stride necessary to get from one row to the next). -# Change shape of t using view(): +Next, change shape of `t` using `view()`: + +```{r} t2 <- t$view(c(5,3)) t2 t2$stride() ``` + Check memory location: ```{r} @@ -278,7 +309,7 @@ t2$storage()$data_ptr() # same location ``` -View vs reshape: +`view()` vs `reshape()`: When two operations that change the stride are done in sequence, this will likely fail. Below is an example of transpose, `t()` followed by `view()`. From 18f33f0776873824775019f2505f8e8b0aba4ab7 Mon Sep 17 00:00:00 2001 From: Amanda Peterson Date: Wed, 4 Oct 2023 16:40:08 +0000 Subject: [PATCH 05/18] Removed cuda specific operations for push to GitHub. --- 03_tensors.Rmd | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/03_tensors.Rmd b/03_tensors.Rmd index 5fae80f..3917c72 100644 --- a/03_tensors.Rmd +++ b/03_tensors.Rmd @@ -59,8 +59,8 @@ t2 <- t1$to(dtype = torch_int()) t2$dtype # utilize GPU: -t2 <- t1$to(device = "cuda") -t2$device +#t2 <- t1$to(device = "cuda") +#t2$device ``` @@ -91,7 +91,7 @@ class(1:2) Set the type and device: -```{r} +```{r eval=FALSE} torch_tensor(1:5, dtype = torch_float(), device = "cuda") ``` From fa5bbe0f14c17ef16e69e2f29c8f5c4162423ec3 Mon Sep 17 00:00:00 2001 From: Amanda Peterson Date: Wed, 4 Oct 2023 17:34:26 +0000 Subject: [PATCH 06/18] adding torch --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7dd5b88..a51d3a6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,5 +9,6 @@ Depends: R (>= 3.1.0) Imports: bookdown, - rmarkdown + rmarkdown, + torch Encoding: UTF-8 From d36bbd5bbf1b42eeee76707e856fe1e682ab47ad Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Wed, 4 Oct 2023 13:28:20 -0500 Subject: [PATCH 07/18] Add tidyverse and modeldata to DESCRIPTION --- DESCRIPTION | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DESCRIPTION b/DESCRIPTION index a51d3a6..eebe0fb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,6 +9,8 @@ Depends: R (>= 3.1.0) Imports: bookdown, + modeldata, rmarkdown, + tidyverse, torch Encoding: UTF-8 From 8ca99c43dc783c7bfba237aa9e54e8173fadd234 Mon Sep 17 00:00:00 2001 From: Amanda Peterson Date: Wed, 4 Oct 2023 21:23:57 +0000 Subject: [PATCH 08/18] Changed okc data to presidential data. Removed modeldata package. --- 03_tensors.Rmd | 52 ++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/03_tensors.Rmd b/03_tensors.Rmd index 3917c72..e1b34be 100644 --- a/03_tensors.Rmd +++ b/03_tensors.Rmd @@ -17,16 +17,11 @@ not on the CPU only but also on specialized devices such as GPUs and TPUs. Load libraries: -Note: Need to install `modeldata` 0.1.1 to obtain `okc` data. Download package from https://cran.r-project.org/src/contrib/Archive/modeldata/. - ```{r message = FALSE, warning=FALSE} library(torch) library(tidyverse) -library(modeldata) ``` - - Create a tensor: ```{r} @@ -57,10 +52,12 @@ Change attributes: # to integer: t2 <- t1$to(dtype = torch_int()) t2$dtype +``` +```{r eval=FALSE} # utilize GPU: -#t2 <- t1$to(device = "cuda") -#t2$device +t2 <- t1$to(device = "cuda") +t2$device ``` @@ -68,34 +65,34 @@ t2$dtype ### Tensors from values -**Defaults**: long integer type and CPU device. +**Defaults**: long integer type and CPU device Let's test this: ```{r} torch_tensor(1:2) ``` - +In the example above, the type is long integer. ```{r} torch_tensor(c(1,2)) ``` -My guess is that the difference is due to the data types of `1:2` and `c(1,2)`: +In this example, the type is float instead of a long integer. +My guess is that the difference is due to the difference in data types of `1:2` and `c(1,2)`: -```{r hold=TRUE} +```{r} class(c(1,2)) class(1:2) ``` +Explicitly set the type and device: -Set the type and device: - -```{r eval=FALSE} +```{r eval = FALSE} torch_tensor(1:5, dtype = torch_float(), device = "cuda") ``` -Matrix: +Two-dimensional tensor: ```{r} torch_tensor(matrix(1:9, ncol = 3, byrow = TRUE)) @@ -128,30 +125,31 @@ See full list at https://torch.mlverse.org/docs/reference/#tensor-creation-utili ### Tensors from datasets +We'll look at the `presidential` dataset from the `ggplot2` package: + ```{r} -data(okc) # from modeldata library -okc |> glimpse() +data(presidential) # from ggplot2 +presidential |> glimpse() ``` ```{r} -okc |> - mutate(diet = as.numeric(factor(diet)), - location = as.numeric(factor(location)), - Class = as.numeric(Class), - date = as.numeric(date)) |> +presidential |> + mutate(name = as.numeric(factor(name)), + start = as.numeric(start), + end = as.numeric(end), + party = as.numeric(factor(party))) |> as.matrix() |> torch_tensor() |> print(5) ``` -Tip: Above transformation lost information. For example, you may instead want to -use lat/long for location instead of converting to factor. - If your data contains `NA`'s, you'll need to convert them before training a neural network (topic to be covered later). ## Operations on Tensors +Define two tensors: + ```{r} t1 <- torch_tensor(c(1,2)) t2 <- torch_tensor(c(3,4)) @@ -173,6 +171,8 @@ In general: * Other examples of operations: `torch_t()`, `torch_dot()`, `torch_matmul()`, and `torch_multiply()`. See https://torch.mlverse.org/docs/reference/#mathematical-operations-on-tensors. +Another example: + ```{r} t1 <- torch_tensor(1:3) t2 <- torch_tensor(4:6) @@ -184,6 +184,8 @@ t1$dot(t2) ### Summary operations +Create a matrix and a tensor using outer products: + ```{r} m <- outer(1:3, 1:6) t <- torch_outer(torch_tensor(1:3), torch_tensor(1:6)) From 9736b8a7f86c3379a84a07328b3ae36cc6ac48f8 Mon Sep 17 00:00:00 2001 From: Amanda Peterson Date: Wed, 4 Oct 2023 21:27:20 +0000 Subject: [PATCH 09/18] added ggplot2 to DESCRIPTION file (needed for dataset) --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index a51d3a6..7e3a12e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,6 +9,7 @@ Depends: R (>= 3.1.0) Imports: bookdown, + ggplot2, rmarkdown, torch Encoding: UTF-8 From 7754857b811dfcae53d8dcbd9b88df1179f9d7c9 Mon Sep 17 00:00:00 2001 From: Amanda Peterson Date: Wed, 4 Oct 2023 21:29:03 +0000 Subject: [PATCH 10/18] Added dplyr --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 7e3a12e..f9dc574 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,6 +9,7 @@ Depends: R (>= 3.1.0) Imports: bookdown, + dplyr, ggplot2, rmarkdown, torch From c90b062eb8e76d9e16959430621f4e3e0a72e1a4 Mon Sep 17 00:00:00 2001 From: Amanda Peterson Date: Wed, 4 Oct 2023 21:32:46 +0000 Subject: [PATCH 11/18] Removed tidyverse dependency. Added dplyr and ggplot2 (specific dependencies). --- 03_tensors.Rmd | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/03_tensors.Rmd b/03_tensors.Rmd index e1b34be..0cfcf64 100644 --- a/03_tensors.Rmd +++ b/03_tensors.Rmd @@ -19,7 +19,8 @@ Load libraries: ```{r message = FALSE, warning=FALSE} library(torch) -library(tidyverse) +library(dplyr) +library(ggplot2) ``` Create a tensor: From 10a1cb057f754f6a6e8d7b900d8ffa6ff54a30f8 Mon Sep 17 00:00:00 2001 From: Amanda Peterson Date: Wed, 4 Oct 2023 21:34:04 +0000 Subject: [PATCH 12/18] resolve merge conflicts. update package dependencies. --- DESCRIPTION | 5 ----- 1 file changed, 5 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e72fa8b..f9dc574 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,13 +9,8 @@ Depends: R (>= 3.1.0) Imports: bookdown, -<<<<<<< HEAD dplyr, ggplot2, -======= - modeldata, ->>>>>>> d36bbd5bbf1b42eeee76707e856fe1e682ab47ad rmarkdown, - tidyverse, torch Encoding: UTF-8 From f4c606e9f6cbb47013e0246827d3187be8faa3ba Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Fri, 13 Oct 2023 09:57:12 -0500 Subject: [PATCH 13/18] Try a potentially easy fix for installing torch. I'm not confident this will work but we might as well try! --- .github/workflows/deploy_bookdown.yml | 2 ++ .github/workflows/pr_check.yml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.github/workflows/deploy_bookdown.yml b/.github/workflows/deploy_bookdown.yml index eaa103f..b6bdd93 100644 --- a/.github/workflows/deploy_bookdown.yml +++ b/.github/workflows/deploy_bookdown.yml @@ -7,4 +7,6 @@ on: jobs: bookdown: + env: + TORCH_INSTALL: 1 uses: r4ds/r4dsactions/.github/workflows/render_pages.yml@main diff --git a/.github/workflows/pr_check.yml b/.github/workflows/pr_check.yml index b7b9756..6e3e3a8 100644 --- a/.github/workflows/pr_check.yml +++ b/.github/workflows/pr_check.yml @@ -7,4 +7,6 @@ on: jobs: pr_check: + env: + TORCH_INSTALL: 1 uses: r4ds/r4dsactions/.github/workflows/render_check.yml@main From b9307c54d34b474df8356c037861b62c46de8aa9 Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Fri, 13 Oct 2023 09:59:36 -0500 Subject: [PATCH 14/18] Use before env. One more try at a quick fix before reading documentation. --- .github/workflows/deploy_bookdown.yml | 2 +- .github/workflows/pr_check.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy_bookdown.yml b/.github/workflows/deploy_bookdown.yml index b6bdd93..07f3625 100644 --- a/.github/workflows/deploy_bookdown.yml +++ b/.github/workflows/deploy_bookdown.yml @@ -7,6 +7,6 @@ on: jobs: bookdown: + uses: r4ds/r4dsactions/.github/workflows/render_pages.yml@main env: TORCH_INSTALL: 1 - uses: r4ds/r4dsactions/.github/workflows/render_pages.yml@main diff --git a/.github/workflows/pr_check.yml b/.github/workflows/pr_check.yml index 6e3e3a8..3caa020 100644 --- a/.github/workflows/pr_check.yml +++ b/.github/workflows/pr_check.yml @@ -7,6 +7,6 @@ on: jobs: pr_check: + uses: r4ds/r4dsactions/.github/workflows/render_check.yml@main env: TORCH_INSTALL: 1 - uses: r4ds/r4dsactions/.github/workflows/render_check.yml@main From 04071304be16b68c7bfe4ccd02e954ee395497c1 Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Fri, 13 Oct 2023 10:02:48 -0500 Subject: [PATCH 15/18] Fix formatting. Probably still won't work but it's closer. --- .github/workflows/deploy_bookdown.yml | 3 ++- .github/workflows/pr_check.yml | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy_bookdown.yml b/.github/workflows/deploy_bookdown.yml index 07f3625..aa75756 100644 --- a/.github/workflows/deploy_bookdown.yml +++ b/.github/workflows/deploy_bookdown.yml @@ -7,6 +7,7 @@ on: jobs: bookdown: - uses: r4ds/r4dsactions/.github/workflows/render_pages.yml@main + runs-on: ubuntu-latest env: TORCH_INSTALL: 1 + uses: r4ds/r4dsactions/.github/workflows/render_pages.yml@main diff --git a/.github/workflows/pr_check.yml b/.github/workflows/pr_check.yml index 3caa020..a6238f4 100644 --- a/.github/workflows/pr_check.yml +++ b/.github/workflows/pr_check.yml @@ -7,6 +7,7 @@ on: jobs: pr_check: - uses: r4ds/r4dsactions/.github/workflows/render_check.yml@main + runs-on: ubuntu-latest env: TORCH_INSTALL: 1 + uses: r4ds/r4dsactions/.github/workflows/render_check.yml@main From cefd9548d4e296759c3dd1b7486725b7ce5cebf1 Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Fri, 13 Oct 2023 10:03:52 -0500 Subject: [PATCH 16/18] Move uses to steps. --- .github/workflows/deploy_bookdown.yml | 3 ++- .github/workflows/pr_check.yml | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/deploy_bookdown.yml b/.github/workflows/deploy_bookdown.yml index aa75756..7705a17 100644 --- a/.github/workflows/deploy_bookdown.yml +++ b/.github/workflows/deploy_bookdown.yml @@ -10,4 +10,5 @@ jobs: runs-on: ubuntu-latest env: TORCH_INSTALL: 1 - uses: r4ds/r4dsactions/.github/workflows/render_pages.yml@main + steps: + - uses: r4ds/r4dsactions/.github/workflows/render_pages.yml@main diff --git a/.github/workflows/pr_check.yml b/.github/workflows/pr_check.yml index a6238f4..cc3f874 100644 --- a/.github/workflows/pr_check.yml +++ b/.github/workflows/pr_check.yml @@ -10,4 +10,5 @@ jobs: runs-on: ubuntu-latest env: TORCH_INSTALL: 1 - uses: r4ds/r4dsactions/.github/workflows/render_check.yml@main + steps: + - uses: r4ds/r4dsactions/.github/workflows/render_check.yml@main From 721a41547d85e2fac06057a44a3b766212649732 Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Fri, 13 Oct 2023 10:05:28 -0500 Subject: [PATCH 17/18] Copy/paste reusable workflow. --- .github/workflows/pr_check.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pr_check.yml b/.github/workflows/pr_check.yml index cc3f874..d5b3a72 100644 --- a/.github/workflows/pr_check.yml +++ b/.github/workflows/pr_check.yml @@ -11,4 +11,13 @@ jobs: env: TORCH_INSTALL: 1 steps: - - uses: r4ds/r4dsactions/.github/workflows/render_check.yml@main + - uses: actions/checkout@v3 + - uses: r-lib/actions/setup-r@v2 + with: + r-version: '4.2.2' + - uses: r-lib/actions/setup-pandoc@v2 + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + cache-version: 1 + - name: Render Book + run: Rscript -e 'bookdown::render_book("index.Rmd")' From 4dcfa3038afdb85ec9ca04858f5805b8d944a901 Mon Sep 17 00:00:00 2001 From: Jon Harmon Date: Fri, 13 Oct 2023 10:33:12 -0500 Subject: [PATCH 18/18] Install torch in chapter 1. So if earlier chapters use it in later clubs, it's set to go. --- .github/workflows/deploy_bookdown.yml | 6 +----- .github/workflows/pr_check.yml | 15 +-------------- 01_overview-getting-familiar-with-torch.Rmd | 7 +++++++ 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/.github/workflows/deploy_bookdown.yml b/.github/workflows/deploy_bookdown.yml index 7705a17..eaa103f 100644 --- a/.github/workflows/deploy_bookdown.yml +++ b/.github/workflows/deploy_bookdown.yml @@ -7,8 +7,4 @@ on: jobs: bookdown: - runs-on: ubuntu-latest - env: - TORCH_INSTALL: 1 - steps: - - uses: r4ds/r4dsactions/.github/workflows/render_pages.yml@main + uses: r4ds/r4dsactions/.github/workflows/render_pages.yml@main diff --git a/.github/workflows/pr_check.yml b/.github/workflows/pr_check.yml index d5b3a72..b7b9756 100644 --- a/.github/workflows/pr_check.yml +++ b/.github/workflows/pr_check.yml @@ -7,17 +7,4 @@ on: jobs: pr_check: - runs-on: ubuntu-latest - env: - TORCH_INSTALL: 1 - steps: - - uses: actions/checkout@v3 - - uses: r-lib/actions/setup-r@v2 - with: - r-version: '4.2.2' - - uses: r-lib/actions/setup-pandoc@v2 - - uses: r-lib/actions/setup-r-dependencies@v2 - with: - cache-version: 1 - - name: Render Book - run: Rscript -e 'bookdown::render_book("index.Rmd")' + uses: r4ds/r4dsactions/.github/workflows/render_check.yml@main diff --git a/01_overview-getting-familiar-with-torch.Rmd b/01_overview-getting-familiar-with-torch.Rmd index d155f29..a19a892 100644 --- a/01_overview-getting-familiar-with-torch.Rmd +++ b/01_overview-getting-familiar-with-torch.Rmd @@ -4,6 +4,13 @@ - THESE ARE NICE TO HAVE BUT NOT ABSOLUTELY NECESSARY +## Setup {-} + +```{r install-torch} +Sys.setenv(TORCH_INSTALL = 1) +library(torch) +``` + ## SLIDE 1 {-} - ADD SLIDES AS SECTIONS (`##`).