< Back
Env
import torch
x = torch.arange(12)
x
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
x.shape
torch.Size([12])
x.numel()
12
 
change shape
X = x.reshape(3, 4)
X
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
X.shape
torch.Size([3, 4])
X.numel()
12
 
transpose
X.T
X
tensor([[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]])
 
create by python list
P = torch.tensor([[2, 1, 4, 3], [1, 2, 3, 4], [4, 3, 2, 1]])
P
tensor([[2, 1, 4, 3],
[1, 2, 3, 4],
[4, 3, 2, 1]])
P.shape
torch.Size([3, 4])
P.numel()
12
Other specific values
 
0
Z = torch.zeros(2,3,4)
Z
tensor([[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]])
Z.shape
torch.Size([2, 3, 4])
Z.numel()
24
 
1
O = torch.ones((2, 3, 4))
O
tensor([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]],
[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]])
Z.shape
torch.Size([2, 3, 4])
Z.numel()
24
create by sum
 
X = torch.arange(12, dtype=torch.float32).reshape((3,4))
S = X.sum()
X
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]])
S
tensor(66.)