torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])
Out[111]:
tensor([[0.1000, 1.2000],
[2.2000, 3.1000],
[4.9000, 5.2000]])
torch.tensor([0, 1])
Out[112]: tensor([0, 1])
torch.tensor([[0.11111, 0.222222, 0.3333333]],
dtype=torch.float64, device=torch.device('cpu'))
Out[113]: tensor([[0.1111, 0.2222, 0.3333]], dtype=torch.float64)
torch.tensor(3.14159)
Out[114]: tensor(3.1416)
torch.tensor([])
Out[115]: tensor([])
torch.tensor([[0.11111, 0.222222, 0.3333333]],
dtype=torch.float64, device=torch.device('cpu'), requires_grad=True, pin_memory=False)
Out[117]: tensor([[0.1111, 0.2222, 0.3333]], dtype=torch.float64, requires_grad=True)
>>> a = numpy.array([1, 2, 3])
>>> t = torch.as_tensor(a)
>>> t
tensor([1, 2, 3])
>>> t[0] = -1
>>> a
array([-1, 2, 3])
>>> a = numpy.array([1, 2, 3])
>>> t = torch.as_tensor(a, device=torch.device('cuda'))
>>> t[0] = -1
>>> a
array([1, 2, 3])
t = torch.as_tensor([2, 2, 2], device=torch.device('cuda'))
>>> t
tensor([2, 2, 2], device='cuda:0')
a = numpy.array([1, 2, 3])
t = torch.from_numpy(a)
t
Out[38]: tensor([1, 2, 3])
t[0] = -1
a
Out[40]: array([-1, 2, 3])
torch.zeros(2, 3)
Out[41]:
tensor([[0., 0., 0.],
[0., 0., 0.]])
torch.zeros(5)
Out[42]: tensor([0., 0., 0., 0., 0.])
torch.ones(2, 3)
Out[43]:
tensor([[1., 1., 1.],
[1., 1., 1.]])
torch.ones(5)
Out[44]: tensor([1., 1., 1., 1., 1.])
参数列表:
torch.arange(5)
Out[45]: tensor([0, 1, 2, 3, 4])
torch.arange(1, 4)
Out[46]: tensor([1, 2, 3])
torch.arange(1, 2.5, 0.5)
Out[47]: tensor([1.0000, 1.5000, 2.0000])
torch.linspace(3, 10, steps=5)
Out[49]: tensor([ 3.0000, 4.7500, 6.5000, 8.2500, 10.0000])
torch.linspace(-10, 10, steps=5)
Out[50]: tensor([-10., -5., 0., 5., 10.])
torch.linspace(start=-10, end=10, steps=1)
Out[51]: tensor([-10.])
torch.eye(3)
Out[58]:
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
torch.empty((2,3), dtype=torch.int64)
Out[59]:
tensor([[0, 0, 0],
[0, 0, 2]])
torch.full((2, 3), 3.141592)
Out[67]:
tensor([[3.1416, 3.1416, 3.1416],
[3.1416, 3.1416, 3.1416]])
暂时完结。。。