-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralNetwork.py
31 lines (24 loc) · 1.13 KB
/
NeuralNetwork.py
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
28
29
30
31
import numpy as np
class NeuralNetwork():
def __init__(self, input_size):
np.random.seed(1)
self.synaptic_weights = 2 * np.random.random((input_size, 1)) - 1
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(self, x):
return x * (1 - x)
def train(self, training_inputs, training_outputs, training_iterations):
for iteration in range(training_iterations):
# Pass training set through the neural network
output = self.think(training_inputs)
# Calculate the error rate
error = training_outputs - output
# Multiply error by input and gradient of the sigmoid function
# Less confident weights are adjusted more through the nature of the function
adjustments = np.dot(training_inputs.T, error * self.sigmoid_derivative(output))
# Adjust synaptic weights
self.synaptic_weights += adjustments
def think(self, inputs):
inputs = inputs.astype(float)
output = self.sigmoid(np.dot(inputs, self.synaptic_weights))
return output