In [1]:
import torch
import numpy as np
In [2]:
v = torch.tensor([1,2,3,4,5,6])
print(v.dtype)
print(v)
print(v[1:4])
print(v[1:])
torch.int64
tensor([1, 2, 3, 4, 5, 6])
tensor([2, 3, 4])
tensor([2, 3, 4, 5, 6])
In [3]:
f = torch.FloatTensor([1,2,3,4,5,6])
print(f.dtype)
print(f)
print(f.size())
torch.float32
tensor([1., 2., 3., 4., 5., 6.])
torch.Size([6])
In [4]:
v.view(6,1) # reshape
Out[4]:
tensor([[1],
        [2],
        [3],
        [4],
        [5],
        [6]])
In [5]:
v.view(3,2) # reshape
Out[5]:
tensor([[1, 2],
        [3, 4],
        [5, 6]])
In [6]:
v.view(3,-1) # reshape
Out[6]:
tensor([[1, 2],
        [3, 4],
        [5, 6]])
In [7]:
a = np.array([1,2,3,4,5])
print(a)
tensor_cnv = torch.from_numpy(a)
print(tensor_cnv)
print(tensor_cnv.type())
[1 2 3 4 5]
tensor([1, 2, 3, 4, 5])
torch.LongTensor
In [8]:
numpy_cnv = tensor_cnv.numpy()
print(numpy_cnv)
[1 2 3 4 5]