Tensor Manipulation
Explore the fundamentals of tensor manipulation in PyTorch as a foundation for building neural networks. Understand tensor creation, conversion between NumPy arrays and tensors, tensor operations, and how tensors support backpropagation. Learn also how to convert images to tensors for computer vision applications.
In the coming lessons, we’ll explore neural networks with the PyTorch library. Images are a form of data easily consumable by a neural network. Therefore, in many situations, neural networks can help us in automated inspection.
Before diving into the internal operators of a neural network, we must take a closer look at the data format that each component will process as input and output: the tensor.
What is a tensor?
A tensor is a multidimensional array.
Note: If you have used the NumPy module, you’ve probably manipulated
numpy.arrayobjects, which are tensors.
You can create a numpy.array object and convert it to a torch.Tensor object with the function torch.from_numpy().
In line 5, we create a torch.Tensor object from a numpy.array object.
Conversely, we can create a numpy.array object from a tensor with the Tensor.numpy() method.
In line 5, we create a random tensor sampled from a normal distribution