Skip to content

Instantly share code, notes, and snippets.

@vikene
Created August 6, 2018 08:17
Show Gist options
  • Save vikene/c2baf8e0f256605be86899fe8c74c9a6 to your computer and use it in GitHub Desktop.
Save vikene/c2baf8e0f256605be86899fe8c74c9a6 to your computer and use it in GitHub Desktop.
Basic Tensors using pytorch tutorial
import torch #import torch
X = torch.tensor(1.0)
Y = torch.tensor([1.0,2.0])
#Creates two tensor objects
#Alternatively we can also create a tensor from data like this
Z = torch.tensor([[1.0,2.0,3.0],
[2.0,3.0,4.0],
[3.0,4.0,5.0]])
#We can also look at the shape of each tensor as follows
print('Shape of Z: ',Z.shape) #prints same as Z.size()
#we can create Tensors with different default values using
A = torch.rand(3,3) #Fills with uniform distribution from (0,1]
#Or
B = torch.randn(3,3) #Fills random numbers between mean 0 and variance 1
#Both creates a 3x3 tensor
C = torch.empty(20,20) #Uninitalized data, could be anything :P
D = torch.zeros(5,5) #strictly zeros
#We can get the type and also size using following
print(C.dtype)
print(C.size())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment