-
Notifications
You must be signed in to change notification settings - Fork 0
/
bikesharing_nn.py
165 lines (118 loc) · 5.81 KB
/
bikesharing_nn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import numpy as np
class NeuralNetwork(object):
def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate):
# Set number of nodes in input, hidden and output layers.
self.input_nodes = input_nodes
self.hidden_nodes = hidden_nodes
self.output_nodes = output_nodes
# Initialize weights
self.weights_input_to_hidden = np.random.normal(0.0, self.input_nodes**-0.5,
(self.input_nodes, self.hidden_nodes))
self.weights_hidden_to_output = np.random.normal(0.0, self.hidden_nodes**-0.5,
(self.hidden_nodes, self.output_nodes))
# Save learning rate
self.lr = learning_rate
# Set the Activation function using lambda expression
self.activation_function = lambda x : 1 / (1 + np.exp(-x))
def print_nn(self):
# Print some useful information
print('---------------------------------------------------------------')
print('Bikesharing NeuralNetwork')
print('---------------------------------------------------------------')
print('')
print('Nodes Input Hidden Output')
print(" [{}] ======> [{}] ======> [{}]".format(
self.input_nodes,
self.hidden_nodes,
self.output_nodes))
print("Weights Wi Wo")
print(" {} {}".format(
self.weights_input_to_hidden.shape,
self.weights_hidden_to_output.shape))
print("")
def train(self, features, targets):
''' Train the network on batch of features and targets.
Arguments
---------
features: 2D array, each row is one data record, each column is a feature
targets: 1D array of target values
'''
n_records = features.shape[0]
# Clear all the weights
delta_weights_i_h = np.zeros(self.weights_input_to_hidden.shape)
delta_weights_h_o = np.zeros(self.weights_hidden_to_output.shape)
for X, y in zip(features, targets):
# Implement the forward pass function below
final_outputs, hidden_outputs = self.forward_pass_train(X)
# Implement the backproagation function below
delta_weights_i_h, delta_weights_h_o = self.backpropagation(
final_outputs,
hidden_outputs,
X,
y,
delta_weights_i_h,
delta_weights_h_o)
self.update_weights(delta_weights_i_h, delta_weights_h_o, n_records)
def forward_pass_train(self, X):
''' Implement forward pass here
Arguments
---------
X: features batch
'''
### Forward pass ###
# Hidden layer
# X [ n_records(always 128) x N_i ] * weights_input_to_hidden [ N_i x hidden_nodes ]
hidden_inputs = np.dot(X, self.weights_input_to_hidden)
hidden_outputs = self.activation_function(hidden_inputs)
# Output layer
# hidden_outputs [ n_records(always 128) x hidden_nodes ] * weights_hidden_to_output [ hidden_nodes x 1 ]
final_inputs = np.dot(hidden_outputs, self.weights_hidden_to_output)
final_outputs = final_inputs
return final_outputs, hidden_outputs
def backpropagation(self, final_outputs, hidden_outputs, X, y, delta_weights_i_h, delta_weights_h_o):
''' Implement backpropagation
Arguments
---------
final_outputs: output from forward pass
y: target (i.e. label) batch
delta_weights_i_h: change in weights from input to hidden layers
delta_weights_h_o: change in weights from hidden to output layers
'''
### Backward pass ###
# Output error: difference between desired target and actual output
error_term = y - final_outputs
# Hidden layer's contribution to the error
hidden_error = np.dot(self.weights_hidden_to_output, error_term)
# Backpropagated error terms
hidden_error_term = hidden_error * hidden_outputs * (1 - hidden_outputs)
# Weight step (input to hidden) 56,18
delta_weights_i_h += np.dot(X[:, None], hidden_error_term[None, :])
# Weight step (hidden to output)
delta_weights_h_o += np.dot(hidden_outputs[:, None], error_term[None, :])
return delta_weights_i_h, delta_weights_h_o
def update_weights(self, delta_weights_i_h, delta_weights_h_o, n_records):
''' Update weights on gradient descent step
Arguments
---------
delta_weights_i_h: change in weights from input to hidden layers
delta_weights_h_o: change in weights from hidden to output layers
n_records: number of records
'''
# update hidden-to-output weights with gradient descent step
self.weights_hidden_to_output += self.lr * delta_weights_h_o/n_records
# update input-to-hidden weights with gradient descent step
self.weights_input_to_hidden += self.lr * delta_weights_i_h/n_records
def run(self, features):
''' Run a forward pass through the network with input features
Arguments
---------
features: 1D array of feature values
'''
# Forward pass
# Hidden layer
hidden_inputs = np.dot(features, self.weights_input_to_hidden)
hidden_outputs = self.activation_function(hidden_inputs)
# Output layer
final_inputs = np.dot(hidden_outputs, self.weights_hidden_to_output)
final_outputs = final_inputs
return final_outputs