3.2 Operations on Tensors
Define two tensors:
Add:
## torch_tensor
## 4
## 6
## [ CPUFloatType{2} ]
## torch_tensor
## 4
## 6
## [ CPUFloatType{2} ]
## torch_tensor
## 4
## 6
## [ CPUFloatType{2} ]
## torch_tensor
## 4
## 6
## [ CPUFloatType{2} ]
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()
, andtorch_multiply()
. See https://torch.mlverse.org/docs/reference/#mathematical-operations-on-tensors.
Another example:
## torch_tensor
## 32
## [ CPULongType{} ]
3.2.1 Summary operations
Create a matrix and a tensor using outer products:
## [1] 21 42 63
## 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
## 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):
## torch_tensor
## -0.4262
## 0.2174
## [ CPUFloatType{2} ]