Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/backpropogation.cpython-37.pyc
Binary file not shown.
Binary file added __pycache__/network.cpython-37.pyc
Binary file not shown.
21 changes: 14 additions & 7 deletions backpropogation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def sigmoid(z):

def sigmoid_prime(z):
return sigmoid(z)*(1-sigmoid(z))

#The backpropogation function
def backprop(net, x, y):
'''
Expand All @@ -20,18 +21,24 @@ def backprop(net, x, y):
activations = [x]
zs = []
for b, w in zip(net.biases, net.weights):
z = np.dot(w, activation)+b
z = np.dot(w, activation)+b`
zs.append(z)
activation = sigmoid(z)
activation = sigmoid(z) #calculating activations for the given layer
activations.append(activation)
delta = net.cost_derivative(activations[-1], y) * \
sigmoid_prime(zs[-1])
nabla_b[-1] = delta
nabla_w[-1] = np.dot(delta, activations[-2].transpose())
delta = net.cost_derivative(activations[-1], y) *(sigmoid_prime(zs[-1])) #calculating delta for the output layer
nabla_b[-1] = delta #calculating delta[b] for output layer
nabla_w[-1] = np.dot(delta, activations[-2].transpose()) #calculating delta[w] for output layers


"""
Similarly calculating
delta[b],delta[w] for other hidden layers

"""
for l in range(2, net.num_layers):
z = zs[-l]
sp = sigmoid_prime(z)
delta = np.dot(net.weights[-l+1].transpose(), delta) * sp
nabla_b[-l] = delta
nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
return (net,nabla_b, nabla_w)
return (net,nabla_b, nabla_w) #returning network, delta[b],delta[w] for the whole network
6 changes: 3 additions & 3 deletions network.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def save(self, filename):
data = {"sizes": self.sizes,
"weights": [w.tolist() for w in self.weights],
"biases": [b.tolist() for b in self.biases]}
f = open(filename, "w")
json.dump(data, f)
f.close()
f = open(filename, "w")
json.dump(data, f)
f.close()

def load(filename):
f = open(filename, "r")
Expand Down