Get startedGet started for free

Adding layers to a network

You've seen how to experiment with wider networks. In this exercise, you'll try a deeper network (more hidden layers).

Once again, you have a baseline model called model_1 as a starting point. It has 1 hidden layer, with 10 units. You can see a summary of that model's structure printed out. You will create a similar network with 3 hidden layers (still keeping 10 units in each layer).

This will again take a moment to fit both models, so you'll need to wait a few seconds to see the results after you run your code.

This exercise is part of the course

Introduction to Deep Learning in Python

View Course

Exercise instructions

  • Specify a model called model_2 that is like model_1, but which has 3 hidden layers of 10 units instead of only 1 hidden layer.
    • Use input_shape to specify the input shape in the first hidden layer.
    • Use 'relu' activation for the 3 hidden layers and 'softmax' for the output layer, which should have 2 units.
  • Compile model_2 as you have done with previous models: Using 'adam' as the optimizer, 'categorical_crossentropy' for the loss, and metrics=['accuracy'].
  • Hit 'Submit Answer' to fit both the models and visualize which one gives better results!

Hands-on interactive exercise

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

# The input shape to use in the first hidden layer
input_shape = (n_cols,)

# Create the new model: model_2
model_2 = ____

# Add the first, second, and third hidden layers
____
____
____

# Add the output layer
____

# Compile model_2
____

# Fit model 1
model_1_training = model_1.fit(predictors, target, epochs=15, validation_split=0.4, verbose=False)

# Fit model 2
model_2_training = model_2.fit(predictors, target, epochs=15, validation_split=0.4, verbose=False)

# Create the plot
plt.plot(model_1_training.history['val_loss'], 'r', model_2_training.history['val_loss'], 'b')
plt.xlabel('Epochs')
plt.ylabel('Validation score')
plt.show()
Edit and Run Code