3.2 Operations on Tensors

Define two tensors:

t1 <- torch_tensor(c(1,2))
t2 <- torch_tensor(c(3,4))

Add:

torch_add(t1, t2) # t1 is NOT modified
## torch_tensor
##  4
##  6
## [ CPUFloatType{2} ]
t1$add(t2) # t1 is NOT modified
## torch_tensor
##  4
##  6
## [ CPUFloatType{2} ]
t1$add_(t2) # t1 IS modified
## torch_tensor
##  4
##  6
## [ CPUFloatType{2} ]
t1
## torch_tensor
##  4
##  6
## [ CPUFloatType{2} ]

In general:

Another example:

t1 <- torch_tensor(1:3)
t2 <- torch_tensor(4:6)
t1$dot(t2)
## torch_tensor
## 32
## [ CPULongType{} ]

3.2.1 Summary operations

Create a matrix and a tensor using outer products:

m <- outer(1:3, 1:6)
t <- torch_outer(torch_tensor(1:3), torch_tensor(1:6))
apply(m, 1, sum) # row sums (of R matrix)
## [1] 21 42 63
t$sum(dim = 2) # row sums (of tensor)
## torch_tensor
##  21
##  42
##  63
## [ CPULongType{3} ]
  • 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
t <- torch_randn(4, 3, 2)
t
## torch_tensor
## (1,.,.) = 
##  -1.1857 -2.0498
##   0.1143  0.6361
##  -2.1201  0.7477
## 
## (2,.,.) = 
##  -0.6812 -0.2455
##   0.6659 -0.5313
##   0.2013  0.8907
## 
## (3,.,.) = 
##  -1.4984 -0.2056
##  -0.2714  1.9833
##   0.6964  1.1166
## 
## (4,.,.) = 
##  -0.9787  0.6257
##   0.4578 -0.6484
##  -0.5144  0.2885
## [ CPUFloatType{4,3,2} ]

Obtain averages of features, independent of subject (dimension 1) and time (dimension 2):

t$mean(dim = c(1, 2))
## torch_tensor
## -0.4262
##  0.2174
## [ CPUFloatType{2} ]