You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1 KiB
Python

import numpy as np
class NN():
score = 0
def __init__(self, *args):
self.layers = args
self.synapses = []
for i in range(len(self.layers)-1):
self.synapses.append(np.random.normal(scale=1,size=(self.layers[i], self.layers[i+1])))
def calculateOutput(self, input):
layerInput = input
for synapseLayer in self.synapses:
layerInput = np.dot(layerInput, synapseLayer)
layerInput = self.sigmoid(layerInput)
return layerInput
def sigmoid(self, s):
#print("Arr:", arr)
#s = np.array(arr)
# activation function
res = 1 / (1 + np.exp(-s))
#print("Res:", res)
return res
def getSynapses(self):
return self.synapses
def setSynapses(self, synapses):
self.synapses = synapses
def mutate(self, val):
mut_arr = []
for i in range(len(self.layers)-1):
mut_arr.append(np.random.normal(scale=val,size=(self.layers[i], self.layers[i+1])))
self.synapses = np.add(self.synapses, mut_arr)
return mut_arr