Your First Neural Network
Your First Neural Network
(https://twitter.com/FishBerhane)
(https://scholar.google.com/citations?user=zBPe3MkAAAAJ&hl=en)
Home (../index.html)
Education (../education.html)
Publications (../publications.html)
SQL
Hadoop (../Hadoop/hadoop.html) Miscellaneous
(../Miscellaneous/miscellaneous.html)
%matplotlib inline
%load_ext autoreload
%autoreload 2
import numpy as np
import pandas as pd
data_path = 'Bike-Sharing-Dataset/hour.csv'
rides = pd.read_csv(data_path)
https://datascience-enthusiast.com/DL/bike_prediction_nn.html 1/15
9/7/22, 9:45 PM Your_first_neural_network
rides.head()
2011-
0 1 1 0 1 0 0 6 0 1 0.
01-01
2011-
1 2 1 0 1 1 0 6 0 1 0.
01-01
2011-
2 3 1 0 1 2 0 6 0 1 0.
01-01
2011-
3 4 1 0 1 3 0 6 0 1 0.
01-01
2011-
4 5 1 0 1 4 0 6 0 1 0.
01-01
Below is a plot showing the number of bike riders over the first 10 days or so in the data set. (Some days
don't have exactly 24 entries in the data set, so it's not exactly 10 days.) You can see the hourly rentals here.
This data is pretty complicated! The weekends have lower over all ridership and there are spikes when
people are biking to and from work during the week. Looking at the data above, we also have information
about temperature, humidity, and windspeed, all of these likely affecting the number of riders. You'll be trying
to capture all this with your model.
rides[:24*10].plot(x='dteday', y='cnt')
<matplotlib.axes._subplots.AxesSubplot at 0x8a3f2b0>
https://datascience-enthusiast.com/DL/bike_prediction_nn.html 2/15
9/7/22, 9:45 PM Your_first_neural_network
Dummy variables
Here we have some categorical variables like season, weather, month. To include these in our model, we'll
need to make binary dummy variables. This is simple to do with Pandas thanks to get_dummies().
data.head()
yr holiday temp hum windspeed casual registered cnt season_1 season_2 ...
5 rows × 59 columns
The scaling factors are saved so we can go backwards when we use the network for predictions.
scaled_features = {}
for each in quant_features:
https://datascience-enthusiast.com/DL/bike_prediction_nn.html 3/15
9/7/22, 9:45 PM Your_first_neural_network
test_data = data[-21*24:]
data = data[:-21*24]
We'll split the data into two sets, one for training and one for validating as the network is being trained. Since
this is time series data, we'll train on historical data, then try to predict on future data (the validation set).
# Hold out the last 60 days or so of the remaining data as a validation set
https://datascience-enthusiast.com/DL/bike_prediction_nn.html 4/15
9/7/22, 9:45 PM Your_first_neural_network
The network has two layers, a hidden layer and an output layer. The hidden layer will use the sigmoid
function for activations. The output layer has only one node and is used for the regression, the output of the
node is the same as the input of the node. That is, the activation function is f (x) = x . A function that takes
the input signal and generates an output signal, but takes into account the threshold, is called an activation
function. We work through each layer of our network calculating the outputs for each neuron. All of the
outputs from one layer become inputs to the neurons on the next layer. This process is called forward
propagation.
We use the weights to propagate signals forward from the input to the output layers in a neural network. We
use the weights to also propagate error backwards from the output back into the network to update our
weights. This is called backpropagation.
Hint: You'll need the derivative of the output activation function (f (x) = x ) for the
backpropagation implementation. If you aren't familiar with calculus, this function is equivalent
to the equation y = x . What is the slope of that equation? That is the derivative of f (x) .
1. Implement the sigmoid function to use as the activation function. Set self.activation_function
in __init__ to your sigmoid function.
2. Implement the forward pass in the train method.
3. Implement the backpropagation algorithm in the train method, including calculating the output
error.
4. Implement the forward pass in the run method.
https://datascience-enthusiast.com/DL/bike_prediction_nn.html 5/15
9/7/22, 9:45 PM Your_first_neural_network
class NeuralNetwork(object):
self.input_nodes = input_nodes
self.hidden_nodes = hidden_nodes
self.output_nodes = output_nodes
# Initialize weights
(self.input_nodes, self.hidden_nodes))
(self.hidden_nodes, self.output_nodes))
self.lr = learning_rate
# as shown below.
### If the lambda code above is not something you're familiar with,
# You can uncomment out the following three lines and put your
def sigmoid(x):
self.activation_function = sigmoid
Arguments
---------
features: 2D array, each row is one data record, each column is a feature
'''
n_records = features.shape[0]
delta_weights_i_h = np.zeros(self.weights_input_to_hidden.shape)
delta_weights_h_o = np.zeros(self.weights_hidden_to_output.shape)
delta_weights_i_
h, delta_weights_h_o)
https://datascience-enthusiast.com/DL/bike_prediction_nn.html 6/15
9/7/22, 9:45 PM Your_first_neural_network
Arguments
---------
X: features batch
'''
final_outputs = final_inputs
Arguments
---------
'''
# TODO: Backpropagated error terms - Replace these values with your calculation
s.
output_error_term = error
Arguments
---------
https://datascience-enthusiast.com/DL/bike_prediction_nn.html 7/15
9/7/22, 9:45 PM Your_first_neural_network
'''
''' Run a forward pass through the network with input features
Arguments
---------
'''
# TODO: Hidden layer - replace these values with the appropriate calculations.
# TODO: Output layer - Replace these values with the appropriate calculations.
return final_outputs
#########################################################
##########################################################
iterations = 10000
learning_rate = 0.5
hidden_nodes = 19
output_nodes = 1
return np.mean((y-Y)**2)
Unit tests
Run these unit tests to check the correctness of your network implementation. This will help you be sure your
network was implemented correctly befor you starting trying to train it. These tests must all be successful to
pass the project.
https://datascience-enthusiast.com/DL/bike_prediction_nn.html 8/15
9/7/22, 9:45 PM Your_first_neural_network
import unittest
targets = np.array([[0.4]])
[0.4, 0.5],
[-0.3, 0.2]])
test_w_h_o = np.array([[0.3],
[-0.1]])
class TestMethods(unittest.TestCase):
##########
##########
def test_data_path(self):
self.assertTrue(data_path.lower() == 'bike-sharing-dataset/hour.csv')
def test_data_loaded(self):
self.assertTrue(isinstance(rides, pd.DataFrame))
##########
##########
def test_activation(self):
self.assertTrue(np.all(network.activation_function(0.5) == 1/(1+np.exp(-0.5))))
def test_train(self):
network.weights_input_to_hidden = test_w_i_h.copy()
network.weights_hidden_to_output = test_w_h_o.copy()
network.train(inputs, targets)
self.assertTrue(np.allclose(network.weights_hidden_to_output,
np.array([[ 0.37275328],
[-0.03172939]])))
self.assertTrue(np.allclose(network.weights_input_to_hidden,
[0.39775194, 0.50074398],
[-0.29887597, 0.19962801]])))
def test_run(self):
network.weights_input_to_hidden = test_w_i_h.copy()
network.weights_hidden_to_output = test_w_h_o.copy()
self.assertTrue(np.allclose(network.run(inputs), 0.09998924))
suite = unittest.TestLoader().loadTestsFromModule(TestMethods())
unittest.TextTestRunner().run(suite)
https://datascience-enthusiast.com/DL/bike_prediction_nn.html 9/15
9/7/22, 9:45 PM Your_first_neural_network
.....
----------------------------------------------------------------------
OK
You'll also be using a method know as Stochastic Gradient Descent (SGD) to train the network. The idea is
that for each training pass, you grab a random sample of the data instead of using the whole data set. You
use many more training passes than with normal gradient descent, but each pass is much faster. This ends
up training the network more efficiently. You'll learn more about SGD later.
Try a few different numbers and see how it affects the performance. You can look at the losses dictionary for
a metric of the network performance. If the number of hidden units is too low, then the model won't have
enough space to learn and if it is too high there are too many options for the direction that the learning can
take. The trick here is to find the right balance in number of hidden units you choose. You'll generally find that
the best number of hidden nodes to use ends up being between the number of input and output nodes.
https://datascience-enthusiast.com/DL/bike_prediction_nn.html 10/15
9/7/22, 9:45 PM Your_first_neural_network
import sys
####################
####################
N_i = train_features.shape[1]
for ii in range(iterations):
# Go through a random batch of 128 records from the training data set
X, y = train_features.ix[batch].values, train_targets.ix[batch]['cnt']
network.train(X, y)
sys.stdout.flush()
losses['train'].append(train_loss)
losses['validation'].append(val_loss)
Progress: 0.1% ... Training loss: 3.941 ... Validation loss: 5.640
C:\Users\fberhane\AppData\Local\Continuum\anaconda3\lib\site-packages\ipyke
rnel\__main__.py:17: DeprecationWarning:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-dep
recated (http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-index
er-is-deprecated)
Progress: 100.0% ... Training loss: 0.051 ... Validation loss: 0.151
https://datascience-enthusiast.com/DL/bike_prediction_nn.html 11/15
9/7/22, 9:45 PM Your_first_neural_network
plt.legend()
_ = plt.ylim()
fig, ax = plt.subplots(figsize=(8,4))
ax.plot(predictions[0], label='Prediction')
ax.set_xlim(right=len(predictions))
ax.legend()
dates = pd.to_datetime(rides.ix[test_data.index]['dteday'])
ax.set_xticks(np.arange(len(dates))[12::24])
_ = ax.set_xticklabels(dates[12::24], rotation=45)
https://datascience-enthusiast.com/DL/bike_prediction_nn.html 12/15
9/7/22, 9:45 PM Your_first_neural_network
ALSO ON HTTP://DATASCIENCE-ENTHUSIAST.COM
Recently, I took hands-on, Face Recognition for the For Kmeans clustering to This is th
performance-based Happy House¶ Welcome to work well, the following on the S
certification for Spark on … the first assignment of … assumptions have to hold … DataFram
https://datascience-enthusiast.com/DL/bike_prediction_nn.html 13/15
9/7/22, 9:45 PM Your_first_neural_network
Sponsored
Suami ilustrasikan kehidupan sehari-hari bersama istri dalam 22 gambar, usahakan jangan
menangis!
5minstory.com
Exotic Bra and Panty Sets to Boost Your Confidence (Take a Look)
Bra and Panty Sets
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
https://datascience-enthusiast.com/DL/bike_prediction_nn.html 14/15
9/7/22, 9:45 PM Your_first_neural_network
Sponsored
The Hidden Secret for Making Men Look Slim Is Finally Out!
The Super Shaper
https://datascience-enthusiast.com/DL/bike_prediction_nn.html 15/15