From 21f531669d96aff21a57ce3cb2897a57b86f2e7d Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Tue, 20 Oct 2020 19:49:39 +0200 Subject: [PATCH] More learning rate stuff --- rgbAI/lib/ailib/ai.py | 30 +++++++++++++++++++++++++----- rgbAI/main.py | 4 +--- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/rgbAI/lib/ailib/ai.py b/rgbAI/lib/ailib/ai.py index f47e8d3..8d44a48 100644 --- a/rgbAI/lib/ailib/ai.py +++ b/rgbAI/lib/ailib/ai.py @@ -2,6 +2,14 @@ import numpy as np from copy import deepcopy as copy import os +DEBUG_BUFFER = { + "cost": None, + "lr": { + "weight": None, + "bias": None + } +} + def sigmoid(x): return 1/(1 + np.exp(-x)) @@ -45,6 +53,9 @@ def compareAIobjects( inp, obj1, obj2 ): # Compare the two instances res1 = think( inp, obj1 ) cost1 = getThinkCost( inp, res1 ) # get the cost + + global DEBUG_BUFFER + DEBUG_BUFFER["cost"] = cost1 res2 = think( inp, obj2 ) cost2 = getThinkCost( inp, res2 ) # get the second cost @@ -122,7 +133,7 @@ def calculateSteepness( cost:float, gradient:np.matrix ): ddCost = cost / gradLen out = np.absolute( np.arcsin( np.sin(ddCost) ) ) - return out + return out def getLearningRate( cost:float, gradient:dict, maxLen:int ): learningrate = { @@ -130,6 +141,9 @@ def getLearningRate( cost:float, gradient:dict, maxLen:int ): "bias": calculateSteepness( cost, gradient["bias"] ) } + global DEBUG_BUFFER + DEBUG_BUFFER["lr"] = learningrate + return learningrate @@ -148,7 +162,14 @@ def mutateProps( inpObj, curCost:float, maxLayer:int, gradient:list ): return obj -def learn( inputNum:int, targetCost:float, obj, theta:float, curCost: float=None ): +def printProgress(): + global DEBUG_BUFFER + + os.system("clear") + print(f"LR: {DEBUG_BUFFER['lr']}") + print(f"Cost: {DEBUG_BUFFER['cost']}") + +def learn( inputNum:int, targetCost:float, obj, theta:float, curCost: float=None, trainForever: bool=False ): # Calculate the derivative for: # Cost in respect to weights # Cost in respect to biases @@ -158,14 +179,13 @@ def learn( inputNum:int, targetCost:float, obj, theta:float, curCost: float=None 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 + while( trainForever or not curCost or curCost > targetCost ): # targetCost is the target for the cost function maxLen = len(obj.bias) grads, costW, costB, curCost = gradient( inp, obj, theta, maxLen - 1 ) obj = mutateProps( obj, curCost, maxLen, grads ) # mutate the props for next round - os.system("clear") - print(f"Cost: {curCost}") + printProgress() print("DONE\n") print(obj.weights) diff --git a/rgbAI/main.py b/rgbAI/main.py index a66903a..3c2c565 100755 --- a/rgbAI/main.py +++ b/rgbAI/main.py @@ -14,8 +14,6 @@ class rgb(object): self.bias = [ ai.genRandomMatrix(1, 8), ai.genRandomMatrix(1, 8), ai.genRandomMatrix(1, 8), ai.genRandomMatrix(1, 3) ] # This doesn't look very good, but it works so... - self.learningrate = 0.01 # the learning rate of this ai - print( self.weights ) print( self.bias ) @@ -29,7 +27,7 @@ class rgb(object): return cost def learn( self ): - ai.learn( 3, 0.0001, self, 0.000001 ) + ai.learn( 3, 0.0001, self, 3e-7 ) def think( self, inp:np.array ): print("\n-Input-")