In [4]: import torch torch.__version__ x=torch.empty(5,3) x
Out[4]:
tensor([[0.0000e+00, 0.0000e+00, 4.6430e-23], [1.4013e-45, 1.2612e-44, 0.0000e+00], [3.5733e-43, 0.0000e+00, 0.0000e+00], [0.0000e+00, 0.0000e+00, 0.0000e+00], [0.0000e+00, 0.0000e+00, 0.0000e+00]])
In [5]:
x=torch.rand(5,3) x
Out[5]:
tensor([[0.8045, 0.6600, 0.5920], [0.9726, 0.2459, 0.5417], [0.5958, 0.6286, 0.5736], [0.5969, 0.0276, 0.8971], [0.9583, 0.4394, 0.5928]])#tensor(张量)几维矩阵都行
In [20]:
x=torch.zeros(5,3)#x=torch.zeros(5,4,dtype=torch.int) x
Out[20]:
tensor([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])
In [18]:
y=torch.tensor([5,4]) y
Out[18]:
tensor([5, 4])
In [21]:
x.size()#当前的维度是几行几列的
Out[21]:
torch.Size([5, 3])
In [25]:
y=torch.rand(5,3) x=torch.rand(5,3) x+y
Out[25]:
tensor([[0.8520, 0.6184, 1.2141], [1.8745, 1.0329, 1.1968], [0.9743, 0.5262, 1.4275], [0.5415, 1.0113, 1.2635], [0.9762, 0.7496, 1.4369]])
In [26]:
torch.add(x,y)
Out[26]:
tensor([[0.8520, 0.6184, 1.2141], [1.8745, 1.0329, 1.1968], [0.9743, 0.5262, 1.4275], [0.5415, 1.0113, 1.2635], [0.9762, 0.7496, 1.4369]])
In [27]:
x[:1]
Out[27]:
tensor([[0.0229, 0.1664, 0.5243]])
In [28]:
x=torch.rand(4,4) y=x.view(16) z=x.view(-1,8)#-1代表自动填充 print(x.size(),y.size(),z.size())
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
In [30]:
a=torch.ones(5) b=a.numpy() b
Out[30]:
array([1., 1., 1., 1., 1.], dtype=float32)
In [29]:
import numpy as np a=np.ones(5) b=torch.from_numpy(a) b
Out[29]:
tensor([1., 1., 1., 1., 1.], dtype=torch.float64)