63. Basics of PyTorch#

import torch
import numpy as np

Tensors can be created using torch.Tensor(row, col)

row = 3
col = 2
tensor_1 = torch.Tensor( row, col )
print(tensor_1)
print(tensor_1.size())
tensor([[-4.6739e+19,  4.5766e-41],
        [-4.5579e+19,  4.5766e-41],
        [ 1.4824e+06,  2.5331e-03]])
torch.Size([3, 2])

It’s also useful to have random matrices.

torch.rand( row, col )
tensor([[0.1084, 0.4474],
        [0.4898, 0.6890],
        [0.3914, 0.4056]])

Addition

tensor_rand_1 = torch.rand(row,col)
tensor_rand_2 = torch.rand(row,col)
print(tensor_rand_1,tensor_rand_2, tensor_rand_1+tensor_rand_2, torch.add(tensor_rand_1, tensor_rand_2) )
tensor([[0.2099, 0.0021],
        [0.6671, 0.4352],
        [0.2508, 0.6668]]) tensor([[0.9219, 0.3698],
        [0.6310, 0.5634],
        [0.2768, 0.9205]]) tensor([[1.1317, 0.3719],
        [1.2981, 0.9986],
        [0.5275, 1.5873]]) tensor([[1.1317, 0.3719],
        [1.2981, 0.9986],
        [0.5275, 1.5873]])

torch.and() function can take argument and assign the results to a specific tensor.

tensor_rand_1p2 = torch.Tensor(row, col)
print(tensor_rand_1p2)
torch.add(tensor_rand_1, tensor_rand_2, out = tensor_rand_1p2)
print(tensor_rand_1p2)
tensor([[5.6933e-28, 4.5768e-41],
        [2.8551e-08, 3.0851e-41],
        [4.4842e-44, 0.0000e+00]])
tensor([[1.1317, 0.3719],
        [1.2981, 0.9986],
        [0.5275, 1.5873]])

Tensors have member function add_(). This _ indicates that the tensor itself will be mutated.

tensor_2 = ( torch.Tensor(row, col) ).zero_() # initialize the tensor to be zeros
print(tensor_2)
tensor_2.add_(tensor_1);
print(tensor_2)
tensor([[0., 0.],
        [0., 0.],
        [0., 0.]])
tensor([[-4.6739e+19,  4.5766e-41],
        [-4.5579e+19,  4.5766e-41],
        [ 1.4824e+06,  2.5331e-03]])

Slicing

tensor_2[1,1]
tensor(4.5766e-41)

Reshape the tensor using .view()

print( tensor_2.view(row*col), tensor_2.view( col,row ) )
tensor([-4.6739e+19,  4.5766e-41, -4.5579e+19,  4.5766e-41,  1.4824e+06,
         2.5331e-03]) tensor([[-4.6739e+19,  4.5766e-41, -4.5579e+19],
        [ 4.5766e-41,  1.4824e+06,  2.5331e-03]])

Create ones and zeros

print( torch.ones(row, col), torch.zeros(row, col) )
tensor([[1., 1.],
        [1., 1.],
        [1., 1.]]) tensor([[0., 0.],
        [0., 0.],
        [0., 0.]])

Tensors can be converted to numpy arrays.

print( tensor_2 )
print( tensor_2.numpy() )
tensor([[-4.6739e+19,  4.5766e-41],
        [-4.5579e+19,  4.5766e-41],
        [ 1.4824e+06,  2.5331e-03]])
[[-4.6739412e+19  4.5766408e-41]
 [-4.5578610e+19  4.5766408e-41]
 [ 1.4824439e+06  2.5331338e-03]]

Numpy array can be converted to tensors

np_arr_1 = np.ones( (row, col) )
print(np_arr_1)
tensor_from_np_arr_1 = torch.from_numpy( np_arr_1 )
print(tensor_from_np_arr_1)
[[1. 1.]
 [1. 1.]
 [1. 1.]]
tensor([[1., 1.],
        [1., 1.],
        [1., 1.]], dtype=torch.float64)