In [3]:
from __future__ import print_function
import torch

In [4]:
print(torch.__version__)


0.1.12

In [32]:
x = torch.Tensor(5,3)  # initialize as 0 or approxiamte to 0
print(x)


1.00000e-42 *
  0.0000  0.0000  0.0000
  0.0000  0.0000  0.0000
  0.0000  0.0000  0.0000
  0.0000  1.5975  0.0000
  0.0000  0.6208  0.0000
[torch.FloatTensor of size 5x3]


In [33]:
x = torch.rand(5,3)
print(x)


 0.5446  0.9017  0.3697
 0.2556  0.0482  0.2955
 0.5412  0.3691  0.4219
 0.0770  0.7549  0.3826
 0.4210  0.8101  0.5251
[torch.FloatTensor of size 5x3]


In [36]:
x.size()


Out[36]:
torch.Size([5, 3])

In [37]:
y = torch.rand(5,3)
print(y)


 0.8818  0.1887  0.2476
 0.0571  0.8426  0.1903
 0.9361  0.9746  0.8263
 0.3841  0.9526  0.2364
 0.9489  0.0245  0.8846
[torch.FloatTensor of size 5x3]


In [38]:
x+y


Out[38]:
 1.4264  1.0904  0.6173
 0.3127  0.8908  0.4858
 1.4772  1.3437  1.2482
 0.4611  1.7075  0.6189
 1.3699  0.8346  1.4098
[torch.FloatTensor of size 5x3]

In [40]:
torch.add(x,y)


Out[40]:
 1.4264  1.0904  0.6173
 0.3127  0.8908  0.4858
 1.4772  1.3437  1.2482
 0.4611  1.7075  0.6189
 1.3699  0.8346  1.4098
[torch.FloatTensor of size 5x3]

In [41]:
x+y == torch.add(x,y)  # ele-wise


Out[41]:
 1  1  1
 1  1  1
 1  1  1
 1  1  1
 1  1  1
[torch.ByteTensor of size 5x3]

In [45]:
result = torch.Tensor(5,3)
torch.add(x,y,out=result)


Out[45]:
 1.4264  1.0904  0.6173
 0.3127  0.8908  0.4858
 1.4772  1.3437  1.2482
 0.4611  1.7075  0.6189
 1.3699  0.8346  1.4098
[torch.FloatTensor of size 5x3]

In [46]:
result


Out[46]:
 1.4264  1.0904  0.6173
 0.3127  0.8908  0.4858
 1.4772  1.3437  1.2482
 0.4611  1.7075  0.6189
 1.3699  0.8346  1.4098
[torch.FloatTensor of size 5x3]

In [47]:
x.add_(y)  # x will be change


Out[47]:
 1.4264  1.0904  0.6173
 0.3127  0.8908  0.4858
 1.4772  1.3437  1.2482
 0.4611  1.7075  0.6189
 1.3699  0.8346  1.4098
[torch.FloatTensor of size 5x3]

In [48]:
x.add_(y)


Out[48]:
 2.3082  1.2790  0.8649
 0.3697  1.7335  0.6761
 2.4133  2.3183  2.0745
 0.8452  2.6600  0.8553
 2.3187  0.8592  2.2944
[torch.FloatTensor of size 5x3]

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]:
 2.3082  1.2790  0.8649
[torch.FloatTensor of size 1x3]

In [53]:
x[:,1]


Out[53]:
 1.2790
 1.7335
 2.3183
 2.6600
 0.8592
[torch.FloatTensor of size 5]

In [54]:
x[1]


Out[54]:
 0.3697
 1.7335
 0.6761
[torch.FloatTensor of size 3]

In [57]:
a = torch.ones(5)
print(a)


 1
 1
 1
 1
 1
[torch.FloatTensor of size 5]


In [59]:
b = a.numpy()
print(b)


[ 1.  1.  1.  1.  1.]

In [60]:
a.add_(1)
print(a)
print(b)


 2
 2
 2
 2
 2
[torch.FloatTensor of size 5]

[ 2.  2.  2.  2.  2.]

In [62]:
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
print(a)
print(b)


[ 1.  1.  1.  1.  1.]

 1
 1
 1
 1
 1
[torch.DoubleTensor of size 5]


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)


111

 3.1900  1.4677  1.1126
 0.4268  2.5761  0.8663
 3.3493  3.2929  2.9008
 1.2292  3.6126  1.0917
 3.2676  0.8837  3.1791
[torch.cuda.FloatTensor of size 5x3 (GPU 0)]


In [73]:


In [ ]: