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.

Convolutional Neural Networks

Our net will learn 32 convolutional filters, each with a 3×3 size. The output dimension

is the same one as the input shape, so it will be 32×32 and the activation function used

is a ReLU function, which is a simple way of introducing non-linearity. After that we

have a max pooling operation with pool size 2×2 and a Dropout of 25%:

# define the convnet

def build(input_shape, classes):

model = models.Sequential()

model.add(layers.Convolution2D(32, (3, 3), activation='relu',

input_shape=input_shape))

model.add(layers.MaxPooling2D(pool_size=(2, 2)))

model.add(layers.Dropout(0.25))

The next stage in the deep pipeline is a dense network with 512 units and ReLU

activation followed by a dropout at 50% and by a softmax layer with 10 classes

as output, one for each category:

model.add(layers.Flatten())

model.add(layers.Dense(512, activation='relu'))

model.add(layers.Dropout(0.5))

model.add(layers.Dense(classes, activation='softmax'))

return model

After defining the network, we can train the model. In this case, we split the data

and compute a validation set in addition to the training and testing sets. The training

is used to build our models, the validation is used to select the best performing

approach, while the test set is used to check the performance of our best models

on fresh, unseen data:

# use TensorBoard, princess Aurora!

callbacks = [

# Write TensorBoard logs to './logs' directory

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

]

# train

model.compile(loss='categorical_crossentropy', optimizer=OPTIM,

metrics=['accuracy'])

model.fit(X_train, y_train, batch_size=BATCH_SIZE,

epochs=EPOCHS, validation_split=VALIDATION_SPLIT,

verbose=VERBOSE, callbacks=callbacks)

score = model.evaluate(X_test, y_test,

batch_size=BATCH_SIZE, verbose=VERBOSE)

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

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

[ 124 ]

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

Saved successfully!

Ooh no, something went wrong!