In [1]:
import numpy as np
import torch
torch.manual_seed(1)
Out[1]:
In [2]:
x = torch.Tensor(2,2) # 2D Tensor
print(x)
x.type(), x.size(), x.numel()
Out[2]:
In [3]:
y = torch.Tensor(3,3,3) # 3D Tensor
print(y)
y.type(), y.size(), y.numel()
Out[3]:
In [4]:
torch.ones(3,3), torch.zeros(3,3), torch.eye(3,3), torch.randn(3,3)
Out[4]:
In [47]:
torch.arange(0,3, step=0.5)
Out[47]:
In [5]:
torch.Tensor([[1,2],[3,4]])
Out[5]:
In [6]:
numpy_matrix = np.array([[5,6],[7,8]])
torch.from_numpy(numpy_matrix)
Out[6]:
In [35]:
x = torch.Tensor([[1,1,1],[2,2,2],[3,3,3]])
x, x[0:2,:]
Out[35]:
In [34]:
x[0,:] = torch.Tensor([0,0,0])
x
Out[34]:
In [8]:
split = torch.split(x, split_size=1, dim=0)
print(split)
In [9]:
torch.cat(split, dim=0) # concatentae Tensor
Out[9]:
In [133]:
torch.stack(chunk, dim=0) # concatenate Tensor with New dimenstion
Out[133]:
In [48]:
# torch.masked_select(input, mask)
x = torch.randn(2,3)
mask = torch.ByteTensor([[0,0,1],[0,1,0]])
out = torch.masked_select(x,mask)
x, mask, out
Out[48]:
In [59]:
x = torch.zeros(2, 1, 2)
y = x.squeeze(1) #remove dimension of 1
x.size(), y.size()
Out[59]:
In [53]:
x.view(2,2)
Out[53]:
In [54]:
x.view(-1)
Out[54]:
In [58]:
x.view(-1,4), x.view(4,-1)
Out[58]:
In [39]:
x.unsqueeze(1)
Out[39]:
In [91]:
x1 = torch.FloatTensor([[1,2,3],[4,5,6]])
x2 = torch.FloatTensor([[1,2,3],[4,5,6]])
x1+ x2 , x1.add(x2), torch.add(x1,x2)
Out[91]:
In [66]:
x1 + 10 # broadingcasting
Out[66]:
In [68]:
x1 * x2, x1.mul(x2), torch.mul(x1,x2)
Out[68]:
In [69]:
x1 * 10
Out[69]:
In [74]:
x1.pow(2), torch.pow(x1,2), x1**2
Out[74]:
In [76]:
x1.log(), torch.log(x1)
Out[76]:
In [78]:
x1 % 2
Out[78]:
In [125]:
x1.sum(), x1.max(), x1.min(), x1.mean(), x1.std(), x1.abs(), x1.sqrt()
Out[125]:
In [103]:
value, index = x1.max(1)
value, index
Out[103]:
In [115]:
# torch.mm(mat1, mat2) -> matrix multiplication
x1 = torch.randn(3,4)
x2 = torch.randn(4,5)
torch.mm(x1,x2), x1.mm(x2)
Out[115]:
In [118]:
# torch.mv(mat1, vector) -> matrix vector multiplication
x = torch.randn(3,4)
v = torch.randn(4)
torch.mv(x,v), x.mv(v)
Out[118]:
In [110]:
# torch.bmm(batch1, batch2) -> batch matrix multiplication
x1 = torch.randn(10,3,4)
x2 = torch.randn(10,4,5)
torch.bmm(x1,x2).size()
Out[110]:
In [112]:
# torch.dot(tensor1,tensor2) -> dot product of two tensor
x1 = torch.randn(2,2)
x2 = torch.randn(2,2)
torch.dot(x1.view(-1),x2.view(-1))
Out[112]:
In [114]:
# torch.t(matrix) -> transposed matrix
x1 = torch.randn(3,4)
x1, x1.t()
Out[114]:
In [119]:
a = torch.Tensor([[8.79, 6.11, -9.15, 9.57, -3.49, 9.84],
[9.93, 6.91, -7.93, 1.64, 4.02, 0.15],
[9.83, 5.04, 4.86, 8.83, 9.80, -8.99],
[5.45, -0.27, 4.85, 0.74, 10.00, -6.02],
[3.16, 7.98, 3.01, 5.80, 4.27, -5.31]]).t()
a
In [121]:
u, s, v = torch.svd(a)
u,s,v
Out[121]:
In [124]:
torch.dist(a, torch.mm(torch.mm(u, torch.diag(s)), v.t()))
Out[124]:
In [4]:
x = torch.rand(2,2)
x
Out[4]:
In [133]:
x = torch.LongTensor([1,1])
x
Out[133]:
In [134]:
x.float()
Out[134]:
In [140]:
x.double()
Out[140]:
In [142]:
x.byte()
In [11]:
x = x.cuda()
x
Out[11]:
In [12]:
x = x.cpu()
x
Out[12]:
In [13]:
x.numpy()
Out[13]:
In [14]:
torch.from_numpy(np.array([[2,3],[1,2]]))
Out[14]:
In [ ]: