![Deep Learning with PyTorch](https://wfqqreader-1252317822.image.myqcloud.com/cover/214/36700214/b_36700214.jpg)
上QQ阅读APP看书,第一时间看更新
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 array
panda = 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 panda
plt.imshow(panda)
Since displaying the tensor of size 224, 224, 3 would occupy a couple of pages in the book, we will display the image and learn to slice the image into smaller tensors to visualize it:
![](https://epubservercos.yuewen.com/DD5357/19470397608907106/epubprivate/OEBPS/Images/b5de7e6a-4c8a-4aed-91da-5d4180b3f9f3.png?sign=1738870488-Jx1v37wTLPabJ3v37qfFWiWd2fiw3VTl-0-82b48a3ee2512697e90d4a47a15d42c1)
Displaying the image