09.05.2023 Views

pdfcoffee

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 1

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

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

# Building 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.Dropout(DROPOUT))

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

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

model.add(keras.layers.Dropout(DROPOUT))

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)

Let's run the code for 200 iterations as before, and we'll see that this net achieves an

accuracy of 91.70% on training, 94.42% on validation, and 94.15% on testing:

Figure 17: Further testing of the neutal network, with accuracies shown

[ 25 ]

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

Saved successfully!

Ooh no, something went wrong!