· Understanding Numpy, Tensor and Variable in PyTorch

PyTorch: How to use GPU Acceleration (CONVERTING CPU to GPU data)

1. Problem description

We can use Numpy to manually write neural networks for back-propagation deep learning, but there are two problems,

1.Numpy is tedious to write neural network manually, with a large amount of code, which is not conducive to large-scale development; Numpy cannot use GPU to speed up calculation directly

I see a lot of people online saying PyTorch is very easy to use and more elegant and convenient than TensorFlow. I think one of the main reasons PyTorch is very similar to Numpy in that it handles data very simply. PyTorch also supports GPU acceleration, so PyTorch is a GPU version of Numpy.

PyTorch introduced Tensor for GPU acceleration and Variable for automatic differentiation. However, due to the introduction of these new concepts, some beginners who do not understand the use of some problems.

2. Problem analysis

We now know that PyTorch has introduced the Tensor for GPU acceleration, and Variable for automatic differentiation. We usually read data in Numpy Array mode. At TensorFlow, Numpy data will automatically translate into Tensor when it is put into the network. Generally, we don’t need to do explicit manipulation, but sometimes there will be an exception. In TensorFlow, Numpy and Tensor data translate into each other. But in PyTorch, we had to do the explicit manipulation ourselves.

Now I will explain how to convert them to each other through a network training process.

  • The first thing we’re going to do is read Numpy data, and we’re going to do Numpy2Tensor in order to put it into the network, we’re going to use the GPU to do acceleration, so Numpy2Tensor, because the network inputs and outputs are Variable, we’re going to need Tensor2Variable. In the process of training, we need to take out the value of loss. Since loss participates in BACKWARD (), loss at this time has become Variable, and we need to take out the Tensor when we take out loss. Similarly, if I want to take out the output of the network, because the input and output of the network are Variable, we need to do Variable2Tensor, and then if we want to show the loss, we need Tensor2Numpy.

So just to sum up, what we developers are really dealing with is Numpy data, Numpy2Tensor when you need to send it into the network, Tensor2Variable when you need to solve gradient information for some Tensor parameters. When you need to take data from Variable, you use Variable2Tensor. You need Tensor2Numpy to read the Tensor.

3. Transformation method

  • Numpy2Tensor: 1. Torch. From_numpy (Numpy_data) 2. Torch.
  • Tensor2Variable: 1. Variable(Tensor_data)
  • Variable2Tensor: 1. Variable_data.data()
  • Tensor2Numpy : 1.Tensor_data.numpy() 

Note that Numpy doesn’t translate to Variable directly, you have to have a Tensor.

Hope this helps