Skip to content

Instantly share code, notes, and snippets.

@afonsomatos
Created January 30, 2019 22:17
Show Gist options
  • Save afonsomatos/6ad3cff91f4dfb995074625c871a053b to your computer and use it in GitHub Desktop.
Save afonsomatos/6ad3cff91f4dfb995074625c871a053b to your computer and use it in GitHub Desktop.
python neural network
import numpy
from scipy.special import expit as sigmoid
class neural_network:
def __init__(self, nodes, learning_rate):
self.nodes = nodes
self.layers = len(nodes)
self.lr = learning_rate
self.weights = []
for i in range(self.layers - 1):
array = numpy.random.normal(0.0, pow(nodes[i + 1], -0.5), (nodes[i + 1], nodes[i]))
self.weights.append(array)
def train(self, inputs, targets):
targets = numpy.array(targets, ndmin=2).T
result = self.query(inputs)
error = targets - result[-1]
for i in range(1, self.layers):
previous = result[-i - 1]
z = result[-i]
delta = self.lr * numpy.dot(error * z * (1.0 - z), previous.T)
# next layer's error
error = numpy.dot(self.weights[-i].T, error)
self.weights[-i] += delta
def query(self, inputs):
output = numpy.array(inputs, ndmin=2).T
result = [output]
for w in self.weights:
output = sigmoid(numpy.dot(w, output))
result.append(output)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment