A torch.Tensor is a multi-dimensional matrix containing elements of a single data type.
张量(torch.Tensor)是包含单个数据类型元素的多维矩阵。
1、张量定义了如下八种CPU张量类型和八种GPU张量类型:
#CPU对应八种数据类型,GPU对应也有八种数据类型,如torch.cuda.FloatTensor([])
torch.FloatTensor([])
torch.DoubleTensor([])
torch.HalfTensor([])
torch.IntTensor([])
torch.LongTensor([])
torch.ShortTensor([])
torch.ByteTensor([])
torch.CharTensor([])
Data type CPU tensor GPU tensor
32-bit floating point torch.FloatTensor torch.cuda.FloatTensor
64-bit floating point torch.DoubleTensor torch.cuda.DoubleTensor
16-bit floating point torch.HalfTensor torch.cuda.HalfTensor
8-bit integer (unsigned) torch.ByteTensor torch.cuda.ByteTensor
8-bit integer (signed) torch.CharTensor torch.cuda.CharTensor
16-bit integer (signed) torch.ShortTensor torch.cuda.ShortTensor
32-bit integer (signed) torch.IntTensor torch.cuda.IntTensor
64-bit integer (signed) torch.LongTensor torch.cuda.LongTensor
2、数据的原地修改还是生成副本
#函数后面跟短下划线,表示就地修改 a=torch.Tensor([-1,-2]) b=torch.Tensor([-2,2]) print(‘a.abs() :‘,a.abs(),‘\n‘,‘a:‘,a) print(‘b.abs_() :‘,b.abs_(),‘\n‘,‘b:‘,b) # a.abs(): # 1 # 2 # [torch.FloatTensor of size 2] # # a: # -1 # -2 # [torch.FloatTensor of size 2] # # b.abs_(): # 2 # 2 # [torch.FloatTensor of size 2] # # b: # 2 # 2 # [torch.FloatTensor of size 2]