[关闭]
@BruceWang 2018-01-01T15:37:54.000000Z 字数 4714 阅读 1399

神经网络前向传播python编程练习

神经网络


1.Coding the single forward propagation algorithm

本文的PPT你可以从这里下载

In this exercise, you'll write code to do forward propagation (prediction) for your first neural network:

FP

Each data point is a customer. The first input is how many accounts they have, and the second input is how many children they have. The model will predict how many transactions the user makes in the next year. You will use this data throughout the first 2 chapters of this course.

The input data has been pre-loaded as input_data, and the weights are available in a dictionary called weights. The array of weights for the first node in the hidden layer are in weights['node_0'], and the array of weights for the second node in the hidden layer are in weights['node_1']

The weights feeding into the output node are available in weights ['output'].

NumPy will be pre-imported for you as np in all exercises

Calculate the value in node 0 by multiplying input_data by its weights weights['node_0'] and computing their sum. This is the 1st node in the hidden layer.
Calculate the value in node 1 using input_data and weights['node_1']. This is the 2nd node in the hidden layer.
Put the hidden layer values into an array. This has been done for you.
Generate the prediction by multiplying hidden_layer_outputs by weights['output'] and computing their sum.

  1. In [1]: import numpy as np
  2. In [2]: input_data = np.array([2, 3])
  3. In [3]: weights = {'node_0': np.array([1, 1]),
  4. ...: 'node_1': np.array([-1, 1]),
  5. ...: 'output': np.array([2, -1])}
  6. In [4]: node_0_value = (input_data * weights['node_0']).sum()
  7. In [5]: node_1_value = (input_data * weights['node_1']).sum()
  8. # Calculate node 0 value: node_0_value
  9. node_0_value = (input_data * weights['node_0']).sum()
  10. # Calculate node 1 value: node_1_value
  11. node_1_value = (input_data * weights['node_1']).sum()
  12. # Put node values into array: hidden_layer_outputs
  13. hidden_layer_outputs = np.array([node_0_value, node_1_value])
  14. # Calculate output: output
  15. output = (hidden_layer_outputs * weights['output']).sum()
  16. # Print output
  17. print(output)

Activation function Relu

  1. def relu(input):
  2. '''Define your relu activation function here'''
  3. # Calculate the value for the output of the relu function: output
  4. output = max(input, 0)
  5. # Return the value just calculated
  6. return(output)
  7. # Calculate node 0 value: node_0_output
  8. node_0_input = (input_data * weights['node_0']).sum()
  9. node_0_output = relu(node_0_input)
  10. # Calculate node 1 value: node_1_output
  11. node_1_input = (input_data * weights['node_1']).sum()
  12. node_1_output = relu(node_1_input)
  13. # Put node values into array: hidden_layer_outputs
  14. hidden_layer_outputs = np.array([node_0_output, node_1_output])
  15. # Calculate model output (do not apply relu)
  16. model_output = (hidden_layer_outputs * weights['output']).sum()
  17. # Print model output
  18. print(model_output)

Execution of the FP Network

  1. # Define predict_with_network()
  2. def predict_with_network(input_data_row, weights):
  3. # Calculate node 0 value
  4. node_0_input = (input_data_row * weights['node_0']).sum()
  5. node_0_output = relu(node_0_input)
  6. # Calculate node 1 value
  7. node_1_input = (input_data_row * weights['node_1']).sum()
  8. node_1_output = relu(node_1_input)
  9. # Put node values into array: hidden_layer_outputs
  10. hidden_layer_outputs = np.array([node_0_output, node_1_output])
  11. # Calculate model output
  12. input_to_final_layer = (hidden_layer_outputs * weights['output']).sum()
  13. model_output = relu(input_to_final_layer)
  14. # Return model output
  15. return(model_output)
  16. # Create empty list to store prediction results
  17. results = []
  18. for input_data_row in input_data:
  19. # Append prediction to results
  20. results.append(predict_with_network(input_data_row, weights))
  21. # Print results
  22. print(results)

2.Coding the multi-hidden-layer forward propagation

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.

Deeper FP

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'].

  1. def predict_with_network(input_data):
  2. # Calculate node 0 in the first hidden layer
  3. node_0_0_input = (input_data * weights['node_0_0']).sum()
  4. node_0_0_output = relu(node_0_0_input)
  5. # Calculate node 1 in the first hidden layer
  6. node_0_1_input = (input_data * weights['node_0_1']).sum()
  7. node_0_1_output = relu(node_0_1_input)
  8. # Put node values into array: hidden_0_outputs
  9. hidden_0_outputs = np.array([node_0_0_output, node_0_1_output])
  10. # Calculate node 0 in the second hidden layer
  11. node_1_0_input = (hidden_0_outputs * weights['node_1_0']).sum()
  12. node_1_0_output = relu(node_1_0_input)
  13. # Calculate node 1 in the second hidden layer
  14. node_1_1_input = (hidden_0_outputs * weights['node_1_1']).sum()
  15. node_1_1_output = relu(node_1_1_input)
  16. # Put node values into array: hidden_1_outputs
  17. hidden_1_outputs = np.array([node_1_0_output, node_1_1_output])
  18. # Calculate model output: model_output
  19. model_output = relu((hidden_1_outputs * weights['output']).sum())
  20. # Return model_output
  21. return(model_output)
  22. output = predict_with_network(input_data)
  23. print(output)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注