3.1 Creating tensors

3.1.1 Tensors from values

Defaults: long integer type and CPU device

Let’s test this:

torch_tensor(1:2)
## torch_tensor
##  1
##  2
## [ CPULongType{2} ]

In the example above, the type is long integer.

torch_tensor(c(1,2))
## torch_tensor
##  1
##  2
## [ CPUFloatType{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):

class(c(1,2))
## [1] "numeric"
class(1:2)
## [1] "integer"

Explicitly set the type and device:

torch_tensor(1:5, dtype = torch_float(), device = "cuda")

Two-dimensional tensor:

torch_tensor(matrix(1:9, ncol = 3, byrow = TRUE))
## torch_tensor
##  1  2  3
##  4  5  6
##  7  8  9
## [ CPULongType{3,3} ]

Higher dimensions:

torch_tensor(array(1:24, dim = c(4,3,2)))
## torch_tensor
## (1,.,.) = 
##    1  13
##    5  17
##    9  21
## 
## (2,.,.) = 
##    2  14
##    6  18
##   10  22
## 
## (3,.,.) = 
##    3  15
##    7  19
##   11  23
## 
## (4,.,.) = 
##    4  16
##    8  20
##   12  24
## [ CPULongType{4,3,2} ]

3.1.2 Tensors from specifications

#normal distribution:
torch_randn(3, 3)
## torch_tensor
## -0.1667 -1.2478 -0.7803
##  0.1401 -0.0415  1.1399
## -0.0668  0.0320  0.5980
## [ CPUFloatType{3,3} ]
#unfiorm distribution
torch_rand(3, 3)
## torch_tensor
##  0.4194  0.6816  0.2232
##  0.8247  0.6816  0.1533
##  0.8597  0.9562  0.4227
## [ CPUFloatType{3,3} ]
torch_zeros(2,2)
## torch_tensor
##  0  0
##  0  0
## [ CPUFloatType{2,2} ]
torch_ones(3,3)
## torch_tensor
##  1  1  1
##  1  1  1
##  1  1  1
## [ CPUFloatType{3,3} ]
torch_eye(3,3)
## torch_tensor
##  1  0  0
##  0  1  0
##  0  0  1
## [ CPUFloatType{3,3} ]
torch_diag(c(1,2,3))
## torch_tensor
##  1  0  0
##  0  2  0
##  0  0  3
## [ CPUFloatType{3,3} ]

See full list at https://torch.mlverse.org/docs/reference/#tensor-creation-utilities.

3.1.3 Tensors from datasets

We’ll look at the presidential dataset from the ggplot2 package:

data(presidential) # from ggplot2 
presidential |> glimpse()
## Rows: 12
## Columns: 4
## $ name  <chr> "Eisenhower", "Kennedy", "Johnson", "Nixon", "Ford", "Carter", "…
## $ start <date> 1953-01-20, 1961-01-20, 1963-11-22, 1969-01-20, 1974-08-09, 197…
## $ end   <date> 1961-01-20, 1963-11-22, 1969-01-20, 1974-08-09, 1977-01-20, 198…
## $ party <chr> "Republican", "Democratic", "Democratic", "Republican", "Republi…
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)
## torch_tensor
##      4  -6190  -3268      2
##      7  -3268  -2232      1
##      6  -2232   -346      1
##      8   -346   1681      2
##      5   1681   2576      2
## ... [the output was truncated (use n=-1 to disable)]
## [ CPUFloatType{12,4} ]

If your data contains NA’s, you’ll need to convert them before training a neural network (topic to be covered later).