Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XingLiangLondon/731c9d0eb86f70b2794805be16ded744 to your computer and use it in GitHub Desktop.
Save XingLiangLondon/731c9d0eb86f70b2794805be16ded744 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://cognitiveclass.ai\"><img src = \"https://ibm.box.com/shared/static/9gegpsmnsoo25ikkbl4qzlvlyjbgxs5x.png\" width = 400> </a>\n",
"\n",
"<h1 align=center><font size = 5>Artificial Neural Networks - Forward Propagation</font></h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Introduction\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this lab, we will build a neural network from scratch and code how it performs predictions using forward propagation. Please note that all deep learning libraries have the entire training and prediction processes implemented, and so in practice you wouldn't really need to build a neural network from scratch. However, hopefully completing this lab will help you understand neural networks and how they work even better."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Table of Contents\n",
"\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"\n",
"<font size = 3> \n",
"1. <a href=\"#item1\">Recap</a> \n",
"2. <a href=\"#item2\">Initalize a Network</a> \n",
"3. <a href=\"#item3\">Compute Weighted Sum at Each Node</a> \n",
"4. <a href=\"#item4\">Compute Node Activation</a> \n",
"5. <a href=\"#item5\">Forward Propagation</a>\n",
"</font>\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id=\"item1\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Recap"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From the videos, let's recap how a neural network makes predictions through the forward propagation process. Here is a neural network that takes two inputs, has one hidden layer with two nodes, and an output layer with one node."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"http://cocl.us/neural_network_example\" alt=\"Neural Network Example\" width=600px>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's start by randomly initializing the weights and the biases in the network. We have 6 weights and 3 biases, one for each node in the hidden layer as well as for each node in the output layer."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np # import Numpy library to generate \n",
"\n",
"weights = np.around(np.random.uniform(size=6), decimals=2) # initialize the weights\n",
"biases = np.around(np.random.uniform(size=3), decimals=2) # initialize the biases"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's print the weights and biases for sanity check."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.42 0.88 0.31 0.95 0.44 0.72]\n",
"[0.29 0.66 0.36]\n"
]
}
],
"source": [
"print(weights)\n",
"print(biases)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have the weights and the biases defined for the network, let's compute the output for a given input, $x_1$ and $x_2$."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x1 is 0.5 and x2 is 0.85\n"
]
}
],
"source": [
"x_1 = 0.5 # input 1\n",
"x_2 = 0.85 # input 2\n",
"\n",
"print('x1 is {} and x2 is {}'.format(x_1, x_2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's start by computing the wighted sum of the inputs, $z_{1, 1}$, at the first node of the hidden layer."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The weighted sum of the inputs at the first node in the hidden layer is 1.248\n"
]
}
],
"source": [
"z_11 = x_1 * weights[0] + x_2 * weights[1] + biases[0]\n",
"\n",
"print('The weighted sum of the inputs at the first node in the hidden layer is {}'.format(z_11))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, let's compute the weighted sum of the inputs, $z_{1, 2}$, at the second node of the hidden layer. Assign the value to **z_12**."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"### type your answer here\n",
"\n",
"z_12= x_1*weights[2]+x_2*weights[3]+biases[1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click __here__ for the solution.\n",
"<!-- The correct answer is:\n",
"z_12 = x_1 * weights[2] + x_2 * weights[3] + biases[1]\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print the weighted sum."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The weighted sum of the inputs at the second node in the hidden layer is 1.6225\n"
]
}
],
"source": [
"print('The weighted sum of the inputs at the second node in the hidden layer is {}'.format(np.around(z_12, decimals=4)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, assuming a sigmoid activation function, let's compute the activation of the first node, $a_{1, 1}$, in the hidden layer."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The activation of the first node in the hidden layer is 0.777\n"
]
}
],
"source": [
"a_11 = 1.0 / (1.0 + np.exp(-z_11))\n",
"\n",
"print('The activation of the first node in the hidden layer is {}'.format(np.around(a_11, decimals=4)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's also compute the activation of the second node, $a_{1, 2}$, in the hidden layer. Assign the value to **a_12**."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"### type your answer here\n",
"\n",
"a_12=1/(1+np.exp(-z_12))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click __here__ for the solution.\n",
"<!-- The correct answer is:\n",
"a_12 = 1.0 / (1.0 + np.exp(-z_12))\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print the activation of the second node."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The activation of the second node in the hidden layer is 0.8351\n"
]
}
],
"source": [
"print('The activation of the second node in the hidden layer is {}'.format(np.around(a_12, decimals=4)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now these activations will serve as the inputs to the output layer. So, let's compute the weighted sum of these inputs to the node in the output layer. Assign the value to **z_2**."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"### type your answer here\n",
"z_2= a_11*weights[4]+ a_12*weights[5]+biases[2]\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click __here__ for the solution.\n",
"<!-- The correct answer is:\n",
"z_2 = a_11 * weights[4] + a_12 * weights[5] + biases[2]\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print the weighted sum of the inputs at the node in the output layer."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The weighted sum of the inputs at the node in the output layer is 1.3032\n"
]
}
],
"source": [
"print('The weighted sum of the inputs at the node in the output layer is {}'.format(np.around(z_2, decimals=4)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, let's compute the output of the network as the activation of the node in the output layer. Assign the value to **a_2**."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"### type your answer here\n",
"a_2=1.0/(1.0+np.exp(-z_2))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click __here__ for the solution.\n",
"<!-- The correct answer is:\n",
"a_2 = 1.0 / (1.0 + np.exp(-z_2))\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print the activation of the node in the output layer which is equivalent to the prediction made by the network."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The output of the network for x1 = 0.5 and x2 = 0.85 is 0.7864\n"
]
}
],
"source": [
"print('The output of the network for x1 = 0.5 and x2 = 0.85 is {}'.format(np.around(a_2, decimals=4)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Obviously, neural networks for real problems are composed of many hidden layers and many more nodes in each layer. So, we can't continue making predictions using this very inefficient approach of computing the weighted sum at each node and the activation of each node manually. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to code an automatic way of making predictions, let's generalize our network. A general network would take $n$ inputs, would have many hidden layers, each hidden layer having $m$ nodes, and would have an output layer. Although the network is showing one hidden layer, but we will code the network to have many hidden layers. Similarly, although the network shows an output layer with one node, we will code the network to have more than one node in the output layer."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"http://cocl.us/general_neural_network\" alt=\"Neural Network General\" width=600px>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id=\"item2\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Initialize a Network"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's start by formally defining the structure of the network."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"n = 2 # number of inputs\n",
"num_hidden_layers = 2 # number of hidden layers\n",
"m = [2, 2] # number of nodes in each hidden layer\n",
"num_nodes_output = 1 # number of nodes in the output layer"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we defined the structure of the network, let's go ahead and inititailize the weights and the biases in the network to random numbers. In order to be able to initialize the weights and the biases to random numbers, we will need to import the **Numpy** library."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'layer_1': {'node_1': {'weights': array([0.58, 0.83]), 'bias': array([0.31])}, 'node_2': {'weights': array([0.74, 0.05]), 'bias': array([0.16])}}, 'layer_2': {'node_1': {'weights': array([0.11, 0.4 ]), 'bias': array([0.31])}, 'node_2': {'weights': array([0.96, 0.2 ]), 'bias': array([0.67])}}, 'output': {'node_1': {'weights': array([0.54, 0.07]), 'bias': array([0.75])}}}\n"
]
}
],
"source": [
"import numpy as np # import the Numpy library\n",
"\n",
"num_nodes_previous = n # number of nodes in the previous layer\n",
"\n",
"network = {} # initialize network an an empty dictionary\n",
"\n",
"# loop through each layer and randomly initialize the weights and biases associated with each node\n",
"# notice how we are adding 1 to the number of hidden layers in order to include the output layer\n",
"for layer in range(num_hidden_layers + 1): \n",
" \n",
" # determine name of layer\n",
" if layer == num_hidden_layers:\n",
" layer_name = 'output'\n",
" num_nodes = num_nodes_output\n",
" else:\n",
" layer_name = 'layer_{}'.format(layer + 1)\n",
" num_nodes = m[layer]\n",
" \n",
" # initialize weights and biases associated with each node in the current layer\n",
" network[layer_name] = {}\n",
" for node in range(num_nodes):\n",
" node_name = 'node_{}'.format(node+1)\n",
" network[layer_name][node_name] = {\n",
" 'weights': np.around(np.random.uniform(size=num_nodes_previous), decimals=2),\n",
" 'bias': np.around(np.random.uniform(size=1), decimals=2),\n",
" }\n",
" \n",
" num_nodes_previous = num_nodes\n",
" \n",
"print(network) # print network"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Awesome! So now with the above code, we are able to initialize the weights and the biases pertaining to any network of any number of hidden layers and number of nodes in each layer. But let's put this code in a function so that we are able to repetitively execute all this code whenever we want to construct a neural network.\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def initialize_network(num_inputs, num_hidden_layers, num_nodes_hidden, num_nodes_output):\n",
" \n",
" num_nodes_previous = num_inputs # number of nodes in the previous layer\n",
"\n",
" network = {}\n",
" \n",
" # loop through each layer and randomly initialize the weights and biases associated with each layer\n",
" for layer in range(num_hidden_layers + 1):\n",
" \n",
" if layer == num_hidden_layers:\n",
" layer_name = 'output' # name last layer in the network output\n",
" num_nodes = num_nodes_output\n",
" else:\n",
" layer_name = 'layer_{}'.format(layer + 1) # otherwise give the layer a number\n",
" num_nodes = num_nodes_hidden[layer] \n",
" \n",
" # initialize weights and bias for each node\n",
" network[layer_name] = {}\n",
" for node in range(num_nodes):\n",
" node_name = 'node_{}'.format(node+1)\n",
" network[layer_name][node_name] = {\n",
" 'weights': np.around(np.random.uniform(size=num_nodes_previous), decimals=2),\n",
" 'bias': np.around(np.random.uniform(size=1), decimals=2),\n",
" }\n",
" \n",
" num_nodes_previous = num_nodes\n",
"\n",
" return network # return the network"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Use the *initialize_network* function to create a network that:\n",
"\n",
"1. takes 5 inputs\n",
"2. has three hidden layers\n",
"3. has 3 nodes in the first layer, 2 nodes in the second layer, and 3 nodes in the third layer\n",
"4. has 1 node in the output layer\n",
"\n",
"Call the network **small_network**."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'layer_1': {'node_1': {'weights': array([0.42, 0.42, 0.46, 0.37, 0.47]), 'bias': array([0.04])}, 'node_2': {'weights': array([0.08, 0.73, 0.64, 0.03, 0.3 ]), 'bias': array([0.22])}, 'node_3': {'weights': array([0.06, 0.52, 0.42, 0.05, 0.57]), 'bias': array([0.8])}}, 'layer_2': {'node_1': {'weights': array([0.11, 0.28, 0.64]), 'bias': array([0.49])}, 'node_2': {'weights': array([0.51, 0.46, 0.89]), 'bias': array([0.61])}}, 'layer_3': {'node_1': {'weights': array([0.6 , 0.44]), 'bias': array([0.48])}, 'node_2': {'weights': array([0.89, 0.21]), 'bias': array([0.94])}, 'node_3': {'weights': array([0.07, 0.6 ]), 'bias': array([0.03])}}, 'output': {'node_1': {'weights': array([0.67, 0.64, 0.86]), 'bias': array([0.94])}}}\n"
]
}
],
"source": [
"### type your answer here\n",
"\n",
"small_network = initialize_network(num_inputs=5, num_hidden_layers=3, num_nodes_hidden=[3,2,3], num_nodes_output=1)\n",
"print(small_network)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click __here__ for the solution.\n",
"<!-- The correct answer is:\n",
"small_network = initialize_network(5, 3, [3, 2, 3], 1)\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id=\"item3\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compute Weighted Sum at Each Node"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The weighted sum at each node is computed as the dot product of the inputs and the weights plus the bias. So let's create a function called *compute_weighted_sum* that does just that."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def compute_weighted_sum(inputs, weights, bias):\n",
" return np.sum(inputs * weights) + bias"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's generate 5 inputs that we can feed to **small_network**."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The inputs to the network are [0.15 0.74 0.26 0.53 0.01]\n"
]
}
],
"source": [
"from random import seed\n",
"import numpy as np\n",
"\n",
"np.random.seed(12)\n",
"inputs = np.around(np.random.uniform(size=5), decimals=2)\n",
"\n",
"print('The inputs to the network are {}'.format(inputs))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Use the *compute_weighted_sum* function to compute the weighted sum at the first node in the first hidden layer."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Wighted sum of the first node in first layer is 0.7342000000000001\n"
]
}
],
"source": [
"### type your answer here\n",
"\n",
"weighted_sum=compute_weighted_sum(inputs, weights= small_network['layer_1']['node_1']['weights'], bias= small_network['layer_1']['node_1']['bias'])\n",
"\n",
"print('Weighted sum of the first node in first layer is {}'.format(weighted_sum[0]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click __here__ for the solution.\n",
"<!-- The correct answer is:\n",
"node_weights = small_network['layer_1']['node_1']['weights']\n",
"node_bias = small_network['layer_1']['node_1']['bias']\n",
"\n",
"weighted_sum = compute_weighted_sum(inputs, node_weights, node_bias)\n",
"print('The weighted sum at the first node in the hidden layer is {}'.format(np.around(weighted_sum[0], decimals=4)))\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id=\"item4\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compute Node Activation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Recall that the output of each node is simply a non-linear tranformation of the weighted sum. We use activation functions for this mapping. Let's use the sigmoid function as the activation function here. So let's define a function that takes a weighted sum as input and returns the non-linear transformation of the input using the sigmoid function."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"def node_activation(weighted_sum):\n",
" return 1.0 / (1.0 + np.exp(-1 * weighted_sum))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Use the *node_activation* function to compute the output of the first node in the first hidden layer."
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The output of the first node in the hidden layer is 0.6757\n"
]
},
{
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"### type your answer here\n",
"\n",
"node_output=node_activation(weighted_sum)\n",
"print('The output of the first node in the hidden layer is {}'.format(np.around(node_output[0], decimals=4)))\n",
"type(node_output)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click __here__ for the solution.\n",
"<!-- The correct answer is:\n",
"node_output = node_activation(compute_weighted_sum(inputs, node_weights, node_bias))\n",
"print('The output of the first node in the hidden layer is {}'.format(np.around(node_output[0], decimals=4)))\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id=\"item5\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Forward Propagation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The final piece of building a neural network that can perform predictions is to put everything together. So let's create a function that applies the *compute_weighted_sum* and *node_activation* functions to each node in the network and propagates the data all the way to the output layer and outputs a prediction for each node in the output layer."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The way we are going to accomplish this is through the following procedure:\n",
"\n",
"1. Start with the input layer as the input to the first hidden layer.\n",
"2. Compute the weighted sum at the nodes of the current layer.\n",
"3. Compute the output of the nodes of the current layer.\n",
"4. Set the output of the current layer to be the input to the next layer.\n",
"5. Move to the next layer in the network.\n",
"5. Repeat steps 2 - 4 until we compute the output of the output layer."
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"def forward_propagate(network, inputs):\n",
" \n",
" layer_inputs = list(inputs) # start with the input layer as the input to the first hidden layer\n",
" \n",
" for layer in network:\n",
" \n",
" layer_data = network[layer]\n",
" \n",
" layer_outputs = [] \n",
" for layer_node in layer_data:\n",
" \n",
" node_data = layer_data[layer_node]\n",
" \n",
" # compute the weighted sum and the output of each node at the same time \n",
" node_output = node_activation(compute_weighted_sum(layer_inputs, node_data['weights'], node_data['bias']))\n",
" layer_outputs.append(np.around(node_output[0], decimals=4))\n",
" \n",
" if layer != 'output':\n",
" print('The outputs of the nodes in hidden layer number {} is {}'.format(layer.split('_')[1], layer_outputs))\n",
" \n",
" layer_inputs = layer_outputs # set the output of this layer to be the input to next layer\n",
" \n",
" network_predictions = layer_outputs\n",
" return network_predictions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Use the *forward_propagate* function to compute the prediction of our small network"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The outputs of the nodes in hidden layer number 1 is [0.6757, 0.7226, 0.7917]\n",
"The outputs of the nodes in hidden layer number 2 is [0.7813, 0.8799]\n",
"The outputs of the nodes in hidden layer number 3 is [0.7918, 0.8606, 0.6485]\n",
"Prediction is 0.9295\n"
]
}
],
"source": [
"### type your answser here\n",
"predictions=forward_propagate(small_network, inputs)\n",
"\n",
"print('Prediction is {}'.format(np.around(predictions[0], decimals=4)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Double-click __here__ for the solution.\n",
"<!-- The correct answer is:\n",
"predictions = forward_propagate(small_network, inputs)\n",
"print('The predicted value by the network for the given input is {}'.format(np.around(predictions[0], decimals=4)))\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So we built the code to define a neural network. We can specify the number of inputs that a neural network can take, the number of hidden layers as well as the number of nodes in each hidden layer, and the number of nodes in the output layer."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We first use the *initialize_network* to create our neural network and define its weights and biases."
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"my_network = initialize_network(5, 3, [2, 3, 2], 3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then, for a given input,"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"inputs = np.around(np.random.uniform(size=5), decimals=2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"we compute the network predictions."
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The outputs of the nodes in hidden layer number 1 is [0.8355, 0.929]\n",
"The outputs of the nodes in hidden layer number 2 is [0.8527, 0.7297, 0.8413]\n",
"The outputs of the nodes in hidden layer number 3 is [0.8697, 0.7379]\n",
"The predicted values by the network for the given input are [0.7928, 0.781, 0.7255]\n"
]
}
],
"source": [
"predictions = forward_propagate(my_network, inputs)\n",
"print('The predicted values by the network for the given input are {}'.format(predictions))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Feel free to play around with the code by creating different networks of different structures and enjoy making predictions using the *forward_propagate* function."
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The outputs of the nodes in hidden layer number 1 is [0.9898, 0.9893, 0.9831, 0.9925, 0.9818]\n",
"The outputs of the nodes in hidden layer number 2 is [0.9199, 0.9712, 0.974, 0.9649, 0.9498]\n",
"The outputs of the nodes in hidden layer number 3 is [0.9734, 0.9697, 0.973, 0.9376, 0.9374]\n",
"The outputs of the nodes in hidden layer number 4 is [0.9445, 0.9453, 0.9655, 0.9374, 0.9229]\n",
"The outputs of the nodes in hidden layer number 5 is [0.9513, 0.9675, 0.9774, 0.9276, 0.8963]\n",
"The predicted values by the network for the given input are [0.9557, 0.8164, 0.9517, 0.9081]\n"
]
}
],
"source": [
"### create a network\n",
"xing_network1=initialize_network(12,5, [5,5,5,5,5], 4)\n",
"input=np.around(np.random.uniform(size=12),decimals=2)\n",
"predictions=forward_propagate(xing_network1, input)\n",
"print('The predicted values by the network for the given input are {}'.format(predictions))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The outputs of the nodes in hidden layer number 1 is [0.8856, 0.7136, 0.8157, 0.8466, 0.8856]\n",
"The outputs of the nodes in hidden layer number 2 is [0.9602, 0.9472, 0.9209, 0.9127, 0.9503, 0.9337]\n",
"The outputs of the nodes in hidden layer number 3 is [0.982, 0.9038, 0.9812, 0.9448, 0.9229, 0.9551]\n",
"The outputs of the nodes in hidden layer number 4 is [0.9836, 0.8574, 0.9324, 0.954, 0.9584, 0.948]\n",
"The outputs of the nodes in hidden layer number 5 is [0.9314, 0.9582, 0.9808, 0.9643, 0.9556, 0.9731]\n",
"The outputs of the nodes in hidden layer number 6 is [0.9635, 0.9044, 0.9676, 0.9754, 0.944, 0.9572, 0.9812, 0.8849]\n",
"The outputs of the nodes in hidden layer number 7 is [0.9944, 0.9939, 0.9908, 0.9915, 0.9949, 0.9623, 0.9741, 0.9863]\n",
"The outputs of the nodes in hidden layer number 8 is [0.9725, 0.9887, 0.9873, 0.9724, 0.971]\n",
"The predicted values by the network for the given input are [0.8356, 0.9568, 0.9203, 0.9771]\n"
]
},
{
"data": {
"text/plain": [
"0.5664790559676278"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"### create another network\n",
"\n",
"xing_network2=initialize_network(6, 8, [5,6,6,6,6,8,8,5], 4)\n",
"input=np.around(np.random.uniform(size=6),decimals=2)\n",
"predictions=forward_propagate(xing_network2, input)\n",
"print('The predicted values by the network for the given input are {}'.format(predictions))\n",
"\n",
"1/(1+np.exp(-0.2675))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Thank you for completing this lab!\n",
"\n",
"This notebook was created by [Alex Aklson](https://www.linkedin.com/in/aklson/). I hope you found this lab interesting and educational. Feel free to contact me if you have any questions!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook is part of a course on **edX** called *Deep Learning Fundamentals with Keras*. If you accessed this notebook outside the course, you can take this course online by clicking [here](http://cocl.us/DL0101EN_edX_Week1_LAB1)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"\n",
"Copyright &copy; 2018 [IBM Developer Skills Network](https://cognitiveclass.ai/?utm_source=bducopyrightlink&utm_medium=dswb&utm_campaign=bdu). This notebook and its source code are released under the terms of the [MIT License](https://bigdatauniversity.com/mit-license/)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment