In [0]:
import torch
In [2]:
## empty matrix
x = torch.empty(5,3)
x
Out[2]:
In [4]:
## random matrix
x = torch.rand(3,2)
x
Out[4]:
In [6]:
## zero matrix with dtype as long.
x = torch.zeros(5,3, dtype=torch.long)
x
Out[6]:
In [11]:
## prepare a tensor directly from data
data = [5.2, 3]
x = torch.tensor(data, dtype=torch.int)
x
Out[11]:
In [15]:
## create a new tensor from existing tensor by overriding
x = x.new_ones(2,2)
x = torch.randn_like(x, dtype=float)
x ## note the structure remains the same as the previous tensor
Out[15]:
In [16]:
## size of tensor
x.size()
Out[16]:
In [18]:
## adding two tensors
y = torch.rand(2,2)
print(y)
x.add(y)
Out[18]:
In [21]:
print(x[:,1])
In [5]:
## numpy bridge
x = torch.ones(2,3)
x
Out[5]:
In [7]:
a = x.numpy()
a
Out[7]:
In [0]: