@BruceWang
2018-01-01T07:37:54.000000Z
字数 4714
阅读 1651
神经网络
本文的PPT你可以从这里下载
In this exercise, you'll write code to do forward propagation (prediction) for your first neural network:

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.
In [1]: import numpy as npIn [2]: input_data = np.array([2, 3])In [3]: weights = {'node_0': np.array([1, 1]),...: 'node_1': np.array([-1, 1]),...: 'output': np.array([2, -1])}In [4]: node_0_value = (input_data * weights['node_0']).sum()In [5]: node_1_value = (input_data * weights['node_1']).sum()# Calculate node 0 value: node_0_valuenode_0_value = (input_data * weights['node_0']).sum()# Calculate node 1 value: node_1_valuenode_1_value = (input_data * weights['node_1']).sum()# Put node values into array: hidden_layer_outputshidden_layer_outputs = np.array([node_0_value, node_1_value])# Calculate output: outputoutput = (hidden_layer_outputs * weights['output']).sum()# Print outputprint(output)
Relu
def relu(input):'''Define your relu activation function here'''# Calculate the value for the output of the relu function: outputoutput = max(input, 0)# Return the value just calculatedreturn(output)# Calculate node 0 value: node_0_outputnode_0_input = (input_data * weights['node_0']).sum()node_0_output = relu(node_0_input)# Calculate node 1 value: node_1_outputnode_1_input = (input_data * weights['node_1']).sum()node_1_output = relu(node_1_input)# Put node values into array: hidden_layer_outputshidden_layer_outputs = np.array([node_0_output, node_1_output])# Calculate model output (do not apply relu)model_output = (hidden_layer_outputs * weights['output']).sum()# Print model outputprint(model_output)
# Define predict_with_network()def predict_with_network(input_data_row, weights):# Calculate node 0 valuenode_0_input = (input_data_row * weights['node_0']).sum()node_0_output = relu(node_0_input)# Calculate node 1 valuenode_1_input = (input_data_row * weights['node_1']).sum()node_1_output = relu(node_1_input)# Put node values into array: hidden_layer_outputshidden_layer_outputs = np.array([node_0_output, node_1_output])# Calculate model outputinput_to_final_layer = (hidden_layer_outputs * weights['output']).sum()model_output = relu(input_to_final_layer)# Return model outputreturn(model_output)# Create empty list to store prediction resultsresults = []for input_data_row in input_data:# Append prediction to resultsresults.append(predict_with_network(input_data_row, weights))# Print resultsprint(results)
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'].
def predict_with_network(input_data):# Calculate node 0 in the first hidden layernode_0_0_input = (input_data * weights['node_0_0']).sum()node_0_0_output = relu(node_0_0_input)# Calculate node 1 in the first hidden layernode_0_1_input = (input_data * weights['node_0_1']).sum()node_0_1_output = relu(node_0_1_input)# Put node values into array: hidden_0_outputshidden_0_outputs = np.array([node_0_0_output, node_0_1_output])# Calculate node 0 in the second hidden layernode_1_0_input = (hidden_0_outputs * weights['node_1_0']).sum()node_1_0_output = relu(node_1_0_input)# Calculate node 1 in the second hidden layernode_1_1_input = (hidden_0_outputs * weights['node_1_1']).sum()node_1_1_output = relu(node_1_1_input)# Put node values into array: hidden_1_outputshidden_1_outputs = np.array([node_1_0_output, node_1_1_output])# Calculate model output: model_outputmodel_output = relu((hidden_1_outputs * weights['output']).sum())# Return model_outputreturn(model_output)output = predict_with_network(input_data)print(output)