diff --git a/rgbAI/lib/func.py b/rgbAI/lib/func.py index 2b1c763..1276ce5 100644 --- a/rgbAI/lib/func.py +++ b/rgbAI/lib/func.py @@ -54,29 +54,41 @@ class AIlib: def gradient( dCost:float, prop:list ): propLen = len(prop) gradient = [None] * propLen - for i in range( propLen, -1, -1 ): - if( i == propLen ): - gradient[i] = dCost / prop[i] - else: - gradient[i] = dCost / (prop[i] + gradient[i+1]) + for i in range( propLen - 1, -1, -1 ): + # if( i == propLen - 1 ): + # gradient[i] = dCost / prop[i] + # else: + # gradient[i] = dCost / (prop[i] + gradient[i+1]) + gradient[i] = dCost / prop[i] return gradient - def learn( inp:np.array, weights:list, bias:list, theta:float ): + def mutateProp( prop:list, gradient:list ): + newProp = [None] * len(gradient) + + for i in range(len(gradient)): + newProp[i] = prop[i] - gradient[i] # * theta (relative to slope or something) + + return newProp + + def learn( inp:np.array, obj, theta:float ): # Calculate the derivative for: # Cost in respect to weights # Cost in respect to biases - res1 = AIlib.think( inp, weights, bias ) # Think the first result + res1 = AIlib.think( inp, obj.weights, obj.bias ) # Think the first result cost1 = AIlib.calcCost( inp, res1 ) # Calculate the cost of the thought result inp2 = np.asarray( inp + theta ) # make the new input with `theta` as diff - res2 = AIlib.think( inp2, weights, bias ) # Think the second result + res2 = AIlib.think( inp2, obj.weights, obj.bias ) # Think the second result cost2 = AIlib.calcCost( inp2, res2 ) # Calculate the cost dCost = cost2 - cost1 # get the difference - weightDer = AIlib.gradient( dCost, weights ) - biasDer = AIlib.gradient( dCost, bias ) + weightDer = AIlib.gradient( dCost, obj.weights ) + biasDer = AIlib.gradient( dCost, obj.bias ) + + obj.weights = AIlib.mutateProp( obj.weights, weightDer ) + obj.bias = AIlib.mutateProp( obj.bias, biasDer ) - print(weights, len(weights)) + print("Cost: ", cost1) diff --git a/rgbAI/main.py b/rgbAI/main.py index 8687524..eb91768 100755 --- a/rgbAI/main.py +++ b/rgbAI/main.py @@ -16,6 +16,9 @@ class rgb(object): self.generation = 0 + print( self.weights ) + print( self.bias ) + else: # if we want to load our progress from before then this would do it self.weights = loadedWeights self.bias = loadedBias @@ -26,9 +29,9 @@ class rgb(object): return cost def learn( self, inp:np.array, theta:float ): - ai.learn( inp, self.weights, self.bias, theta ) + ai.learn( inp, self, theta ) - def think(self, inp:np.array): + def think( self, inp:np.array ): print("-----Gen " + str(self.generation) + "------") print("\n-Input-") print(inp) @@ -41,10 +44,18 @@ class rgb(object): print("\n----------------\n\n") return res + def train( self ): + for i in range(self.traintimes): + inpArr = np.asarray(np.random.rand( 1, 3 ))[0] + print("##################", inpArr) + self.generation = i + self.learn( inpArr, 0.1 ) + def init(): # init bot = rgb() - inpArr = np.array( [0.2, 0.4, 0.8] ) - bot.learn( inpArr, 0.1 ) + bot.traintimes = 100 + bot.train() + init()