Neural Network
Neural Network
A neural network is loosely based on how the human brain works: many neurons
connected to other neurons, passing information through their connections and firing
when the input to a neuron surpasses a certain threshold. Our artificial neural network
will consist of artificial neurons and synapses with information being passed between
them. The synapses, or connections, will be weighted according to the neuron’s strength
optimization process called backpropagation. For each iteration during the training
process, backpropagation will be used to go back through the layers of the network and
adjusts the weights according to their contribution to the neural net’s error.
Neural networks are essentially self-optimizing functions that map inputs to the correct
outputs. We can then place a new input into the function, where it will predict an output
This neural network, like all neural networks, will have to learn what the important
features are in the data to produce the output. In particular, this neural net will be given
an input matrix with six samples, each with three feature columns consisting of solely
zeros and ones. For example, one sample in the training set may be [0, 1, 1]. The output
to each sample will be a single one or zero. The output will be determined by the number
in the first feature column of the data samples. Using the example given before, the
output for [0, 1, 1] would be 0, because the first column contains a zero. An example
chart will be given below to demonstrate the output for each input sample.
No matter which method you choose, working with a neural network to make a
prediction is essentially the same:
1. Import the libraries. For example: import numpy as np
2. Define/create input data. For example, use numpy to create a dataset and an
array of data values.
3. Add weights and bias (if applicable) to input features. These are learnable
parameters, meaning that they can be adjusted during training.
Weights = input parameters that influences output
Bias = an extra threshold value added to the output
4. Train the network against known, good data in order to find the correct values
for the weights and biases.
5. Test the Network against a set of test data to see how it performs.
6. Fit the model with hyperparameters (parameters whose values are used to
control the learning process), calculate accuracy, and make a prediction.
Create a Neural Network from Scratch
In this example, I’ll use Python code and the numpy and scipy libraries to create a
simple neural network with two nodes.
# Import python libraries required in this example:
import numpy as np
# normal distribution:
return truncnorm(
class Nnetwork:
def __init__(self,
no_of_in_nodes,
no_of_out_nodes,
no_of_hidden_nodes,
learning_rate):
self.no_of_in_nodes = no_of_in_nodes
self.no_of_out_nodes = no_of_out_nodes
self.no_of_hidden_nodes = no_of_hidden_nodes
self.learning_rate = learning_rate
self.create_weight_matrices()
def create_weight_matrices(self):
rad = 1 / np.sqrt(self.no_of_in_nodes)
X = truncated_normal(mean=0, sd=1, low=-rad, upp=rad)
self.weights_in_hidden = X.rvs((self.no_of_hidden_nodes,
self.no_of_in_nodes))
rad = 1 / np.sqrt(self.no_of_hidden_nodes)
self.weights_hidden_out = X.rvs((self.no_of_out_nodes,
self.no_of_hidden_nodes))
"""
"""
return output_vector
simple_network = Nnetwork(no_of_in_nodes=2,
no_of_out_nodes=2,
no_of_hidden_nodes=4,
learning_rate=0.6)
# Run simple_network for arrays, lists and tuples with shape (2):
simple_network.run([(3, 4)])
neural
network