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

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

input_shape=(RESHAPED,),

name='dense_layer',

activation='softmax'))

Once we define the model, we have to compile it so that it can be executed by

TensorFlow 2.0. There are a few choices to be made during compilation. Firstly,

we need to select an optimizer, which is the specific algorithm used to update

weights while we train our model. Second, we need to select an objective function,

which is used by the optimizer to navigate the space of weights (frequently,

objective functions are called either loss functions or cost functions and the process of

optimization is defined as a process of loss minimization). Third, we need to evaluate

the trained model.

A complete list of optimizers can be found at https://

www.tensorflow.org/api_docs/python/tf/keras/

optimizers.

Some common choices for objective functions are:

• MSE, which defines the mean squared error between the predictions and the

true values. Mathematically, if d is a vector of predictions and y is the vector

nn

of n observed values, then MMMMMM = 1 ∑(dd − yy)2

nn . Note that this objective function

ii=1

is the average of all the mistakes made in each prediction. If a prediction is

far off from the true value, then this distance is made more evident by the

squaring operation. In addition, the square can add up the error regardless

of whether a given value is positive or negative.

• binary_crossentropy, which defines the binary logarithmic loss. Suppose

that our model predicts p while the target is c, then the binary cross-entropy

is defined as LL(pp, cc) = −cc ln(pp) − (1 − cc) ln(1 − pp) . Note that this objective

function is suitable for binary label prediction.

• categorical_crossentropy, which defines the multiclass logarithmic

loss. Categorical cross-entropy compares the distribution of the predictions

with the true distribution, with the probability of the true class set to 1 and

0 for the other classes. If the true class is c and the prediction is y, then the

categorical cross-entropy is defined as:

LL(cc, pp) = − ∑ CC ii ln(pp ii )

ii

[ 17 ]

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

Saved successfully!

Ooh no, something went wrong!