## Sunday, November 12, 2017

### Numpy Vs Pytorch

In [1]:
import numpy as np
import torch


### 1 Creating a matrix with numpy vs pytorch¶

In [2]:
arr = [[1,2], [2,3]]

In [3]:
np.array(arr)

Out[3]:
array([[1, 2],
[2, 3]])
In [4]:
torch.Tensor(arr)

Out[4]:
 1  2
2  3
[torch.FloatTensor of size 2x2]
In [7]:
np.ones((2,2))

Out[7]:
array([[ 1.,  1.],
[ 1.,  1.]])
In [8]:
torch.ones((2,2))

Out[8]:
 1  1
1  1
[torch.FloatTensor of size 2x2]
In [9]:
np.zeros((2,3))

Out[9]:
array([[ 0.,  0.,  0.],
[ 0.,  0.,  0.]])
In [10]:
torch.zeros((2,3))

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

### 2 Random numbers¶

In [11]:
np.random.rand(2,3)

Out[11]:
array([[ 0.68749833,  0.62618793,  0.34557432],
[ 0.95888593,  0.35415139,  0.48186146]])
In [12]:
torch.rand(2,3)

Out[12]:
 0.3996  0.4675  0.3076
0.5733  0.7604  0.5373
[torch.FloatTensor of size 2x3]

### 3 Seed for reproducibility¶

In [15]:
np.random.seed(8)
np.random.randn(2,2)

Out[15]:
array([[ 0.09120472,  1.09128273],
[-1.94697031, -1.38634953]])
In [16]:
np.random.seed(8)
np.random.randn(2,2)

Out[16]:
array([[ 0.09120472,  1.09128273],
[-1.94697031, -1.38634953]])
In [17]:
np.random.randn(2,2)

Out[17]:
array([[-2.29649157,  2.4098343 ],
[ 1.72783617,  2.20455628]])
In [18]:
torch.manual_seed(8)
torch.randn(2,2)

Out[18]:
 0.1047 -0.1068
0.7255 -0.1453
[torch.FloatTensor of size 2x2]
In [19]:
torch.manual_seed(8)
torch.randn(2,2)

Out[19]:
 0.1047 -0.1068
0.7255 -0.1453
[torch.FloatTensor of size 2x2]
In [20]:
torch.randn(2,2)

Out[20]:
 0.6629 -0.7132
-1.8069 -0.3548
[torch.FloatTensor of size 2x2]

### 4 from_numpy()¶

In [22]:
numpy_arr = np.array(np.random.rand(2,2))
numpy_arr

Out[22]:
array([[ 0.28907503,  0.97385524],
[ 0.33377405,  0.21880106]])
In [23]:
type(numpy_arr)

Out[23]:
numpy.ndarray
In [25]:
pytorch_arr = torch.from_numpy(numpy_arr)
pytorch_arr

Out[25]:
 0.2891  0.9739
0.3338  0.2188
[torch.DoubleTensor of size 2x2]
In [26]:
type(pytorch_arr)

Out[26]:
torch.DoubleTensor

#### How to change the datatype to IntTensor?¶

In [32]:
numpy_arr = np.array(np.random.rand(2,2), dtype=np.int32)
numpy_arr

Out[32]:
array([[0, 0],
[0, 0]], dtype=int32)
In [33]:
type(numpy_arr)

Out[33]:
numpy.ndarray
In [34]:
pytorch_arr = torch.from_numpy(numpy_arr)
pytorch_arr

Out[34]:
 0  0
0  0
[torch.IntTensor of size 2x2]
In [35]:
type(pytorch_arr)

Out[35]:
torch.IntTensor
We can convert any numpy array to pytorch tensors using $\verb|torch.from_numpy()|$

### How to convert torch tensors to numpy array?¶

In [37]:
torch_tensors = torch.randn(2,2)
torch_tensors

Out[37]:
-1.5444  0.7212
-0.8290  0.5838
[torch.FloatTensor of size 2x2]
In [38]:
numpy_arr = torch_tensors.numpy()
numpy_arr

Out[38]:
array([[-1.54443169,  0.72120529],
[-0.82901162,  0.5837965 ]], dtype=float32)

### How to resize torch tensors?¶

##### Numpy¶
In [43]:
numpy_arr = np.zeros((2,2))
numpy_arr

Out[43]:
array([[ 0.,  0.],
[ 0.,  0.]])
In [45]:
numpy_arr.reshape(1,4)

Out[45]:
array([[ 0.,  0.,  0.,  0.]])
##### PyTorch¶
In [46]:
torch_ten = torch.zeros((2,2))
torch_ten

Out[46]:
 0  0
0  0
[torch.FloatTensor of size 2x2]
In [47]:
torch_ten.view(1,4)

Out[47]:
 0  0  0  0
[torch.FloatTensor of size 1x4]
In [48]:
numpy_arr.shape

Out[48]:
(2, 2)
In [51]:
torch_ten.size()

Out[51]:
torch.Size([2, 2])

### How to do element-wise addition?¶

##### Numpy¶
In [70]:
a = np.array([[1,2], [2,3]])
a

Out[70]:
array([[1, 2],
[2, 3]])
In [71]:
b = np.array([[1,2], [2,3]])
b

Out[71]:
array([[1, 2],
[2, 3]])
In [72]:
a+b

Out[72]:
array([[2, 4],
[4, 6]])
In [73]:
np.add(a,b)

Out[73]:
array([[2, 4],
[4, 6]])
##### PyTorch¶
In [74]:
a = torch.Tensor([[1,2], [2,3]])
a

Out[74]:
 1  2
2  3
[torch.FloatTensor of size 2x2]
In [75]:
b = torch.Tensor([[1,2], [2,3]])
b

Out[75]:
 1  2
2  3
[torch.FloatTensor of size 2x2]
In [76]:
a+b

Out[76]:
 2  4
4  6
[torch.FloatTensor of size 2x2]
In [77]:
torch.add(a,b)

Out[77]:
 2  4
4  6
[torch.FloatTensor of size 2x2]

### How to perform inplace additions?¶

##### PyTorch¶
In [104]:
a = torch.Tensor([[1,2], [2,3]])
a

Out[104]:
 1  2
2  3
[torch.FloatTensor of size 2x2]
In [105]:
b = torch.Tensor([[1,2], [2,3]])
b

Out[105]:
 1  2
2  3
[torch.FloatTensor of size 2x2]
In [106]:
a.add(b) # not in place

Out[106]:
 2  4
4  6
[torch.FloatTensor of size 2x2]
In [107]:
a

Out[107]:
 1  2
2  3
[torch.FloatTensor of size 2x2]
In [108]:
a.add_(b) # in plcace

Out[108]:
 2  4
4  6
[torch.FloatTensor of size 2x2]
In [109]:
a

Out[109]:
 2  4
4  6
[torch.FloatTensor of size 2x2]
In [110]:
np.multiply(a,b)

Out[110]:
  2   8
8  18
[torch.FloatTensor of size 2x2]
In [111]:
torch.mul(a,b)

Out[111]:
  2   8
8  18
[torch.FloatTensor of size 2x2]
In [112]:
a.mul(b)

Out[112]:
  2   8
8  18
[torch.FloatTensor of size 2x2]

### How to perform mean operation?¶

In [118]:
numpy_arr = np.array([[1,2,3,4,5], [1,2,3,4,9]])
numpy_arr

Out[118]:
array([[1, 2, 3, 4, 5],
[1, 2, 3, 4, 9]])
In [119]:
numpy_arr.mean()

Out[119]:
3.3999999999999999
In [120]:
numpy_arr.mean(axis=1)

Out[120]:
array([ 3. ,  3.8])
In [122]:
torch_ten = torch.Tensor([[1,2,3,4,5],[1,2,3,4,9]])
torch_ten

Out[122]:
 1  2  3  4  5
1  2  3  4  9
[torch.FloatTensor of size 2x5]
In [123]:
torch_ten.mean()

Out[123]:
3.4
In [126]:
torch_ten.mean(dim=1)

Out[126]:
 3.0000
3.8000
[torch.FloatTensor of size 2]
In [127]:
numpy_arr.shape

Out[127]:
(2, 5)
In [128]:
torch_ten.size()

Out[128]:
torch.Size([2, 5])