3-D tensors

When we add multiple matrices together, we get a 3-D tensor. 3-D tensors are used to represent data-like images. Images can be represented as numbers in a matrix, which are stacked together. An example of an image shape is 224, 224, 3, where the first index represents height, the second represents width, and the third represents a channel (RGB). Let's see how a computer sees a panda, using the next code snippet:

from PIL import Image# Read a panda image from disk using a library called PIL and convert it to numpy arraypanda = np.array(Image.open('panda.jpg').resize((224,224)))panda_tensor = torch.from_numpy(panda)panda_tensor.size()Output - torch.Size([224, 224, 3])#Display pandaplt.imshow(panda)

Since displaying the tensor of ...

Get Deep Learning with PyTorch now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.