Tweaked AI a lot, made tests in the end of training

master
E. Almqvist 4 years ago
parent bfa7c451c7
commit eab52714a4
  1. 39
      rgbAI/lib/ailib/ai.py
  2. 18
      rgbAI/main.py

@ -9,7 +9,8 @@ DEBUG_BUFFER = {
}, },
"inp": None, "inp": None,
"predicted": None, "predicted": None,
"correct": None "correct": None,
"gen": None
} }
def sigmoid(x): def sigmoid(x):
@ -155,7 +156,7 @@ def getLearningRate( cost:float, gradient:dict, maxLen:int ):
def mutateProps( inpObj, curCost:float, maxLayer:int, gradient:list ): def mutateProps( inpObj, curCost:float, maxLayer:int, gradient:list ):
obj = copy(inpObj) obj = inpObj
for layer in range(maxLayer): for layer in range(maxLayer):
lr = getLearningRate( curCost, gradient[layer], maxLayer ) lr = getLearningRate( curCost, gradient[layer], maxLayer )
@ -163,11 +164,8 @@ def mutateProps( inpObj, curCost:float, maxLayer:int, gradient:list ):
obj.weights[layer] -= lr["weight"] * gradient[layer]["weight"] # mutate the weights obj.weights[layer] -= lr["weight"] * gradient[layer]["weight"] # mutate the weights
obj.bias[layer] -= lr["bias"] * gradient[layer]["bias"] obj.bias[layer] -= lr["bias"] * gradient[layer]["bias"]
# obj.weights[i] -= obj.learningrate * gradient[i]["weight"] # mutate the weights # obj.weights[layer] -= 0.0001 * gradient[layer]["weight"] # mutate the weights
# obj.bias[i] -= obj.learningrate * gradient[i]["bias"] # obj.bias[layer] -= 0.0001 * gradient[layer]["bias"]
return obj
def printProgress(): def printProgress():
import os import os
@ -176,10 +174,10 @@ def printProgress():
os.system("clear") os.system("clear")
print(f"LR: {DEBUG_BUFFER['lr']}") print(f"LR: {DEBUG_BUFFER['lr']}")
print(f"Cost: {DEBUG_BUFFER['cost']}") print(f"Cost: {DEBUG_BUFFER['cost']}")
print("") print(f"Gen: {DEBUG_BUFFER['gen']}")
print(f"inp: {DEBUG_BUFFER['inp']} | pre: {DEBUG_BUFFER['predicted']} cor: {DEBUG_BUFFER['correct']}") print(f"inp: {DEBUG_BUFFER['inp']} | pre: {DEBUG_BUFFER['predicted']} cor: {DEBUG_BUFFER['correct']}")
def learn( inputNum:int, targetCost:float, obj, theta:float, curCost: float=None, trainForever: bool=False ): def learn( inputNum:int, targetCost:float, obj, theta:float, curCost: float=None ):
# 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
@ -187,17 +185,20 @@ def learn( inputNum:int, targetCost:float, obj, theta:float, curCost: float=None
# i.e. : W' = W - lr * gradient (respect to W in layer i) = W - lr*[ dC / dW[i] ... ] # 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 # So if we change all the weights with i.e. 0.01 = theta, then we can derive the gradient with math and stuff
#inp = np.asarray(np.random.rand( 1, inputNum ))[0] # create a random learning sample count = 0
inp = np.asarray([1.0, 1.0, 1.0]) while( count <= 1000 ): # targetCost is the target for the cost function
count += 1
inp = np.asarray(np.random.rand( 1, inputNum ))[0] # create a random learning sample
# inp = np.asarray([1.0, 1.0, 1.0])
global DEBUG_BUFFER
DEBUG_BUFFER["inp"] = inp
DEBUG_BUFFER["gen"] = count
global DEBUG_BUFFER
DEBUG_BUFFER["inp"] = inp
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 mutateProps( obj, curCost, maxLen, grads ) # mutate the props for next round
printProgress() printProgress()
@ -205,4 +206,8 @@ def learn( inputNum:int, targetCost:float, obj, theta:float, curCost: float=None
print(obj.weights) print(obj.weights)
print(obj.bias) print(obj.bias)
return obj test = think( np.asarray([1.0, 1.0, 1.0]), obj )
print(f"Test 1: {test}")
test2 = think( np.asarray([0.0, 0.0, 0.0]), obj )
print(f"Test 2: {test2}")

@ -7,11 +7,11 @@ class rgb(object):
if( not loadedWeights or not loadedBias ): # if one is null (None) then just generate new ones if( not loadedWeights or not loadedBias ): # if one is null (None) then just generate new ones
print("Generating weights and biases...") print("Generating weights and biases...")
self.weights = [ ai.genRandomMatrix(3, 16), ai.genRandomMatrix(16, 16), ai.genRandomMatrix(16, 16), ai.genRandomMatrix(16, 3) ] # array of matrices of weights self.weights = [ ai.genRandomMatrix(3, 3), ai.genRandomMatrix(3, 3), ai.genRandomMatrix(3, 3) ] # array of matrices of weights
# 3 input neurons -> 16 hidden neurons -> 16 hidden neurons -> 3 output neurons # 3 input neurons -> 3 hidden neurons -> 3 hidden neurons -> 3 output neurons
# Generate the biases # Generate the biases
self.bias = [ ai.genRandomMatrix(1, 16), ai.genRandomMatrix(1, 16), ai.genRandomMatrix(1, 16), ai.genRandomMatrix(1, 3) ] self.bias = [ ai.genRandomMatrix(1, 3), ai.genRandomMatrix(1, 3), ai.genRandomMatrix(1, 3) ]
# This doesn't look very good, but it works so... # This doesn't look very good, but it works so...
print( self.weights ) print( self.weights )
@ -27,7 +27,7 @@ class rgb(object):
return cost return cost
def learn( self ): def learn( self ):
ai.learn( 3, 0.0001, self, 3e-7 ) ai.learn( 3, 0.001, self, 0.001 )
def think( self, inp:np.array ): def think( self, inp:np.array ):
print("\n-Input-") print("\n-Input-")
@ -41,11 +41,11 @@ class rgb(object):
def init(): def init():
bot = rgb() bot = rgb()
bot = bot.learn() bot.learn()
inpArr = np.asarray([1.0, 1.0, 1.0]) #inpArr = np.asarray([1.0, 1.0, 1.0])
res = bot.think( inpArr ) #res = bot.think( inpArr )
err = bot.calcError( inpArr, res ) #err = bot.calcError( inpArr, res )
print(err) #print(err)
init() init()

Loading…
Cancel
Save