PyTorch tensors & NumPy study notes

Numpy is an important package for constructing matrices in python, so how to convert the data in numpy and 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 

One thing to note is that the converted data format is always float32 

For data conversion between NumPy and Tensor, more information can be found at: 

Warm-up: numpy — PyTorch Tutorials 2.0.1+cu117 documentation

Guess you like

Origin blog.csdn.net/weixin_44897685/article/details/130818415