Loading Data, Devices, and CUDA
Learn about the different PyTorch built-in functions used in converting Numpy to PyTorch and vice versa, along with exploring GPU tensors.
Conversions between Numpy and PyTorch
It is time to start converting our Numpy code to PyTorch. We will start with the training data; that is, our x_train
and y_train
arrays.
The “as_tensor
” method
”How do we go from Numpy’s arrays to PyTorch’s tensors?”
The as_tensor
method preserves the type of the array, which can also be seen in the code below:
# Using as_tensor function to convert Numpy array to PyTorch tensorx_train_tensor = torch.as_tensor(x_train)print(x_train.dtype, x_train_tensor.dtype)
You can also easily cast it to a different type like a lower precision (32-bit) float, which will occupy less space in memory using the float()
method:
# Using float function to change data typesx_train_tensor = torch.as_tensor(x_train)float_tensor = x_train_tensor.float() # (going from float64 to float32)print(float_tensor.dtype)
IMPORTANT: Both
as_tensor()
andfrom_numpy()
return a tensor that shares the underlying data with the original Numpy array. Similar to what happened when we usedview()
in the previous lesson, if you modify the original Numpy array, you are modifying the corresponding PyTorch tensor too and vice-versa. ... ...