Get startedGet started for free

Metrics and validation with Keras

We trained a model to predict sign language letters in the previous exercise, but it is unclear how successful we were in doing so. In this exercise, we will try to improve upon the interpretability of our results. Since we did not use a validation split, we only observed performance improvements within the training set; however, it is unclear how much of that was due to overfitting. Furthermore, since we did not supply a metric, we only saw decreases in the loss function, which do not have any clear interpretation.

Note that keras has been imported for you from tensorflow.

This exercise is part of the course

Introduction to TensorFlow in Python

View Course

Exercise instructions

  • Set the first dense layer to have 32 nodes, use a sigmoid activation function, and have an input shape of (784,).
  • Use the root mean square propagation optimizer, a categorical crossentropy loss, and the accuracy metric.
  • Set the number of epochs to 10 and use 10% of the dataset for validation.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Define sequential model
model = keras.Sequential()

# Define the first layer
model.add(keras.layers.Dense(____, ____, ____))

# Add activation function to classifier
model.add(keras.layers.Dense(4, activation='softmax'))

# Set the optimizer, loss function, and metrics
model.compile(optimizer='____', loss='____', metrics=['____'])

# Add the number of epochs and the validation split
model.fit(sign_language_features, sign_language_labels, epochs=____, validation_split=____)
Edit and Run Code