Numpy是python中用于构建矩阵的重要的package,那么如何转化numpy和tensor中的数据呢?
#Numpy array to tensor
import torch
import numpy as np
array= np.arrange(1.0,8.0)
tensor = torch.form_numpy(array) #这个方法是可以将numpy格式的数据转化为tensor的数据格式
array, tensor
# Tensor to Numpy array
tensor = torch.ones(7)
numpy_tensor = tensor.numpy()
tensor, numpy_tensor
其中要注意的一点是,转化出来的数据格式总是float32
对于NumPy和Tensor的数据转化, 更多的信息在网址上面可以找到:
Warm-up: numpy — PyTorch Tutorials 2.0.1+cu117 documentation