Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import numpy as np
- def sigmoid(x):
- return 1.0/(1+ np.exp(-x))
- def sigmoid_derivative(x):
- return x * (1.0 - x)
- class NeuralNetwork:
- def __init__(self, x, y):
- self.input = x
- self.weights1 = np.random.rand(self.input.shape[1],3)
- self.weights2 = np.random.rand(3,1)
- self.y = y
- self.output = np.zeros(self.y.shape)
- def feedforward(self):
- self.layer1 = sigmoid(np.dot(self.input, self.weights1))
- self.output = sigmoid(np.dot(self.layer1, self.weights2))
- def backprop(self):
- # application of the chain rule to find derivative of the loss function with respect to weights2 and weights1
- d_weights2 = np.dot(self.layer1.T, (2*(self.y - self.output) * sigmoid_derivative(self.output)))
- d_weights1 = np.dot(self.input.T, (np.dot(2*(self.y - self.output) * sigmoid_derivative(self.output), self.weights2.T) * sigmoid_derivative(self.layer1)))
- # update the weights with the derivative (slope) of the loss function
- self.weights1 += d_weights1
- self.weights2 += d_weights2
- if __name__ == "__main__":
- X = np.array([[1/31,1/24,15/60],
- [1/31,1/24,30/60],
- [1/31,1/24,45/60],
- [1/31,1/24,60/60]])
- y = np.array([[15521.7/15534.1],[15534.1/15534.1],[15404.4/15534.1],[15196.3/15534.1]])
- nn = NeuralNetwork(X,y)
- for i in range(10000):
- nn.feedforward()
- nn.backprop()
- print(nn.output)
- print(X)
- #g=np.linspace(0.15,1,4)
- #plt.plot(g,y)
- #plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement