Tensor
MNIST Dataset:
Images as Tensors: Each handwritten digit image in MNIST is a 28x28 pixel grayscale image. It's represented as a 2D NumPy array (a type of tensor), with each element holding a pixel's intensity value (0-255).
Batches as 3D Tensors: Neural networks are often trained on batches of images simultaneously. To accommodate this, a batch of MNIST images forms a 3D tensor:
Dimension 1: Number of images in the batch (e.g., 32)
Dimension 2: Image height (28 pixels)
Dimension 3: Image width (28 pixels)
Exercise : in Colab
import numpy as np
from keras.datasets import mnist
import matplotlib.pyplot as plt
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
print(f"3D tensor: Dimentions {train_images.ndim} Shape {train_images.shape} Dtype {train_images.dtype} ")
single_image = train_images[0]
print(f"2D tensor {single_image.shape}")
plt.imshow(single_image, cmap='gray')
plt.title('MNIST Sample Image')
plt.colorbar()
plt.show()
print(f"print a pixel {single_image[14,14]} ")
Loss Function