Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Custom and Distributed Training with Tensorflow/Week 2 - Custom Training/C2_W2_Lab_2_training-categorical.ipynb
Views: 13370
Fashion MNIST using Custom Training Loop
In this ungraded lab, you will build a custom training loop including a validation loop so as to train a model on the Fashion MNIST dataset.
Imports
Load and Preprocess Data
You will load the Fashion MNIST dataset using Tensorflow Datasets. This dataset has 28 x 28 grayscale images of articles of clothing belonging to 10 clases.
Here you are going to use the training and testing splits of the data. Testing split will be used for validation.
Next, you normalize the images by dividing them by 255.0 so as to make the pixels fall in the range (0, 1). You also reshape the data so as to flatten the 28 x 28 pixel array into a flattened 784 pixel array.
Now you shuffle and batch your training and test datasets before feeding them to the model.
Define the Model
You are using a simple model in this example. You use Keras Functional API to connect two dense layers. The final layer is a softmax that outputs one of the 10 classes since this is a multi class classification problem.
Define Optimizer and Loss Function
You have chosen adam
optimizer and sparse categorical crossentropy loss for this example.
Define Metrics
You will also define metrics so that your training loop can update and display them. Here you are using SparseCategoricalAccuracy
defined in tf.keras.metrics
since the problem at hand is a multi class classification problem.
Building Training Loop
In this section you build your training loop consisting of training and validation sequences.
The core of training is using the model to calculate the logits on specific set of inputs and compute loss (in this case sparse categorical crossentropy) by comparing the predicted outputs to the true outputs. You then update the trainable weights using the optimizer algorithm chosen. Optimizer algorithm requires your computed loss and partial derivatives of loss with respect to each of the trainable weights to make updates to the same.
You use gradient tape to calculate the gradients and then update the model trainable weights using the optimizer.
This function performs training during one epoch. You run through all batches of training data in each epoch to make updates to trainable weights using your previous function. You can see that we also call update_state on your metrics to accumulate the value of your metrics. You are displaying a progress bar to indicate completion of training in each epoch. Here you use tqdm for displaying the progress bar.
At the end of each epoch you have to validate the model on the test dataset. The following function calculates the loss on test dataset and updates the states of the validation metrics.
Next you define the training loop that runs through the training samples repeatedly over a fixed number of epochs. Here you combine the functions you built earlier to establish the following flow:
Perform training over all batches of training data.
Get values of metrics.
Perform validation to calculate loss and update validation metrics on test data.
Reset the metrics at the end of epoch.
Display statistics at the end of each epoch.
Note : You also calculate the training and validation losses for the whole epoch at the end of the epoch.
Evaluate Model
Plots for Evaluation
You plot the progress of loss as training proceeds over number of epochs.
This function displays a row of images with their predictions and true labels.
You make predictions on the test dataset and plot the images with their true and predicted values.