除第一条 tensor 为内置函数创建方法,其余均为内置类创建方法
import torch
t = [ [1, 2, 3], [4, 5, 6] ]
x1 = torch.tensor(t)
print(x1, x1.dtype)
'''
tensor([[1, 2, 3],
[4, 5, 6]]) torch.int64
'''
x2 = torch.Tensor(t)
print(x2, x2.dtype)
'''
tensor([[1., 2., 3.],
[4., 5., 6.]]) torch.float32
'''
x3 = torch.IntTensor(t)
print(x3, x3.dtype)
'''
tensor([[1, 2, 3],
[4, 5, 6]], dtype=torch.int32) torch.int32
'''
x4 = torch.FloatTensor(t)
print(x4, x4.dtype)
'''
tensor([[1., 2., 3.],
[4., 5., 6.]]) torch.float32
'''
x5 = torch.CharTensor(t)
print(x5, x5.dtype)
'''
tensor([[1, 2, 3],
[4, 5, 6]], dtype=torch.int8) torch.int8
'''
x6 = torch.BoolTensor(t)
print(x6, x6.dtype)
'''
tensor([[True, True, True],
[True, True, True]]) torch.bool
'''
x7 = torch.ByteTensor(t)
print(x7, x7.dtype)
'''
tensor([[1, 2, 3],
[4, 5, 6]], dtype=torch.uint8) torch.uint8
'''
x8 = torch.LongTensor(t)
print(x8, x8.dtype)
'''
tensor([[1, 2, 3],
[4, 5, 6]]) torch.int64
'''
x9 = torch.ShortTensor(t)
print(x9, x9.dtype)
'''
tensor([[1, 2, 3],
[4, 5, 6]], dtype=torch.int16) torch.int16
'''
暑期编程PK赛
得CSDN机械键盘等精美礼品!