Get startedGet started for free

Multi-layer neural networks

In this exercise, you'll write code to do forward propagation for a neural network with 2 hidden layers. Each hidden layer has two nodes. The input data has been preloaded as input_data. The nodes in the first hidden layer are called node_0_0 and node_0_1. Their weights are pre-loaded as weights['node_0_0'] and weights['node_0_1'] respectively.

The nodes in the second hidden layer are called node_1_0 and node_1_1. Their weights are pre-loaded as weights['node_1_0'] and weights['node_1_1'] respectively.

We then create a model output from the hidden nodes using weights pre-loaded as weights['output'].

Ch1Ex10

This exercise is part of the course

Introduction to Deep Learning in Python

View Course

Exercise instructions

  • Calculate node_0_0_input using its weights weights['node_0_0'] and the given input_data. Then apply the relu() function to get node_0_0_output.
  • Do the same as above for node_0_1_input to get node_0_1_output.
  • Calculate node_1_0_input using its weights weights['node_1_0'] and the outputs from the first hidden layer - hidden_0_outputs. Then apply the relu() function to get node_1_0_output.
  • Do the same as above for node_1_1_input to get node_1_1_output.
  • Calculate model_output using its weights weights['output'] and the outputs from the second hidden layer hidden_1_outputs array. Do not apply the relu() function to this output.

Hands-on interactive exercise

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

def predict_with_network(input_data):
    # Calculate node 0 in the first hidden layer
    node_0_0_input = (____ * ____).sum()
    node_0_0_output = relu(____)

    # Calculate node 1 in the first hidden layer
    node_0_1_input = ____
    node_0_1_output = ____

    # Put node values into array: hidden_0_outputs
    hidden_0_outputs = np.array([node_0_0_output, node_0_1_output])
    
    # Calculate node 0 in the second hidden layer
    node_1_0_input = ____
    node_1_0_output = ____

    # Calculate node 1 in the second hidden layer
    node_1_1_input = ____
    node_1_1_output = ____

    # Put node values into array: hidden_1_outputs
    hidden_1_outputs = np.array([node_1_0_output, node_1_1_output])

    # Calculate model output: model_output
    model_output = ____
    
    # Return model_output
    return(model_output)

output = predict_with_network(input_data)
print(output)
Edit and Run Code