In [3]:
from __future__ import print_function
import torch
In [4]:
print(torch.__version__)
In [32]:
x = torch.Tensor(5,3) # initialize as 0 or approxiamte to 0
print(x)
In [33]:
x = torch.rand(5,3)
print(x)
In [36]:
x.size()
Out[36]:
In [37]:
y = torch.rand(5,3)
print(y)
In [38]:
x+y
Out[38]:
In [40]:
torch.add(x,y)
Out[40]:
In [41]:
x+y == torch.add(x,y) # ele-wise
Out[41]:
In [45]:
result = torch.Tensor(5,3)
torch.add(x,y,out=result)
Out[45]:
In [46]:
result
Out[46]:
In [47]:
x.add_(y) # x will be change
Out[47]:
In [48]:
x.add_(y)
Out[48]:
Any operation that mutates a tensor in-place is post-fixed with an For example: x.copy(y), x.t_(), will change x.
In [52]:
x[:1] # equal to x[0:1] ,x[0]
Out[52]:
In [53]:
x[:,1]
Out[53]:
In [54]:
x[1]
Out[54]:
In [57]:
a = torch.ones(5)
print(a)
In [59]:
b = a.numpy()
print(b)
In [60]:
a.add_(1)
print(a)
print(b)
In [62]:
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
print(a)
print(b)
In [72]:
# let us run this cell only if CUDA is available
if torch.cuda.is_available():
print(111)
x = x.cuda()
y = y.cuda()
print(x + y)
In [73]:
In [ ]: