01_Tensor


In [1]:
import numpy as np
import torch
torch.manual_seed(1)


Out[1]:
<torch._C.Generator at 0x7f98a4171258>

Tensor is a matrix!


In [2]:
x = torch.Tensor(2,2) # 2D Tensor
print(x)
x.type(), x.size(), x.numel()


-2.7727e-29  4.5779e-41
-2.7727e-29  4.5779e-41
[torch.FloatTensor of size 2x2]

Out[2]:
('torch.FloatTensor', torch.Size([2, 2]), 4)

In [3]:
y = torch.Tensor(3,3,3) # 3D Tensor
print(y)
y.type(), y.size(), y.numel()


(0 ,.,.) = 
1.00000e-29 *
  -2.7727  0.0000 -2.7727
   0.0000  0.0000  0.0000
   0.0000  0.0000  0.0000

(1 ,.,.) = 
1.00000e-29 *
   0.0000  0.0000  0.0000
   0.0000  0.0000  0.0000
   0.0000  0.0000  0.0000

(2 ,.,.) = 
1.00000e-29 *
   0.0000  0.0000  0.0000
   0.0000  0.0000  0.0000
  -2.7727  0.0000  0.0000
[torch.FloatTensor of size 3x3x3]

Out[3]:
('torch.FloatTensor', torch.Size([3, 3, 3]), 27)

A few methods to create a Tensor


In [4]:
torch.ones(3,3), torch.zeros(3,3), torch.eye(3,3), torch.randn(3,3)


Out[4]:
(
  1  1  1
  1  1  1
  1  1  1
 [torch.FloatTensor of size 3x3], 
  0  0  0
  0  0  0
  0  0  0
 [torch.FloatTensor of size 3x3], 
  1  0  0
  0  1  0
  0  0  1
 [torch.FloatTensor of size 3x3], 
 -2.9718  1.7070 -0.4305
 -2.2820  0.5237  0.0004
 -1.2039  3.5283  0.4434
 [torch.FloatTensor of size 3x3])

In [47]:
torch.arange(0,3, step=0.5)


Out[47]:
 0.0000
 0.5000
 1.0000
 1.5000
 2.0000
 2.5000
[torch.FloatTensor of size 6]

In [5]:
torch.Tensor([[1,2],[3,4]])


Out[5]:
 1  2
 3  4
[torch.FloatTensor of size 2x2]

In [6]:
numpy_matrix = np.array([[5,6],[7,8]])
torch.from_numpy(numpy_matrix)


Out[6]:
 5  6
 7  8
[torch.LongTensor of size 2x2]

Slicing, Concatenating, and Masking Tensor


In [35]:
x = torch.Tensor([[1,1,1],[2,2,2],[3,3,3]])
x, x[0:2,:]


Out[35]:
(
  1  1  1
  2  2  2
  3  3  3
 [torch.FloatTensor of size 3x3], 
  1  1  1
  2  2  2
 [torch.FloatTensor of size 2x3])

In [34]:
x[0,:] = torch.Tensor([0,0,0])
x


Out[34]:
 0  0  0
 2  2  2
 3  3  3
[torch.FloatTensor of size 3x3]

In [8]:
split = torch.split(x, split_size=1, dim=0)
print(split)


(
 1  1  1
[torch.FloatTensor of size 1x3]
, 
 2  2  2
[torch.FloatTensor of size 1x3]
, 
 3  3  3
[torch.FloatTensor of size 1x3]
)

In [9]:
torch.cat(split, dim=0) # concatentae Tensor


Out[9]:
 1  1  1
 2  2  2
 3  3  3
[torch.FloatTensor of size 3x3]

In [133]:
torch.stack(chunk, dim=0) # concatenate Tensor with New dimenstion


Out[133]:
(0 ,.,.) = 
  0  0  0

(1 ,.,.) = 
  0  0  0

(2 ,.,.) = 
  0  0  0
[torch.FloatTensor of size 3x1x3]

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]:
(
  0.5848  0.8407  0.5510
  0.3863  0.9124 -0.8410
 [torch.FloatTensor of size 2x3], 
  0  0  1
  0  1  0
 [torch.ByteTensor of size 2x3], 
  0.5510
  0.9124
 [torch.FloatTensor of size 2])

Reshaping on Dimension of Tensor


In [59]:
x  = torch.zeros(2, 1, 2)

y = x.squeeze(1)  #remove dimension of 1

x.size(), y.size()


Out[59]:
(torch.Size([2, 1, 2]), torch.Size([2, 2]))

In [53]:
x.view(2,2)


Out[53]:
 0  0
 0  0
[torch.FloatTensor of size 2x2]

In [54]:
x.view(-1)


Out[54]:
 0
 0
 0
 0
[torch.FloatTensor of size 4]

In [58]:
x.view(-1,4), x.view(4,-1)


Out[58]:
(
  0  0  0  0
 [torch.FloatTensor of size 1x4], 
  0
  0
  0
  0
 [torch.FloatTensor of size 4x1])

In [39]:
x.unsqueeze(1)


Out[39]:
(0 ,.,.) = 
  1  1  1

(1 ,.,.) = 
  2  2  2

(2 ,.,.) = 
  3  3  3
[torch.FloatTensor of size 3x1x3]

Tensor Operations

Arthmetic Operations


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]:
(
   2   4   6
   8  10  12
 [torch.FloatTensor of size 2x3], 
   2   4   6
   8  10  12
 [torch.FloatTensor of size 2x3], 
   2   4   6
   8  10  12
 [torch.FloatTensor of size 2x3])

In [66]:
x1 + 10 # broadingcasting


Out[66]:
 11  12  13
 14  15  16
[torch.FloatTensor of size 2x3]

In [68]:
x1 * x2, x1.mul(x2), torch.mul(x1,x2)


Out[68]:
(
   1   4   9
  16  25  36
 [torch.FloatTensor of size 2x3], 
   1   4   9
  16  25  36
 [torch.FloatTensor of size 2x3], 
   1   4   9
  16  25  36
 [torch.FloatTensor of size 2x3])

In [69]:
x1 * 10


Out[69]:
 10  20  30
 40  50  60
[torch.FloatTensor of size 2x3]

Some useful math Operation


In [74]:
x1.pow(2), torch.pow(x1,2), x1**2


Out[74]:
(
   1   4   9
  16  25  36
 [torch.FloatTensor of size 2x3], 
   1   4   9
  16  25  36
 [torch.FloatTensor of size 2x3], 
   1   4   9
  16  25  36
 [torch.FloatTensor of size 2x3])

In [76]:
x1.log(), torch.log(x1)


Out[76]:
(
  0.0000  0.6931  1.0986
  1.3863  1.6094  1.7918
 [torch.FloatTensor of size 2x3], 
  0.0000  0.6931  1.0986
  1.3863  1.6094  1.7918
 [torch.FloatTensor of size 2x3])

In [78]:
x1 % 2


Out[78]:
 1  0  1
 0  1  0
[torch.FloatTensor of size 2x3]

In [125]:
x1.sum(), x1.max(), x1.min(), x1.mean(), x1.std(), x1.abs(), x1.sqrt()


Out[125]:
(-1.7373209223151207,
 1.4351236820220947,
 -1.3037976026535034,
 -0.14477674352626005,
 0.9596309507617423,
 
  0.7159  1.0067  1.1036  0.9792
  1.4351  0.3460  1.1480  1.3038
  0.4588  0.0625  0.2993  1.0850
 [torch.FloatTensor of size 3x4],
 
  0.8461     nan     nan     nan
  1.1980  0.5882     nan     nan
  0.6774  0.2499     nan  1.0417
 [torch.FloatTensor of size 3x4])

In [103]:
value, index = x1.max(1)
value, index


Out[103]:
(
  3
  6
 [torch.FloatTensor of size 2], 
  2
  2
 [torch.LongTensor of size 2])

Matrix operations


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]:
(
  0.7430  1.0564  5.1072 -2.4826 -2.6390
  2.5699 -0.3730  6.1607 -2.4609  1.1350
 -1.3680 -0.7426 -0.7126 -1.0130  1.3272
 [torch.FloatTensor of size 3x5], 
  0.7430  1.0564  5.1072 -2.4826 -2.6390
  2.5699 -0.3730  6.1607 -2.4609  1.1350
 -1.3680 -0.7426 -0.7126 -1.0130  1.3272
 [torch.FloatTensor of size 3x5], 
  0.7430  1.0564  5.1072 -2.4826 -2.6390
  2.5699 -0.3730  6.1607 -2.4609  1.1350
 -1.3680 -0.7426 -0.7126 -1.0130  1.3272
 [torch.FloatTensor of size 3x5])

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]:
(
 -0.0787
 -4.4828
  0.7590
 [torch.FloatTensor of size 3], 
 -0.0787
 -4.4828
  0.7590
 [torch.FloatTensor of size 3])

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]:
torch.Size([10, 3, 5])

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]:
1.2282490730285645

In [114]:
# torch.t(matrix) -> transposed matrix

x1 = torch.randn(3,4)

x1, x1.t()


Out[114]:
(
 -0.6521  0.4788 -1.4859  1.2957
  0.2860  0.3352 -2.6214 -0.0975
 -0.8456 -0.0528  0.8662 -0.0318
 [torch.FloatTensor of size 3x4], 
 -0.6521  0.2860 -0.8456
  0.4788  0.3352 -0.0528
 -1.4859 -2.6214  0.8662
  1.2957 -0.0975 -0.0318
 [torch.FloatTensor of size 4x3])

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]:
(
 -0.5911  0.2632  0.3554  0.3143  0.2299
 -0.3976  0.2438 -0.2224 -0.7535 -0.3636
 -0.0335 -0.6003 -0.4508  0.2334 -0.3055
 -0.4297  0.2362 -0.6859  0.3319  0.1649
 -0.4697 -0.3509  0.3874  0.1587 -0.5183
  0.2934  0.5763 -0.0209  0.3791 -0.6526
 [torch.FloatTensor of size 6x5], 
  27.4687
  22.6432
   8.5584
   5.9857
   2.0149
 [torch.FloatTensor of size 5], 
 -0.2514  0.8148 -0.2606  0.3967 -0.2180
 -0.3968  0.3587  0.7008 -0.4507  0.1402
 -0.6922 -0.2489 -0.2208  0.2513  0.5891
 -0.3662 -0.3686  0.3859  0.4342 -0.6265
 -0.4076 -0.0980 -0.4933 -0.6227 -0.4396
 [torch.FloatTensor of size 5x5])

In [124]:
torch.dist(a, torch.mm(torch.mm(u, torch.diag(s)), v.t()))


Out[124]:
1.091761987481732e-05

Tensor Data Type


In [4]:
x = torch.rand(2,2)
x


Out[4]:
 0.0923  0.3966
 0.1863  0.3879
[torch.FloatTensor of size 2x2]

In [133]:
x = torch.LongTensor([1,1])
x


Out[133]:
 1
 1
[torch.LongTensor of size 2]

In [134]:
x.float()


Out[134]:
 1
 1
[torch.FloatTensor of size 2]

In [140]:
x.double()


Out[140]:
 1
 1
[torch.DoubleTensor of size 2]

In [142]:
x.byte()

In [11]:
x = x.cuda()
x


Out[11]:
 0.0923  0.3966
 0.1863  0.3879
[torch.cuda.FloatTensor of size 2x2 (GPU 0)]

In [12]:
x = x.cpu()
x


Out[12]:
 0.0923  0.3966
 0.1863  0.3879
[torch.FloatTensor of size 2x2]

In [13]:
x.numpy()


Out[13]:
array([[ 0.09233859,  0.39658073],
       [ 0.18626021,  0.38791075]], dtype=float32)

In [14]:
torch.from_numpy(np.array([[2,3],[1,2]]))


Out[14]:
 2  3
 1  2
[torch.LongTensor of size 2x2]

In [ ]: