How can Tensorflow be used to train and compile the augmented model?

TensorflowServer Side ProgrammingProgramming

The augmented model can be compiled using the ‘compile’ method, which also takes ‘SparseCategoricalCrossentropy’ as parameter to calculate the loss associated with training.

Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?

We will use the Keras Sequential API, which is helpful in building a sequential model that is used to work with a plain stack of layers, where every layer has exactly one input tensor and one output tensor.

A neural network that contains at least one layer is known as a convolutional layer. We can use the Convolutional Neural Network to build learning model. 

We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.

Due to overfitting, the model will not be able to generalize well on the new dataset. There are many ways in which overfitting can be avoided. We can use drop out technique to overcome overfitting. Overfitting can be reduced by introducing dropout in the network. This is considered as a form of regularization. This helps expose the model to more aspects of the data, thereby helping the model generalize better.

When dropout is applied to a layer, it randomly drops out a number of output units from the layer when the training is going on. This is done by setting the activation function to 0. Dropout technique takes a fractional number as the input value (like 0.1, 0.2, 0.4, and so on). This number 0.1 or 0.2 basically indicates that 10 percent or 20 percent of the output units are randomly from the applied layer.

Data augmentation generates additional training data from the existing examples by augmenting them with the help of random transformations that would yield believable-looking images. Following is an example:

Example

print("Compiling the model")
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
print("The complete architecture of the model")
model.summary()

Code credit −https://www.tensorflow.org/tutorials/images/classification

Output

Compiling the model
The complete architecture of the model
Model: "sequential_2"
Layer (type)                Output Shape        Param #
=================================================================
sequential_1 (Sequential)  (None, 180, 180, 3)      0
_________________________________________________________________
rescaling_2 (Rescaling)  (None, 180, 180, 3)        0
_________________________________________________________________
conv2d_3 (Conv2D)         (None, 180, 180, 16)     448
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 90, 90, 16)      0
_________________________________________________________________
conv2d_4 (Conv2D)         (None, 90, 90, 32)        4640
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 45, 45, 32)       0
_________________________________________________________________
conv2d_5 (Conv2D)         (None, 45, 45, 64)         18496
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 22, 22, 64)        0
_________________________________________________________________
dropout (Dropout)         (None, 22, 22, 64)            0
_________________________________________________________________
flatten_1 (Flatten)         (None, 30976)               0
_________________________________________________________________
dense_2 (Dense)            (None, 128)                3965056
_________________________________________________________________
dense_3 (Dense)             (None, 5)                  645
=================================================================
Total params: 3,989,285
Trainable params: 3,989,285
Non-trainable params: 0
_________________________________________________________________

Explanation

  • The model is compiled using the ‘fit’ method.
  • The ‘summary’ method is used to get the complete architecture of the model.
raja
Published on 12-Feb-2021 15:38:48
Advertisements