. Pytorch Basics 2

[3]:
1
import numpy as np
[2]:
1
import torch
[4]:
12
# difference betwee tensor and Tensor 
arr=np.array([1,2,3,4,5])
[5]:
1
float_tensor=torch.Tensor(arr) # This is just a float tensor
[7]:
1
float_tensor # numbers will be float tpye 
Out[7]:
tensor([1., 2., 3., 4., 5.])
[9]:
1
float_tensor.dtype # float type tensor
Out[9]:
torch.float32
[4]:
12
# Create a empty 2*2 tensor 
torch.empty(2,2)  # just empty block of memory is allocates , its not actully zeros
Out[4]:
tensor([[0., 0.], [0., 0.]])
[5]:
12
# to create actual zeros tensors 
torch.zeros(4,3,dtype=torch.int64)
Out[5]:
tensor([[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])
[6]:
1
torch.ones(4,3) # for ones 
Out[6]:
tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.]])
[7]:
1
torch.arange(0,18,2).reshape(3,3) #for range 
Out[7]:
tensor([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]])
[8]:
12
#linespace
torch.linspace(0,18,12).reshape(3,4)
Out[8]:
tensor([[ 0.0000, 1.6364, 3.2727, 4.9091], [ 6.5455, 8.1818, 9.8182, 11.4545], [13.0909, 14.7273, 16.3636, 18.0000]])
[9]:
1
torch.tensor([1,2,3]) # normal python list to tensor 
Out[9]:
tensor([1, 2, 3])
[10]:
12
# change int to float , or increase precision use .type method 
my_tensor=torch.tensor([1,2,3])
[11]:
1
my_tensor.dtype
Out[11]:
torch.int64
[13]:
1
my_tensor=my_tensor.type(torch.float)
[15]:
1
my_tensor.dtype # 32 bit integer 
Out[15]:
torch.float32
[17]:
123
# tensors with random numbers 
# random sample from uniform distribution(between 0 and 1)
torch.rand(4,3)
Out[17]:
tensor([[0.0642, 0.9729, 0.9474], [0.6554, 0.7476, 0.0173], [0.4503, 0.8049, 0.8815], [0.6116, 0.4514, 0.6972]])
[18]:
12
# normal distribution
torch.randn(4,3)
Out[18]:
tensor([[ 0.1052, -0.2184, -1.5320], [-0.8660, 1.6264, 0.3399], [-0.3530, -0.2078, -0.9368], [ 0.0481, -1.7387, -2.4484]])
[20]:
12
# random integers between range 
torch.randint(low=0,high=12,size=(5,5)) # you will not see 12 it is till 11 
Out[20]:
tensor([[11, 2, 9, 3, 10], [ 1, 11, 3, 6, 1], [ 3, 8, 6, 0, 2], [11, 0, 7, 11, 0], [ 1, 3, 7, 10, 4]])
[21]:
123
# create random tensor given a size from some other tensor 
x=torch.zeros(2,5)
[22]:
1
torch.rand_like(x) # create shape from x and create random tensor , uniform distribution
Out[22]:
tensor([[0.9382, 0.5901, 0.5928, 0.5222, 0.9934], [0.1292, 0.5470, 0.3656, 0.2307, 0.7721]])
[23]:
1
torch.randn_like(x) # create shape from x and create random tensor , normal distribution
Out[23]:
tensor([[ 0.2755, 1.7455, -2.4249, -0.2167, 1.4576], [-0.4912, 0.7233, -1.3640, 1.2439, 1.9534]])
[26]:
1
torch.randint_like(x,low=0,high=12) # create shape from x and create random tensor , integer
Out[26]:
tensor([[ 9., 2., 0., 4., 11.], [11., 10., 9., 7., 6.]])
[34]:
123
# setting seed for random values 
torch.manual_seed(42)
torch.rand(2,3)  # seed should be in same cell , if different cell then it will produce different values
Out[34]:
tensor([[0.8823, 0.9150, 0.3829], [0.9593, 0.3904, 0.6009]])
[31]:
1
Out[31]:
tensor([[0.2666, 0.6274, 0.2696], [0.4414, 0.2969, 0.8317]])
[ ]:
1