Pytorch TensorΒΆ
[1]:
import torch
[2]:
torch.manual_seed(1)
[2]:
<torch._C.Generator at 0x7fd3e757fa30>
[3]:
x = torch.rand((4, 3), dtype=torch.float32)
print(x)
tensor([[0.7576, 0.2793, 0.4031],
[0.7347, 0.0293, 0.7999],
[0.3971, 0.7544, 0.5695],
[0.4388, 0.6387, 0.5247]])
[4]:
print(x.shape)
rows, columns = x.shape
rows, columns
torch.Size([4, 3])
[4]:
(4, 3)
[5]:
x.T, x.t()
[5]:
(tensor([[0.7576, 0.7347, 0.3971, 0.4388],
[0.2793, 0.0293, 0.7544, 0.6387],
[0.4031, 0.7999, 0.5695, 0.5247]]),
tensor([[0.7576, 0.7347, 0.3971, 0.4388],
[0.2793, 0.0293, 0.7544, 0.6387],
[0.4031, 0.7999, 0.5695, 0.5247]]))
[6]:
x + x
[6]:
tensor([[1.5153, 0.5586, 0.8061],
[1.4694, 0.0586, 1.5997],
[0.7943, 1.5087, 1.1390],
[0.8776, 1.2774, 1.0493]])
[7]:
x @ x.T
[7]:
tensor([[0.8145, 0.8872, 0.7411, 0.7223],
[0.8872, 1.1804, 0.7694, 0.7607],
[0.7411, 0.7694, 1.0511, 0.9549],
[0.7223, 0.7607, 0.9549, 0.8757]])
[8]:
x * x
[8]:
tensor([[0.5740, 0.0780, 0.1625],
[0.5398, 0.0009, 0.6398],
[0.1577, 0.5691, 0.3243],
[0.1925, 0.4079, 0.2753]])
[9]:
u, s, vT = torch.svd(x)
u, s, vT
[9]:
(tensor([[-0.4645, 0.2591, 0.8391],
[-0.5298, 0.6808, -0.5055],
[-0.5164, -0.5608, -0.1963],
[-0.4866, -0.3935, -0.0422]]),
tensor([1.8458, 0.6668, 0.2649]),
tensor([[-0.6284, 0.4516, 0.6334],
[-0.4581, -0.8729, 0.1679],
[-0.6287, 0.1847, -0.7554]]))