目录

1 通用,简单,CPU/GPU tensor 数据类型转换

2 tensor.type()方法

CPU tensor 数据类型转换

GPU tensor 数据类型转换

3 tensor.to() 方法,CPU/GPU tensor 数据类型转换


1 通用,简单, CPU/GPU tensor 数据类型转换

tensor.double():把一个张量tensor转为torch.float64 数据类型

tensor.float():把一个张量tensor转为torch.float32 数据类型

tensor.int():把一个张量tensor转为torch.int32 数据类型

tensor.long(): 把一个张量tensor转为torch.int64 数据类型

2 tensor.type()方法

CPU tensor 数据类型转换

tensor.type(torch.DoubleTensor) 或者 tensor.type(‘torch.DoubleTensor’)

tensor.type(torch.FloatTensor) 或者 tensor.type('torch.FloatTensor')

tensor.type(torch.LongTensor) 或者 tensor.type('torch.LongTensor')

GPU tensor 数据类型转换

tensor.type(torch.cuda.DoubleTensor) 或者 tensor.type(‘torch.cuda.DoubleTensor’)

tensor.type(torch.cuda.FloatTensor) 或者 tensor.type('torch.cuda.FloatTensor')

tensor.type(torch.cuda.LongTensor) 或者 tensor.type('torch.cuda.LongTensor')

3 tensor.to() 方法, CPU/GPU tensor 数据类型转换

tensor.to(dtype=torch.float64)

tensor.to(dtype=torch.float32)

tensor.to(dtype=torch.int8)

tensor.to(dtype=torch.long)

Pytorch tensor 数据类型快速转换三种方法-LMLPHP

11-07 05:31