Skip to content

Instantly share code, notes, and snippets.

@vikene
Last active August 6, 2018 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vikene/1709045c00abf1b4c3148000dc0d71fc to your computer and use it in GitHub Desktop.
Save vikene/1709045c00abf1b4c3148000dc0d71fc to your computer and use it in GitHub Desktop.
import torch
#Creates two tensor objects
#Where X is a simple 1 Dimentional Tensor
#Y is a vector
X = torch.tensor(1.0)
Y = torch.tensor([1.0,2.0])
#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]])
print('Shape of X :',X.size())
print('Shape of Y :',Y.size())
print('Shape of Z :',z.size())
#we can create Tensors with different default values using
#Fills with uniform distribution from (0,1]
A = torch.rand(3,3)
#Fills random numbers between mean 0 and variance 1
B = torch.randn(3,3)
#Both creates a 3x3 tensor
#Uninitalized data, could be anything :P
C = torch.empty(20,20)
#strictly zeros
D = torch.zeros(5,5)
#We can get the type and also size using following
print(C.dtype)
print(C.size())
import torch
#Creates two tensor objects
#Where X is a simple 1 Dimentional Tensor
#Y is a vector
X = torch.tensor(1.0)
Y = torch.tensor([1.0,2.0])
#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]])
print('Shape of X :',X.size())
print('Shape of Y :',Y.size())
print('Shape of Z :',z.size())
#we can create Tensors with different default values using
#Fills with uniform distribution from (0,1]
A = torch.rand(3,3)
#Fills random numbers between mean 0 and variance 1
B = torch.randn(3,3)
#Both creates a 3x3 tensor
#Uninitalized data, could be anything :P
C = torch.empty(20,20)
#strictly zeros
D = torch.zeros(5,5)
#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