Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- class NeuralNetwork:
- def __init__(self, x, y):
- self.input = x
- self.weights1 = np.random.rand(self.input.shape[1], 4)
- self.weights2 = 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.weights1))
- self.output = sigmoid(np.dot(self.layer1, self.weights2))
- def backprop(self):
- 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)))
- self.weights1 += learning_rate*d_weights1
- self.weights2 += learning_rate*d_weights2
- def train(self):
- self.output = self.feedforward()
- self.backprop()
- def sigmoid(t): return 1/(1+np.exp(-t))
- def sigmoid_derivative(p): return p*(1-p)
- def loss(y, y_out):
- return np.mean(np.square(y - y_out))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement