Pytorch
+ : torch.add()
- : torch.sub()
* : torch.mul()
/ : torch.div()
x = torch.randn(3, 4)
x
>> tensor([[ 0.1392, -0.4403, -0.1479, -1.8080],
[ 0.0270, 0.2358, -0.5572, -0.9726],
[-0.0528, 0.0768, -1.2052, -1.3905]])
indices = torch.tensor([1, 2])
torch.index_select(x, 0, indices)
>> tensor([[ 0.0270, 0.2358, -0.5572, -0.9726],
[-0.0528, 0.0768, -1.2052, -1.3905]])
indices = torch.tensor([2,])
torch.index_select(x, 0, indices)
>> tensor([[-0.0528, 0.0768, -1.2052, -1.3905]])
indices = torch.tensor([0, 1, 3])
torch.index_select(x, 1, indices)
>> tensor([[ 0.1392, -0.4403, -1.8080],
[ 0.0270, 0.2358, -0.9726],
torch.gather(input, dim,index)
a = numpy.array([1, 2, 3])
t = torch.from_numpy(a)
t
t[0] = -1
a
array([-1, 2, 3])
t = torch.tensor([[1, 2, 3],
[4, 5, 6]])
print(torch.chunk(t, 2, 0))
print(torch.chunk(t, 2, 1))
(tensor([[1, 2, 3]]), tensor([[4, 5, 6]]))
(tensor([[1, 2],
[4, 5]]), tensor([[3],
[6]]))
x = torch.tensor([[[0,1],[2,3]],[[4,5],[6,7]]])
torch.swapdims(x, 0, 1)
torch.swapdims(x, 0, 2)
torch.randn(4)
torch.randn(2, 3)
torch.randperm(4)
a = torch.randn(5)
torch.log1p(a)
a = torch.tensor([[3.142, -3.142], [6.283, -6.283], [1.570, -1.570]])
torch.rad2deg(a)
a = torch.randn(1, 3)
a
tensor([[-0.8020, 0.5428, -1.5854]])
torch.prod(a)
tensor(0.6902)
torch.count_nonzero(x)
torch.count_nonzero(x, dim=0)
a = torch.randn(4, 4)
a
tensor([[ 1.3398, 0.2663, -0.2686, 0.2450],
[-0.7401, -0.8805, -0.3402, -1.1936],
[ 0.4907, -1.3948, -1.0691, -0.3132],
[-1.6092, 0.5419, -0.2993, 0.3195]])
torch.argmax(a, dim=1)
tensor([ 0, 2, 0, 1])
a = torch.randn(4, 4)
a
tensor([[ 0.0785, 1.5267, -0.8521, 0.4065],
[ 0.1598, 0.0788, -0.0745, -1.2700],
[ 1.2208, 1.0722, -0.7064, 1.2564],
[ 0.0669, -0.2318, -0.8229, -0.9280]])
torch.argsort(a, dim=1)
tensor([[2, 0, 3, 1],
[3, 2, 1, 0],
[2, 1, 0, 3],
[3, 2, 1, 0]])
a = torch.randn(3, 3)
a
tensor([[ 0.2309, 0.5207, 2.0049],
[ 0.2072, -1.0680, 0.6602],
[ 0.3480, -0.5211, -0.4573]])
torch.triu(a)
tensor([[ 0.2309, 0.5207, 2.0049],
[ 0.0000, -1.0680, 0.6602],
[ 0.0000, 0.0000, -0.4573]])
torch.triu(a, diagonal=1)
tensor([[ 0.0000, 0.5207, 2.0049],
[ 0.0000, 0.0000, 0.6602],
[ 0.0000, 0.0000, 0.0000]])
torch.triu(a, diagonal=-1)
tensor([[ 0.2309, 0.5207, 2.0049],
[ 0.2072, -1.0680, 0.6602],
[ 0.0000, -0.5211, -0.4573]])