Skip to content

Instantly share code, notes, and snippets.

@reginaldojunior
Created June 25, 2021 04:45
Show Gist options
  • Save reginaldojunior/57bf9edf9bf104d88e64e7af210d81b3 to your computer and use it in GitHub Desktop.
Save reginaldojunior/57bf9edf9bf104d88e64e7af210d81b3 to your computer and use it in GitHub Desktop.
feedforward.py
from matrix import Matrix
import math
class RedeNeural():
nodes_input = []
nodes_hidden = []
nodes_output = []
bias_in_to_hidden = []
bias_hidden_to_output = []
weight_in_to_hidden = []
weight_in_to_output = []
learn_rate = 0.5
def __init__(self, nodes_input, nodes_hidden, nodes_output):
self.nodes_input = Matrixx().create_matrix(nodes_input, nodes_hidden)
self.nodes_hidden = Matrixx().create_matrix(nodes_hidden, nodes_output)
self.nodes_output = Matrixx().create_matrix(nodes_output, 1)
self.bias_in_to_hidden = Matrixx().create_matrix(nodes_input, nodes_hidden)
self.bias_hidden_to_output = Matrixx().create_matrix(nodes_hidden, nodes_output)
def sigmoid(self, x):
return 1 / (1 + math.exp(-x))
def feedforward(self, input):
input = Matrixx().array_to_matrix(input)
hidden = input.multiply(self.nodes_hidden, input)
hidden = input.add(hidden, self.bias_in_to_hidden, True)
hidden = list(map(lambda i: list(map(lambda j: self.sigmoid(j), i)), hidden))
hidden = Matrixx().array_to_matrix(hidden)
output = input.multiply(self.nodes_output, hidden)
output.data = list(output.data)[0]
output = input.add_output(output, self.bias_hidden_to_output, True)
output = list(output[0][0])
output = map(lambda i: self.sigmoid(i), output)
print(output)
r = RedeNeural(1, 2, 1)
r.feedforward([1, 2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment