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 Models, Layers, and Loss Functions with TensorFlow/Week 1 - Functional APIs/C1W1_Assignment.ipynb
Views: 13372
Week 1: Multiple Output Models using the Keras Functional API
Welcome to the first programming assignment of the course! Your task will be to use the Keras functional API to train a model to predict two outputs. For this lab, you will use the Wine Quality Dataset from the UCI machine learning repository. It has separate datasets for red wine and white wine.
Normally, the wines are classified into one of the quality ratings specified in the attributes. In this exercise, you will combine the two datasets to predict the wine quality and whether the wine is red or white solely from the attributes.
You will model wine quality estimations as a regression problem and wine type detection as a binary classification problem.
Please complete sections that are marked (TODO)
Imports
Load Dataset
You will now download the dataset from the UCI Machine Learning Repository.
Pre-process the white wine dataset (TODO)
You will add a new column named is_red
in your dataframe to indicate if the wine is white or red.
In the white wine dataset, you will fill the column
is_red
with zeros (0).
All public tests passed
Pre-process the red wine dataset (TODO)
In the red wine dataset, you will fill in the column
is_red
with ones (1).
All public tests passed
Concatenate the datasets
Next, concatenate the red and white wine dataframes.
This will chart the quality of the wines.
Imbalanced data (TODO)
You can see from the plot above that the wine quality dataset is imbalanced.
Since there are very few observations with quality equal to 3, 4, 8 and 9, you can drop these observations from your dataset.
You can do this by removing data belonging to all classes except those > 4 and < 8.
All public tests passed
You can plot again to see the new range of data and quality
Train Test Split (TODO)
Next, you can split the datasets into training, test and validation datasets.
The data frame should be split 80:20 into
train
andtest
sets.The resulting
train
should then be split 80:20 intotrain
andval
sets.The
train_test_split
parametertest_size
takes a float value that ranges between 0. and 1, and represents the proportion of the dataset that is allocated to the test set. The rest of the data is allocated to the training set.
All public tests passed
Here's where you can explore the training stats. You can pop the labels 'is_red' and 'quality' from the data as these will be used as the labels
Explore the training stats!
Get the labels (TODO)
The features and labels are currently in the same dataframe.
You will want to store the label columns
is_red
andquality
separately from the feature columns.The following function,
format_output
, gets these two columns from the dataframe (it's given to you).format_output
also formats the data into numpy arrays.Please use the
format_output
and apply it to thetrain
,val
andtest
sets to get dataframes for the labels.
All public tests passed
Notice that after you get the labels, the train
, val
and test
dataframes no longer contain the label columns, and contain just the feature columns.
This is because you used
.pop
in theformat_output
function.
Normalize the data (TODO)
Next, you can normalize the data, x, using the formula:
The
norm
function is defined for you.Please apply the
norm
function to normalize the dataframes that contains the feature columns oftrain
,val
andtest
sets.
All public tests passed
Define the Model (TODO)
Define the model using the functional API. The base model will be 2 Dense
layers of 128 neurons each, and have the 'relu'
activation.
Check out the documentation for tf.keras.layers.Dense
All public tests passed
Define output layers of the model (TODO)
You will add output layers to the base model.
The model will need two outputs.
One output layer will predict wine quality, which is a numeric value.
Define a
Dense
layer with 1 neuron.Since this is a regression output, the activation can be left as its default value
None
.
The other output layer will predict the wine type, which is either red 1
or not red 0
(white).
Define a
Dense
layer with 1 neuron.Since there are two possible categories, you can use a sigmoid activation for binary classification.
Define the Model
Define the
Model
object, and set the following parameters:inputs
: pass in the inputs to the model as a list.outputs
: pass in a list of the outputs that you just defined: wine quality, then wine type.Note: please list the wine quality before wine type in the outputs, as this will affect the calculated loss if you choose the other order.
All public tests passed
Compiling the Model
Next, compile the model. When setting the loss parameter of model.compile
, you're setting the loss for each of the two outputs (wine quality and wine type).
To set more than one loss, use a dictionary of key-value pairs.
You can look at the docs for the losses here.
Note: For the desired spelling, please look at the "Functions" section of the documentation and not the "classes" section on that same page.
wine_type: Since you will be performing binary classification on wine type, you should use the binary crossentropy loss function for it. Please pass this in as a string.
Hint, this should be all lowercase. In the documentation, you'll see this under the "Functions" section, not the "Classes" section.
wine_quality: since this is a regression output, use the mean squared error. Please pass it in as a string, all lowercase.
Hint: You may notice that there are two aliases for mean squared error. Please use the shorter name.
You will also set the metric for each of the two outputs. Again, to set metrics for two or more outputs, use a dictionary with key value pairs.
The metrics documentation is linked here.
For the wine type, please set it to accuracy as a string, all lowercase.
For wine quality, please use the root mean squared error. Instead of a string, you'll set it to an instance of the class RootMeanSquaredError, which belongs to the tf.keras.metrics module.
Note: If you see the error message
Exception: wine quality loss function is incorrect.
Please also check your other losses and metrics, as the error may be caused by the other three key-value pairs and not the wine quality loss.
All public tests passed
Training the Model
Fit the model to the training inputs and outputs.
Check the documentation for model.fit.
Remember to use the normalized training set as inputs.
For the validation data, please use the normalized validation set.
All public tests passed
Analyze the Model Performance
Note that the model has two outputs. The output at index 0 is quality and index 1 is wine type
So, round the quality predictions to the nearest integer.
Plot Utilities
We define a few utilities to visualize the model performance.
Plots for Metrics
Plots for Confusion Matrix
Plot the confusion matrices for wine type. You can see that the model performs well for prediction of wine type from the confusion matrix and the loss metrics.