125. Converting Arrays To Tensors

There are several ways to convert arrays to torch tensors.

import torch
import numpy as np
data = np.array([1,2,3,4,5,6])

option1 = torch.Tensor(arr)
option2 = torch.tensor(arr)
option3 = torch.as_tensor(arr)
option4 = torch.from_numpy(arr)
Option Description
1 Make a Copy and convert array to torch Tensor’s default data type (float.32)
2 Make a Copy and inherit original array’s data type
3 Share the same memory allocation with the original array, and inherit original array’s data type
4 Share the same memory allocation with the original array, and inherit original array’s data type. BUT, only works for numpy arrays