Finished AI thinking

pull/1/head
E. Almqvist 4 years ago
parent 3cdd15e8fa
commit e96525e4ff
  1. 28
      lib/func.py
  2. 5
      main.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 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) 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 # the length of weights and bias should be the same
# if not then the neural net is flawed/incorrect # if not then the neural net is flawed/incorrect
maxLayer = len(weights) maxLayer = len(weights) - 1
biasLen = len(bias) biasLen = len(bias) - 1
if( maxLayer != len(bias) ): if( maxLayer != biasLen ):
print("Neural Network Error: Length of weights and bias are not equal.") 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() exit()
try: try:
print("Think " + str(layerIndex))
weightedInput = np.dot( weights[layerIndex], inp ) # dot multiply the input and the weights 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 ): print(layer)
return layer print("\n")
if( layerIndex < maxLayer ):
return AIlib.think( layer, weights, bias, layerIndex + 1 )
else: else:
print("New think " + str(layerIndex + 1)) return layer
AIlib.think( layer, weights, bias, layerIndex + 1 )
except ValueError as err: except (ValueError, IndexError) as err:
print("\n---------") print("\n---------")
print( "Error: " + str(err) ) print( "Error: " + str(err) )
print( "Layer index: " + str(layerIndex) ) print( "Layer index: " + str(layerIndex) )
print( "Max layer index: " + str(maxLayer) )

@ -23,9 +23,8 @@ class rgb(object):
def think(self, inp:np.array): def think(self, inp:np.array):
res = ai.think( inp, self.weights, self.bias ) res = ai.think( inp, self.weights, self.bias )
print( "Result: " + str(res) ) print("\n-Result-")
# print(self.weights) print(res)
# print(self.bias)
def init(): # init func def init(): # init func
bot = rgb() bot = rgb()

Loading…
Cancel
Save