pdfcoffee

soumyasankar99
from soumyasankar99 More from this publisher
09.05.2023 Views

Chapter 1model.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 byTensorFlow 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 updateweights 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 ofoptimization is defined as a process of loss minimization). Third, we need to evaluatethe 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 thetrue values. Mathematically, if d is a vector of predictions and y is the vectornnof n observed values, then MMMMMM = 1 ∑(dd − yy)2nn . Note that this objective functionii=1is the average of all the mistakes made in each prediction. If a prediction isfar off from the true value, then this distance is made more evident by thesquaring operation. In addition, the square can add up the error regardlessof whether a given value is positive or negative.• binary_crossentropy, which defines the binary logarithmic loss. Supposethat our model predicts p while the target is c, then the binary cross-entropyis defined as LL(pp, cc) = −cc ln(pp) − (1 − cc) ln(1 − pp) . Note that this objectivefunction is suitable for binary label prediction.• categorical_crossentropy, which defines the multiclass logarithmicloss. Categorical cross-entropy compares the distribution of the predictionswith the true distribution, with the probability of the true class set to 1 and0 for the other classes. If the true class is c and the prediction is y, then thecategorical cross-entropy is defined as:LL(cc, pp) = − ∑ CC ii ln(pp ii )ii[ 17 ]

Neural Network Foundations with TensorFlow 2.0One way to think about multi-class logarithm loss is to consider the trueclass represented as a one-hot encoded vector, and the closer the model'soutputs are to that vector, the lower the loss. Note that this objective functionis suitable for multi-class label predictions. It is also the default choice inassociation with softmax activation.A complete list of loss functions can be found at https://www.tensorflow.org/api_docs/python/tf/keras/losses.Some common choices for metrics are:• Accuracy, which defines the proportion of correct predictions with respect tothe targets• Precision, which defines how many selected items are relevant for a multilabelclassification• Recall, which defines how many selected items are relevant for a multi-labelclassificationA complete list of metrics can be found at https://www.tensorflow.org/api_docs/python/tf/keras/metrics.Metrics are similar to objective functions, with the only difference that they arenot used for training a model, but only for evaluating the model. However, it isimportant to understand the difference between metrics and objective functions.As discussed, the loss function is used to optimize your network. This is thefunction minimized by the selected optimizer. Instead, a metric is used to judge theperformance of your network. This is only for you to run an evaluation on and itshould be separated from the optimization process. On some occasions, it wouldbe ideal to directly optimize for a specific metric. However, some metrics are notdifferentiable with respect to their inputs, which precludes them from being useddirectly.When compiling a model in TensorFlow 2.0, it is possible to select the optimizer,the loss function, and the metric used together with a given model:# Compiling the model.model.compile(optimizer='SGD',loss='categorical_crossentropy',metrics=['accuracy'])[ 18 ]

Neural Network Foundations with TensorFlow 2.0

One way to think about multi-class logarithm loss is to consider the true

class represented as a one-hot encoded vector, and the closer the model's

outputs are to that vector, the lower the loss. Note that this objective function

is suitable for multi-class label predictions. It is also the default choice in

association with softmax activation.

A complete list of loss functions can be found at https://www.

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

Some common choices for metrics are:

• Accuracy, which defines the proportion of correct predictions with respect to

the targets

• Precision, which defines how many selected items are relevant for a multilabel

classification

• Recall, which defines how many selected items are relevant for a multi-label

classification

A complete list of metrics can be found at https://www.

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

Metrics are similar to objective functions, with the only difference that they are

not used for training a model, but only for evaluating the model. However, it is

important to understand the difference between metrics and objective functions.

As discussed, the loss function is used to optimize your network. This is the

function minimized by the selected optimizer. Instead, a metric is used to judge the

performance of your network. This is only for you to run an evaluation on and it

should be separated from the optimization process. On some occasions, it would

be ideal to directly optimize for a specific metric. However, some metrics are not

differentiable with respect to their inputs, which precludes them from being used

directly.

When compiling a model in TensorFlow 2.0, it is possible to select the optimizer,

the loss function, and the metric used together with a given model:

# Compiling the model.

model.compile(optimizer='SGD',

loss='categorical_crossentropy',

metrics=['accuracy'])

[ 18 ]

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

Saved successfully!

Ooh no, something went wrong!