Notas del estudio de Pytorch: resumen de la conversión de tipos de datos

1. Interconversión entre Tensor y Numpy

tensor转numpy
a = torch.ones([2,5])
b = a.numpy()

numpy转tensor
a = np.ones([2,5])
b = torch.from_numpy(a)

2. Conversión mutua entre tensor y lista

tensor转list
a = torch.ones([1,5])
b = a.tolist()

list转tensor
a = list(range(1,6))
b = torch.tensor(a)

3. Conversión de tipos de datos comunes entre Tensor y Tensor

tensor所有的数据类型:
torch.FloatTensor:32位浮点型
torch.DoubleTensor:64位浮点型
torch.ShortTensor:16位整型
torch.IntTensor:32位整型
torch.LongTensor:64位整型

a = torch.tensor(3.1415)
int_t = a.int()
short_t = a.short()
long_t = a.long()

double_t = a.double()

char_t = a.char()
byte_t = a.byte()

4.type_as () convierte el tensor en un tensor del tipo especificado

a = torch.Tensor(2,5)
b = torch.IntTensor(1,2)
a.type_as(b)

Supongo que te gusta

Origin blog.csdn.net/m0_45388819/article/details/109429653
Recomendado
Clasificación