1. Convierta un solo dato a otros formatos de datos
1.1 Datos individuales -> lista
Realice operaciones de lista directamente en los datos o acceda directamente a una lista existente
gramática:
list_name = [single_data] 或者 list_name.append(single_data)
1.2 Datos individuales -> numpy.ndarray
Puede utilizar la sintaxis de inicialización directamente
gramática:
numpyarr_name = np.array(single_data)
Por ejemplo:
import numpy as np
single_data = 100
numpyarr_test = np.array(single_data)
print(numpyarr_test)
print(numpyarr_test.dtype)
print(numpyarr_test.shape)
1.3 Datos individuales -> torch.tensor
Puede utilizar la sintaxis de inicialización directamente
Sintaxis del método uno: (pero este método crea una dimensión de 0)
tensor_name = torch.tensor(single_data)
Sintaxis del método dos: crea un nuevo tensor a través de una lista
tensor_name = torch.Tensor([single_data])
Sintaxis del método tres: cree un nuevo tensor vacío y asígnelo (la nueva dimensión de este método es 1)
tensor_name = torch.empty(1)
tensor_name[0] = single_data
Por ejemplo:
import torch
single_data = 100
tensor_test = torch.tensor(single_data)
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
single_data = 100
tensor_test = torch.Tensor([single_data])
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
single_data = 100
tensor_test = torch.empty(1)
tensor_test[0] = single_data
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
En segundo lugar, la lista se convierte a otros formatos de datos.
2.1 lista -> datos individuales
Uso directo de la conversión de tipos coercitivos en Python
2.2 lista -> numpy.ndarray
Nota: Los tipos de la lista deben ser los mismos al convertir
gramática:
numpyarr_name = np.array(list_name)
Por ejemplo:
import numpy as np
list_test = [1,2,3,4]
print(type(list_test))
numpyarr_test = np.array(list_test)
print(type(numpyarr_test))
print(numpyarr_test.dtype)
print(numpyarr_test.shape)
2.3 lista -> torch.tensor
Nota: Los tipos de todos los elementos de la lista (incluidos los elementos de la lista multidimensional) deben ser los mismos durante la conversión.
gramática:
tensor_name = torch.Tensor(list_name)
Por ejemplo:
import torch
list_test = [[1,2,3],[4,5,6]]
print(type(list_test))
tensor_test = torch.Tensor(list_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
3. Convierta numpy.ndarray a otros formatos de datos
3.1 numpy.ndarry -> datos únicos
Uso directo de la conversión de tipos coercitivos en Python
Por ejemplo:
import numpy as np
single_data = float(np.array([100]))
print(single_data)
print(type(single_data))
3.2 numpy.ndarry -> lista
gramática:
numpyarr_name.tolist()
Por ejemplo:
import numpy as np
numpyarr_test = np.ones((1,2))
list_test = numpyarr_test.tolist()
print(type(list_test))
print(len(list_test))
print(len(list_test[0]))
3.3 numpy.ndarry -> torch.tensor
Nota 1: solo se puede convertir a tensor de CPU
Nota 2: la forma no cambiará
gramática:
tensor_name = torch.from_numpy(numpyarr_name)
Por ejemplo:
import torch
import numpy as np
numpyarr_test = np.ones((1,2))
print(type(numpyarr_test))
print(numpyarr_test.dtype)
tensor_test = torch.from_numpy(numpyarr_test)
print(type(tensor_test))
print(tensor_test.type())
Cuatro, torch.tensor se convierte a otros formatos de datos
4.1 torch.tensor -> datos individuales
Uso directo de la conversión de tipos coercitivos en Python
Por ejemplo:
import torch
tensor_test = torch.ones(1)
single_data = float(tensor_test)
print(single_data)
print(type(single_data))
4.2 torch.tensor -> lista
gramática:
list_name = tensor_name.tolist()
Por ejemplo:
import torch
tensor_test = torch.ones((1,3))
print(tensor_test)
print(type(tensor_test))
print(tensor_test.type())
print(tensor_test.shape)
list_test = tensor_test.tolist()
print(list_test)
print(type(list_test))
4.3 torch.tensor -> numpy.ndarry
Nota: solo los tensores de CPU pueden realizar dicha conversión
Nota: la forma no cambiará
gramática:
numpyarr_name = tensor_name.numpy()
# 直接使用不进行赋值的话不会改变类型
Por ejemplo:
import torch
import numpy as np
tensor_test = torch.Tensor(1,2)
print(type(tensor_test))
print(tensor_test.type())
numpyarr_test = tensor_test.numpy()
print(type(numpyarr_test))
print(numpyarr_test.dtype)
ÚLTIMO, referencias
Pytorch: construcción de tipo tensorial y conversión mutua_JNing-CSDNblog
Tipo de datos NumPy · TutorialsPoint NumPy Tutorial
Pytorch basic data types_torrent source -CSDN blog_pytorch tipos de datos
[numpy] Conversión entre ndarray y list _doufuxixi blog-blog de CSDN _ndarray to list
Lista de Python3, np.array, torch.tensor conversión mutua_black and white Dove Sani blog-CSDN blog
Conversión de Python entre matriz y list_Haiyang_Duan-CSDNblog
Numpy inicialización del análisis de datos de Python (1)
Conversión de tensor y otras estructuras de datos en pytorch (int, list, array)