pdfcoffee

soumyasankar99
from soumyasankar99 More from this publisher
09.05.2023 Views

Chapter 4X_train, X_test = X_train / 255.0, X_test / 255.0# castX_train = X_train.astype('float32')X_test = X_test.astype('float32')# convert class vectors to binary class matricesy_train = tf.keras.utils.to_categorical(y_train, NB_CLASSES)y_test = tf.keras.utils.to_categorical(y_test, NB_CLASSES)# initialize the optimizer and modelmodel = build(input_shape=INPUT_SHAPE, classes=NB_CLASSES)model.compile(loss="categorical_crossentropy", optimizer=OPTIMIZER,metrics=["accuracy"])model.summary()# use TensorBoard, princess Aurora!callbacks = [# Write TensorBoard logs to './logs' directorytf.keras.callbacks.TensorBoard(log_dir='./logs')]# fithistory = model.fit(X_train, y_train,batch_size=BATCH_SIZE, epochs=EPOCHS,verbose=VERBOSE, validation_split=VALIDATION_SPLIT,callbacks=callbacks)score = model.evaluate(X_test, y_test, verbose=VERBOSE)print("\nTest score:", score[0])print('Test accuracy:', score[1])Now let's run the code. As you can see in Figure 6, the time had a significant increaseand each iteration in our DNN now takes ~28 seconds against ~1-2 seconds for thenet defined in Chapter 1, Neural Network Foundations with TensorFlow 2.0.[ 117 ]

Convolutional Neural NetworksHowever, the accuracy reached a new peak at 99.991 on training, 99.91 on validation,and 99.15 on test%!Figure 6: LeNet accuracyLet's see the execution of a full run for 20 epochs:[ 118 ]

Chapter 4

X_train, X_test = X_train / 255.0, X_test / 255.0

# cast

X_train = X_train.astype('float32')

X_test = X_test.astype('float32')

# convert class vectors to binary class matrices

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

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

# initialize the optimizer and model

model = build(input_shape=INPUT_SHAPE, classes=NB_CLASSES)

model.compile(loss="categorical_crossentropy", optimizer=OPTIMIZER,

metrics=["accuracy"])

model.summary()

# use TensorBoard, princess Aurora!

callbacks = [

# Write TensorBoard logs to './logs' directory

tf.keras.callbacks.TensorBoard(log_dir='./logs')

]

# fit

history = model.fit(X_train, y_train,

batch_size=BATCH_SIZE, epochs=EPOCHS,

verbose=VERBOSE, validation_split=VALIDATION_SPLIT,

callbacks=callbacks)

score = model.evaluate(X_test, y_test, verbose=VERBOSE)

print("\nTest score:", score[0])

print('Test accuracy:', score[1])

Now let's run the code. As you can see in Figure 6, the time had a significant increase

and each iteration in our DNN now takes ~28 seconds against ~1-2 seconds for the

net defined in Chapter 1, Neural Network Foundations with TensorFlow 2.0.

[ 117 ]

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

Saved successfully!

Ooh no, something went wrong!