Skip to content

Commit

Permalink
cleanup. added torch_tensor() example and stride definition.
Browse files Browse the repository at this point in the history
  • Loading branch information
AmandaRP committed Oct 4, 2023
1 parent feef41e commit f92d149
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions 03_tensors.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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")
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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}
Expand All @@ -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()`.
Expand Down

0 comments on commit f92d149

Please sign in to comment.