More learning rate stuff

master
E. Almqvist 4 years ago
parent c0a536c49a
commit 21f531669d
  1. 28
      rgbAI/lib/ailib/ai.py
  2. 4
      rgbAI/main.py

@ -2,6 +2,14 @@ import numpy as np
from copy import deepcopy as copy from copy import deepcopy as copy
import os import os
DEBUG_BUFFER = {
"cost": None,
"lr": {
"weight": None,
"bias": None
}
}
def sigmoid(x): def sigmoid(x):
return 1/(1 + np.exp(-x)) return 1/(1 + np.exp(-x))
@ -46,6 +54,9 @@ def compareAIobjects( inp, obj1, obj2 ):
res1 = think( inp, obj1 ) res1 = think( inp, obj1 )
cost1 = getThinkCost( inp, res1 ) # get the cost cost1 = getThinkCost( inp, res1 ) # get the cost
global DEBUG_BUFFER
DEBUG_BUFFER["cost"] = cost1
res2 = think( inp, obj2 ) res2 = think( inp, obj2 )
cost2 = getThinkCost( inp, res2 ) # get the second cost cost2 = getThinkCost( inp, res2 ) # get the second cost
@ -130,6 +141,9 @@ def getLearningRate( cost:float, gradient:dict, maxLen:int ):
"bias": calculateSteepness( cost, gradient["bias"] ) "bias": calculateSteepness( cost, gradient["bias"] )
} }
global DEBUG_BUFFER
DEBUG_BUFFER["lr"] = learningrate
return learningrate return learningrate
@ -148,7 +162,14 @@ def mutateProps( inpObj, curCost:float, maxLayer:int, gradient:list ):
return obj 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: # Calculate the derivative for:
# Cost in respect to weights # Cost in respect to weights
# Cost in respect to biases # 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 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) maxLen = len(obj.bias)
grads, costW, costB, curCost = gradient( inp, obj, theta, maxLen - 1 ) grads, costW, costB, curCost = gradient( inp, obj, theta, maxLen - 1 )
obj = mutateProps( obj, curCost, maxLen, grads ) # mutate the props for next round obj = mutateProps( obj, curCost, maxLen, grads ) # mutate the props for next round
os.system("clear")
print(f"Cost: {curCost}")
printProgress()
print("DONE\n") print("DONE\n")
print(obj.weights) print(obj.weights)

@ -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) ] 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... # 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.weights )
print( self.bias ) print( self.bias )
@ -29,7 +27,7 @@ class rgb(object):
return cost return cost
def learn( self ): 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 ): def think( self, inp:np.array ):
print("\n-Input-") print("\n-Input-")

Loading…
Cancel
Save