From e96525e4ff802429b5886fd0aaf3c6f5327c74c6 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 28 Aug 2020 21:39:02 +0200 Subject: [PATCH] Finished AI thinking --- lib/func.py | 28 +++++++++++++++++----------- main.py | 5 ++--- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/func.py b/lib/func.py index 1b9b910..3f80afa 100644 --- a/lib/func.py +++ b/lib/func.py @@ -21,25 +21,31 @@ class AIlib: def genRandomMatrix( x:int, y:int, min: float=0.0, max: float=1.0 ): # generate a matrix with x, y dimensions with random values from min-max in it return np.random.rand(x, y) - def think( inp:np.matrix, weights:list, bias:list, layerIndex: int=0 ): # recursive thinking, hehe + def think( inp:np.array, weights:list, bias:list, layerIndex: int=0 ): # recursive thinking, hehe # the length of weights and bias should be the same # if not then the neural net is flawed/incorrect - maxLayer = len(weights) - biasLen = len(bias) - if( maxLayer != len(bias) ): + maxLayer = len(weights) - 1 + biasLen = len(bias) - 1 + if( maxLayer != biasLen ): print("Neural Network Error: Length of weights and bias are not equal.") - print("Weights: ${maxLayer}, Bias: ${biasLen}") + print( "Weights: " + str(maxLayer) + " Bias: " + str(biasLen) ) exit() + try: + print("Think " + str(layerIndex)) weightedInput = np.dot( weights[layerIndex], inp ) # dot multiply the input and the weights - layer = np.add( weightedInput, bias[layerIndex] ) # add the biases + layer = AIlib.sigmoid( np.add(weightedInput, bias[layerIndex]) ) # add the biases - if( layerIndex >= maxLayer ): - return layer + print(layer) + print("\n") + + if( layerIndex < maxLayer ): + return AIlib.think( layer, weights, bias, layerIndex + 1 ) else: - print("New think " + str(layerIndex + 1)) - AIlib.think( layer, weights, bias, layerIndex + 1 ) - except ValueError as err: + return layer + + except (ValueError, IndexError) as err: print("\n---------") print( "Error: " + str(err) ) print( "Layer index: " + str(layerIndex) ) + print( "Max layer index: " + str(maxLayer) ) diff --git a/main.py b/main.py index 244559e..d8253fa 100755 --- a/main.py +++ b/main.py @@ -23,9 +23,8 @@ class rgb(object): def think(self, inp:np.array): res = ai.think( inp, self.weights, self.bias ) - print( "Result: " + str(res) ) - # print(self.weights) - # print(self.bias) + print("\n-Result-") + print(res) def init(): # init func bot = rgb()