pdfcoffee

soumyasankar99
from soumyasankar99 More from this publisher
09.05.2023 Views

[ 21 ]Chapter 1Improving the simple net in TensorFlow 2.0with hidden layersOkay, we have a baseline of accuracy of 89.96% on training, 90.70% on validation,and 90.71% on test. It is a good starting point, but we can improve it. Let's see how.An initial improvement is to add additional layers to our network because theseadditional neurons might intuitively help it to learn more complex patterns in thetraining data. In other words, additional layers add more parameters, potentiallyallowing a model to memorize more complex patterns. So, after the input layer, wehave a first dense layer with N_HIDDEN neurons and an activation function "ReLU."This additional layer is considered hidden because it is not directly connected eitherwith the input or with the output. After the first hidden layer, we have a secondhidden layer again with N_HIDDEN neurons followed by an output layer with 10neurons, each one of which will fire when the relative digit is recognized. Thefollowing code defines this new network:import tensorflow as tffrom tensorflow import keras# Network and training.EPOCHS = 50BATCH_SIZE = 128VERBOSE = 1NB_CLASSES = 10 # number of outputs = number of digitsN_HIDDEN = 128VALIDATION_SPLIT = 0.2 # how much TRAIN is reserved for VALIDATION# Loading MNIST dataset.# Labels have one-hot representation.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, X_test = X_train / 255.0, X_test / 255.0print(X_train.shape[0], 'train samples')

Neural Network Foundations with TensorFlow 2.0print(X_test.shape[0], 'test samples')# Labels have one-hot representation.Y_train = tf.keras.utils.to_categorical(Y_train, NB_CLASSES)Y_test = tf.keras.utils.to_categorical(Y_test, NB_CLASSES)# Build the model.model = tf.keras.models.Sequential()model.add(keras.layers.Dense(N_HIDDEN,input_shape=(RESHAPED,),name='dense_layer', activation='relu'))model.add(keras.layers.Dense(N_HIDDEN,name='dense_layer_2', activation='relu'))model.add(keras.layers.Dense(NB_CLASSES,name='dense_layer_3', activation='softmax'))# Summary of the model.model.summary()# Compiling the model.model.compile(optimizer='SGD',loss='categorical_crossentropy',metrics=['accuracy'])# Training the model.model.fit(X_train, Y_train,batch_size=BATCH_SIZE, epochs=EPOCHS,verbose=VERBOSE, validation_split=VALIDATION_SPLIT)# Evaluating the model.test_loss, test_acc = model.evaluate(X_test, Y_test)print('Test accuracy:', test_acc)Note that to_categorical(Y_train, NB_CLASSES) converts the array Y_train intoa matrix with as many columns as there are classes. The number of rows stays thesame. So, for instance if we have:then:> labelsarray([0, 2, 1, 2, 0])to_categorical(labels)array([[ 1., 0., 0.],[ 0., 0., 1.],[ 22 ]

Neural Network Foundations with TensorFlow 2.0

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

# Labels have one-hot representation.

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

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

# Build the model.

model = tf.keras.models.Sequential()

model.add(keras.layers.Dense(N_HIDDEN,

input_shape=(RESHAPED,),

name='dense_layer', activation='relu'))

model.add(keras.layers.Dense(N_HIDDEN,

name='dense_layer_2', activation='relu'))

model.add(keras.layers.Dense(NB_CLASSES,

name='dense_layer_3', activation='softmax'))

# Summary of the model.

model.summary()

# Compiling the model.

model.compile(optimizer='SGD',

loss='categorical_crossentropy',

metrics=['accuracy'])

# Training the model.

model.fit(X_train, Y_train,

batch_size=BATCH_SIZE, epochs=EPOCHS,

verbose=VERBOSE, validation_split=VALIDATION_SPLIT)

# Evaluating the model.

test_loss, test_acc = model.evaluate(X_test, Y_test)

print('Test accuracy:', test_acc)

Note that to_categorical(Y_train, NB_CLASSES) converts the array Y_train into

a matrix with as many columns as there are classes. The number of rows stays the

same. So, for instance if we have:

then:

> labels

array([0, 2, 1, 2, 0])

to_categorical(labels)

array([[ 1., 0., 0.],

[ 0., 0., 1.],

[ 22 ]

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

Saved successfully!

Ooh no, something went wrong!