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/C2W2_Assignment.ipynb
Views: 13372
Breast Cancer Prediction
In this exercise, you will train a neural network on the Breast Cancer Dataset to predict if the tumor is malignant or benign.
If you get stuck, we recommend that you review the ungraded labs for this week.
Imports
Load and Preprocess the Dataset
We first download the dataset and create a data frame using pandas. We explicitly specify the column names because the CSV file does not have column headers.
We have to do some preprocessing on the data. We first pop the id column since it is of no use for our problem at hand.
Upon inspection of data, you can see that some values of the bare_nuclei column are unknown. We drop the rows with these unknown values. We also convert the bare_nuclei column to numeric. This is required for training the model.
We check the class distribution of the data. You can see that there are two classes, 2.0 and 4.0 According to the dataset:
2.0 = benign
4.0 = malignant
We are going to model this problem as a binary classification problem which detects whether the tumor is malignant or not. Hence, we change the dataset so that:
benign(2.0) = 0
malignant(4.0) = 1
We then split the dataset into training and testing sets. Since the number of samples is small, we will perform validation on the test set.
We get the statistics for training. We can look at statistics to get an idea about the distribution of plots. If you need more visualization, you can create additional data plots. We will also be using the mean and standard deviation from statistics for normalizing the data
We pop the class column from the training and test sets to create train and test outputs.
Here we normalize the data by using the formula: X = (X - mean(X)) / StandardDeviation(X)
We now create Tensorflow datasets for training and test sets to easily be able to build and manage an input pipeline for our model.
We shuffle and prepare a batched dataset to be used for training in our custom training loop.
Define the Model
Now we will define the model. Here, we use the Keras Functional API to create a simple network of two Dense
layers. We have modelled the problem as a binary classification problem and hence we add a single layer with sigmoid activation as the final layer of the model.
Define Optimizer and Loss
We use RMSprop optimizer and binary crossentropy as our loss function.
Evaluate Untrained Model
We calculate the loss on the model before training begins.
We also plot the confusion matrix to visualize the true outputs against the outputs predicted by the model.
Define Metrics (Please complete this section)
Define Custom F1Score Metric
In this example, we will define a custom F1Score metric using the formula.
F1 Score = 2 * ((precision * recall) / (precision + recall))
precision = true_positives / (true_positives + false_positives)
recall = true_positives / (true_positives + false_negatives)
We use confusion_matrix
defined in tf.math
to calculate precision and recall.
Here you can see that we have subclassed tf.keras.Metric
and implemented the three required methods update_state
, result
and reset_states
.
Please complete the result() method:
Expected Output:
We initialize the seprate metrics required for training and validation. In addition to our custom F1Score metric, we are also using BinaryAccuracy
defined in tf.keras.metrics
Apply Gradients (Please complete this section)
The core of training is using the model to calculate the logits on specific set of inputs and compute the loss(in this case binary crossentropy) by comparing the predicted outputs to the true outputs. We then update the trainable weights using the optimizer algorithm chosen. The optimizer algorithm requires our computed loss and partial derivatives of loss with respect to each of the trainable weights to make updates to the same.
We use gradient tape to calculate the gradients and then update the model trainable weights using the optimizer.
Please complete the following function:
Expected Output:
The output will be close to these values:
Training Loop (Please complete this section)
This function performs training during one epoch. We run through all batches of training data in each epoch to make updates to trainable weights using our previous function. You can see that we also call update_state
on our metrics to accumulate the value of our metrics.
We are displaying a progress bar to indicate completion of training in each epoch. Here we use tqdm
for displaying the progress bar.
Please complete the following function:
Expected Output:
The losses should generally be decreasing and will start from around 0.75. For example:
At the end of each epoch, we 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 we define the training loop that runs through the training samples repeatedly over a fixed number of epochs. Here we combine the functions we 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 : We also calculate the training and validation losses for the whole epoch at the end of the epoch.
Evaluate the Model
Plots for Evaluation
We plot the progress of loss as training proceeds over number of epochs.
We plot the confusion matrix to visualize the true values against the values predicted by the model.