3.3 Accessing Parts of a Tensor (indexing and slicing)
Indexing in torch
is 1-based. Very similar to functionality in R.
Example:
## torch_tensor
## 1 2 3
## 4 5 6
## 7 8 9
## [ CPULongType{3,3} ]
## torch_tensor
## 1
## 2
## 3
## [ CPULongType{3} ]
## torch_tensor
## 1 2 3
## [ CPULongType{1,3} ]
Slicing example:
## torch_tensor
## (1,.,.) =
## 0.2937 0.4863
## 0.8151 0.8476
##
## (2,.,.) =
## 0.9466 0.6171
## 0.6888 0.2210
## [ CPUFloatType{2,2,2} ]
3.3.1 Beyond R
Access last element:
## torch_tensor
## 4
## [ CPULongType{} ]
## torch_tensor
## 4
## [ CPULongType{} ]
Compare to R:
## [1] 4
## [1] 2 3 4
Step pattern:
## torch_tensor
## 1 2 3 4 5 6 7 8 9 10
## 11 12 13 14 15 16 17 18 19 20
## [ CPULongType{2,10} ]
## torch_tensor
## 1 3 5 7
## 11 13 15 17
## [ CPULongType{2,4} ]
Use ..
to designate all dimensions not explicitly referenced.
## torch_tensor
## (1,.,.) =
## 0.5503 1.5899
## 0.3364 -1.3595
##
## (2,.,.) =
## 0.3992 -0.1357
## 0.0885 1.1671
## [ CPUFloatType{2,2,2} ]
## torch_tensor
## 0.3992 -0.1357
## 0.0885 1.1671
## [ CPUFloatType{2,2} ]