your_first_nn

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
import numpy as np 


class NeuralNetwork(object):
def __init__(self, x, y):
self.input = x
self.weight1 = np.random.rand(self.input.shape[1],4)
self.weight2 = np.random.rand(4,1)
self.y = y
self.output = np.zeros(self.y.shape)

def feedforward(self):
self.layer1 = sigmoid(np.dot(self.input, self.weight1))
self.layer2 = sigmoid(np.dot(self.layer1, self.weight2))

def backprop(self):
d_weight2 = np.dot(self.layer1.T, (2*(self.y - self.output)))
d_weight1 = np.dot(self.input.T,
(np.dot(2*(self.y - self.output)*sigmoid_derivative(self.output), self.weight2.T)*sigmoid_derivative(self.layer1)))
self.weight1 += d_weight1
self.weight2 += d_weight2

def sigmoid_derivative(x):
return x*(1-x)

def sigmoid(z):
return 1.0/(1.0+math.exp(-z))