Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- trg_inputs = np.array([[0,0,1], [1,1,1],[1,0,1],[0,1,1]])
- trg_outputs = np.array([[0,1,1,0]]).T
- # single layer
- class NeuralNetwork:
- def __init__(self):
- np.random.seed(1)
- self.synaptic_weights = 2 * np.random.random((3,1)) - 1 #the randomiser gives values between 0 and 1
- def __sigmoid(self, x):
- return 1/(1+np.exp(-x))
- def __sigmoid_derivative(self, x):
- return x*(1-x)
- def train(self, trg_inputs, trg_outputs,iter_n):
- for iteration in range(iter_n):
- raw, output = self.predict(trg_inputs)
- error = trg_outputs - output
- adj = np.dot(trg_inputs.T , error * self.__sigmoid_derivative(raw))
- self.synaptic_weights += adj
- def predict(self,inputs):
- raw = np.dot(inputs, self.synaptic_weights)
- return raw, self.__sigmoid(raw)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement