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_1_training-basics.ipynb
Views: 13372
Custom Training Basics
In this ungraded lab you'll gain a basic understanding of building custom training loops.
It takes you through the underlying logic of fitting any model to a set of inputs and outputs.
You will be training your model on the linear equation for a straight line, wx + b.
You will implement basic linear regression from scratch using gradient tape.
You will try to minimize the loss incurred by the model using linear regression.
Imports
Define Model
You define your model as a class.
x
is your input tensor.The model should output values of wx+b.
You'll start off by initializing w and b to random values.
During the training process, values of w and b get updated in accordance with linear regression so as to minimize the loss incurred by the model.
Once you arrive at optimal values for w and b, the model would have been trained to correctly predict the values of wx+b.
Hence,
w and b are trainable weights of the model.
x is the input
y = wx + b is the output
Define a loss function
A loss function measures how well the output of a model for a given input matches the target output.
The goal is to minimize this difference during training.
Let's use the standard L2 loss, also known as the least square errors
Obtain training data
First, synthesize the training data using the "true" w and "true" b.
Before training the model, visualize the loss value by plotting the model's predictions in red crosses and the training data in blue dots:
Define a training loop
With the network and training data, train the model using gradient descent
Gradient descent updates the trainable weights w and b to reduce the loss.
There are many variants of the gradient descent scheme that are captured in tf.train.Optimizer
—our recommended implementation. In the spirit of building from first principles, here you will implement the basic math yourself.
You'll use
tf.GradientTape
for automatic differentiationUse
tf.assign_sub
for decrementing a value. Note that assign_sub combinestf.assign
andtf.sub
Finally, you can iteratively run through the training data and see how w
and b
evolve.
In addition to the values for losses, you also plot the progression of trainable variables over epochs.
Plots for Evaluation
Now you can plot the actual outputs in red and the model's predictions in blue on a set of random test examples.
You can see that the model is able to make predictions on the test set fairly accurately.
Visualize the cost function against the values of each of the trainable weights the model approximated to over time.