16.03.2021 Views

Advanced Deep Learning with Keras

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

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

Chapter 2

optimizer='adam',

metrics=['accuracy'])

# train the model with input images and labels

model.fit(x_train,

y_train,

validation_data=(x_test, y_test),

epochs=20,

batch_size=batch_size)

# model accuracy on test dataset

score = model.evaluate(x_test, y_test, batch_size=batch_size)

print("\nTest accuracy: %.1f%%" % (100.0 * score[1]))

By default, MaxPooling2D uses pool_size=2, so the argument has been removed.

In the preceding listing every layer is a function of a tensor. They each generate

a tensor as an output which becomes the input to the next layer. To create this

model, we can call Model() and supply both the inputs and outputs tensors,

or alternatively the lists of tensors. Everything else remains the same.

The same listing can also be trained and evaluated using the fit() and evaluate()

functions, similar to the sequential model. The sequential class is, in fact, a subclass

of the Model class. We need to remember that we inserted the validation_data

argument in the fit() function to see the progress of validation accuracy during

training. The accuracy ranges from 99.3% to 99.4% in 20 epochs.

Creating a two-input and one-output model

We're now going to do something really exciting, creating an advanced model

with two inputs and one output. Before we start, it's important to know that this

is something that is not straightforward in the sequential model.

Let's suppose a new model for the MNIST digit classification is invented, and it's

called the Y-Network, as shown in Figure 2.1.1. The Y-Network uses the same input

twice, both on the left and right CNN branches. The network combines the results

using concatenate layer. The merge operation concatenate is similar to stacking

two tensors of the same shape along the concatenation axis to form one tensor. For

example, concatenating two tensors of shape (3, 3, 16) along the last axis will result

in a tensor of shape (3, 3, 32).

[ 43 ]

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

Saved successfully!

Ooh no, something went wrong!