diff --git a/rgbAI/lib/func.py b/rgbAI/lib/func.py index 575b0c8..898558c 100644 --- a/rgbAI/lib/func.py +++ b/rgbAI/lib/func.py @@ -82,11 +82,14 @@ class AIlib: else: return grads, res1, cost1 - def mutateProps( obj, maxLen:int, gradient:list ): + def mutateProps( inpObj, maxLen:int, gradient:list ): + obj = copy(inpObj) for i in range(maxLen): obj.weights[i] -= obj.learningrate * gradient[i]["weight"] # mutate the weights obj.bias[i] -= obj.learningrate * gradient[i]["bias"] + return obj + def learn( inputNum:int, targetCost:float, obj, theta:float, curCost: float=None ): # Calculate the derivative for: # Cost in respect to weights @@ -95,19 +98,15 @@ class AIlib: # i.e. : W' = W - lr * gradient (respect to W in layer i) = W - lr*[ dC / dW[i] ... ] # So if we change all the weights with i.e. 0.01 = theta, then we can derive the gradient with math and stuff - if( not curCost or curCost > targetCost ): # targetCost is the target for the cost function - inp = np.asarray(np.random.rand( 1, inputNum ))[0] + inp = np.asarray(np.random.rand( 1, inputNum ))[0] # create a random learning sample + while( not curCost or curCost > targetCost ): # targetCost is the target for the cost function maxLen = len(obj.bias) grads, res, curCost = AIlib.gradient( inp, obj, theta, maxLen - 1 ) - AIlib.mutateProps( obj, maxLen, grads ) # mutate the props for next round + obj = AIlib.mutateProps( obj, maxLen, grads ) # mutate the props for next round print("Cost:", curCost, "|", inp, res) - return AIlib.learn( inputNum, targetCost, obj, theta, curCost ) - - else: - print("DONE\n") - print(obj.weights) - print(obj.bias) - return + print("DONE\n") + print(obj.weights) + print(obj.bias) diff --git a/rgbAI/main.py b/rgbAI/main.py index 3573591..b04f38d 100755 --- a/rgbAI/main.py +++ b/rgbAI/main.py @@ -32,20 +32,23 @@ class rgb(object): ai.learn( 3, 0.001, self, 0.001 ) def think( self, inp:np.array ): - print("-----Gen " + str(self.generation) + "------") print("\n-Input-") print(inp) print("\n") - res = ai.think( inp, self.weights, self.bias ) + res = ai.think( inp, self ) print("\n-Output-") print(res) - print("\n----------------\n\n") return res def init(): bot = rgb() bot.learn() + inpArr = np.asarray([1.0, 1.0, 1.0]) + res = bot.think( inpArr ) + err = bot.calcError( inpArr, res ) + print(err) + init()