# How can Tensorflow be used to fit the augmented data to the model?

TensorflowServer Side ProgrammingProgramming

The augmented model can be compiled using the ‘compile’ method, which also takes the validation data and the number of epochs (number of training steps) into the method as parameters.

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.

An image classifier is created using a keras.Sequential model, and data is loaded using preprocessing.image_dataset_from_directory. Data is efficiently loaded off disk. Overfitting is identified and techniques are applied to mitigate it. These techniques include data augmentation, and dropout. There are images of 3700 flowers. This dataset contaisn 5 sub directories, and there is one sub directory per class. They are:

daisy, dandelion, roses, sunflowers, and tulips.

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.

When the number of training examples is small, the model learns from noises or unwanted details from training examples. This negatively impacts the performance of the model on new examples.

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. Following is an example:

## Example

print("Train the model to fit the given data")
epochs = 15
history = model.fit(
train_ds,
validation_data=val_ds,
epochs=epochs
)

## Output

Train the model to fit the given data
Epoch 1/15
92/92 [==============================] - 104s 1s/step - loss: 1.5371 - accuracy: 0.3103 - val_loss: 1.1487 - val_accuracy: 0.5313
Epoch 2/15
92/92 [==============================] - 102s 1s/step - loss: 1.1414 - accuracy: 0.5087 - val_loss: 1.3204 - val_accuracy: 0.5286
Epoch 3/15
92/92 [==============================] - 102s 1s/step - loss: 1.0260 - accuracy: 0.5991 - val_loss: 0.9823 - val_accuracy: 0.6281
Epoch 4/15
92/92 [==============================] - 103s 1s/step - loss: 0.9431 - accuracy: 0.6466 - val_loss: 0.9937 - val_accuracy: 0.6417
Epoch 5/15
92/92 [==============================] - 104s 1s/step - loss: 0.8294 - accuracy: 0.6706 - val_loss: 0.8365 - val_accuracy: 0.6866
Epoch 6/15
92/92 [==============================] - 103s 1s/step - loss: 0.7584 - accuracy: 0.7101 - val_loss: 0.8471 - val_accuracy: 0.6717
Epoch 7/15
92/92 [==============================] - 102s 1s/step - loss: 0.7323 - accuracy: 0.7225 - val_loss: 0.7949 - val_accuracy: 0.6853
Epoch 8/15
92/92 [==============================] - 104s 1s/step - loss: 0.7197 - accuracy: 0.7168 - val_loss: 0.8422 - val_accuracy: 0.6839
Epoch 9/15
92/92 [==============================] - 103s 1s/step - loss: 0.7209 - accuracy: 0.7229 - val_loss: 0.7502 - val_accuracy: 0.7193
Epoch 10/15
92/92 [==============================] - 104s 1s/step - loss: 0.6334 - accuracy: 0.7590 - val_loss: 0.8248 - val_accuracy: 0.7016
Epoch 11/15
92/92 [==============================] - 104s 1s/step - loss: 0.6052 - accuracy: 0.7737 - val_loss: 0.7400 - val_accuracy: 0.7153
Epoch 12/15
92/92 [==============================] - 104s 1s/step - loss: 0.5713 - accuracy: 0.7831 - val_loss: 0.7424 - val_accuracy: 0.7098
Epoch 13/15
92/92 [==============================] - 105s 1s/step - loss: 0.5627 - accuracy: 0.7875 - val_loss: 0.7028 - val_accuracy: 0.7275
Epoch 14/15
92/92 [==============================] - 103s 1s/step - loss: 0.5254 - accuracy: 0.8029 - val_loss: 0.6900 - val_accuracy: 0.7371
Epoch 15/15
92/92 [==============================] - 103s 1s/step - loss: 0.5187 - accuracy: 0.7984 - val_loss: 0.7839 - val_accuracy: 0.7153

## Explanation

• The model is trained to fit the training data.
• The training goes on for 15 steps/iterations/epochs.
• This is done with the help of the ‘fit’ method.
Published on 12-Feb-2021 11:10:59