09.05.2023 Views

pdfcoffee

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Neural Network Foundations with TensorFlow 2.0

# Loading MNIST dataset.

# verify

# You can verify that the split between train and test is 60,000, and

10,000 respectively.

# Labels have one-hot representation.is automatically applied

mnist = keras.datasets.mnist

(X_train, Y_train), (X_test, Y_test) = mnist.load_data()

# X_train is 60000 rows of 28x28 values; we --> reshape it to

# 60000 x 784.

RESHAPED = 784

#

X_train = X_train.reshape(60000, RESHAPED)

X_test = X_test.reshape(10000, RESHAPED)

X_train = X_train.astype('float32')

X_test = X_test.astype('float32')

# Normalize inputs to be within in [0, 1].

X_train /= 255

X_test /= 255

print(X_train.shape[0], 'train samples')

print(X_test.shape[0], 'test samples')

# One-hot representation of the labels.

Y_train = tf.keras.utils.to_categorical(Y_train, NB_CLASSES)

Y_test = tf.keras.utils.to_categorical(Y_test, NB_CLASSES)

You can see from the above code that the input layer has a neuron associated to

each pixel in the image for a total of 28*28=784 neurons, one for each pixel in the

MNIST images.

Typically, the values associated with each pixel are normalized in the range [0,1]

(which means that the intensity of each pixel is divided by 255, the maximum

intensity value). The output can be one of ten classes, with one class for each digit.

The final layer is a single neuron with activation function "softmax", which is a

generalization of the sigmoid function. As discussed earlier, a sigmoid function

output is in the range (0, 1) when the input varies in the range (−∞, ∞) . Similarly,

a softmax "squashes" a K-dimensional vector of arbitrary real values into a

K-dimensional vector of real values in the range (0, 1), so that they all add up to 1.

In our case, it aggregates 10 answers provided by the previous layer with 10 neurons.

What we have just described is implemented with the following code:

# Build the model.

model = tf.keras.models.Sequential()

[ 16 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!