genetics code (untested)
This commit is contained in:
18
brain.py
18
brain.py
@@ -1,17 +1,25 @@
|
||||
import numpy as np
|
||||
import random
|
||||
|
||||
def mat_mult(A,B):
|
||||
return [[sum([A[i][m]*B[m][j] for m in range(len(A[0]))]) for j in range(len(B[0]))] for i in range(len(A))]
|
||||
|
||||
class Neural_Network(object):
|
||||
# inspired from https://enlight.nyc/projects/neural-network/
|
||||
def __init__(self):
|
||||
def __init__(self, W1=None, W2=None):
|
||||
#parameters
|
||||
self.inputSize = 3
|
||||
self.outputSize = 2
|
||||
self.hiddenSize = 3
|
||||
self.fitness = 0
|
||||
|
||||
#weights
|
||||
self.W1 = np.random.randn(self.inputSize, self.hiddenSize) # weights from input to hidden layer
|
||||
self.W2 = np.random.randn(self.hiddenSize, self.outputSize) # weights from hidden to output layer
|
||||
if not W1 :
|
||||
self.W1 = np.random.randn(self.inputSize, self.hiddenSize) # weights from input to hidden layer
|
||||
if not W2 :
|
||||
self.W2 = np.random.randn(self.hiddenSize, self.outputSize) # weights from hidden to output layer
|
||||
# self.w1 = [[random.random() for i in range(self.hiddenSize)] for i in range(self.inputSize)]
|
||||
# self.w2 = [[random.random() for i in range(self.outputSize)] for i in range(self.hiddenSize)]
|
||||
|
||||
def predict(self, X):
|
||||
#forward propagation through our network
|
||||
@@ -19,6 +27,10 @@ class Neural_Network(object):
|
||||
self.z2 = self.sigmoid(self.z) # activation function
|
||||
self.z3 = np.dot(self.z2, self.W2) # dot product of hidden layer (z2) and second set of 3x1 weights
|
||||
o = self.sigmoid(self.z3) # final activation function
|
||||
# self.z = mat_mult(X, self.w1) # dot product of X (input) and first set of 3x2 weights
|
||||
# self.z2 = self.sigmoid(self.z) # activation function
|
||||
# self.z3 = mat_mult(self.z2, self.w2) # dot product of hidden layer (z2) and second set of 3x1 weights
|
||||
# o = self.sigmoid(self.z3) # final activation function
|
||||
return o
|
||||
|
||||
def sigmoid(self, s):
|
||||
|
||||
Reference in New Issue
Block a user