Python
: list
NumPy
: array (ndarray)
PyTorch, TensorFlow
: tensor
list <-> numpy array
list to numpy array
import numpy as np
list1 = [1, 2, 3]
print(list1)
array1 = np.array(list1)
print(array1)
numpy array to list
import numpy as np
array1 = np.array([[1, 2], [3, 4]])
print(array1)
list1 = array1.tolist()
print(list1)
numpy array <-> tensor
numpy array to tensor
import numpy as np
import torch
array1 = np.array([[1, 2], [3, 4]])
print(array1)
tensor1 = torch.tensor(array1)
tensor2 = torch.as_tensor(array1)
tensor3 = torch.from_numpy(array1)
print(tensor1)
tensor to numpy array
import torch
tensor1 = torch.randn(4)
print(tensor1)
array1 = tensor1.numpy()
print(array1)
list <-> tensor
list to tensor
import torch
list1 = [1, 2, 3]
print(list1)
tensor1 = torch.tensor(list1)
tensor2 = torch.as_tensor(list1)
print(tensor1)
tensor to list
import torch
tensor1 = torch.randn(4)
print(tensor1)
list1 = tensor1.tolist()
print(list1)
<참고>
https://technical-support.tistory.com/48