Switched to tabs

pull/1/head
E. Almqvist 4 years ago
parent a543a9db1a
commit ff3d69bf61
  1. 214
      rgbAI/lib/func.py
  2. 68
      rgbAI/main.py

@ -2,111 +2,111 @@ import numpy as np
from copy import deepcopy as copy from copy import deepcopy as copy
class AIlib: class AIlib:
def sigmoid(x): def sigmoid(x):
return 1/(1 + np.exp(-x)) return 1/(1 + np.exp(-x))
def correctFunc(inp:np.array): # generates the correct answer for the AI def correctFunc(inp:np.array): # generates the correct answer for the AI
return np.asarray( [1.0 - inp[0], 1.0 - inp[1], 1.0 - inp[2]] ) # basically invert the rgb values return np.asarray( [1.0 - inp[0], 1.0 - inp[1], 1.0 - inp[2]] ) # basically invert the rgb values
def calcCost( predicted:np.array, correct:np.array ): # cost function, lower -> good, higher -> bad, bad bot, bad def calcCost( predicted:np.array, correct:np.array ): # cost function, lower -> good, higher -> bad, bad bot, bad
costSum = 0 costSum = 0
maxLen = len(correct) maxLen = len(correct)
for i in range(maxLen): for i in range(maxLen):
costSum += (predicted[i] - correct[i])**2 costSum += (predicted[i] - correct[i])**2
return costSum / maxLen return costSum / maxLen
def getThinkCost( inp:np.array, predicted:np.array ): def getThinkCost( inp:np.array, predicted:np.array ):
corr = AIlib.correctFunc(inp) corr = AIlib.correctFunc(inp)
return AIlib.calcCost( predicted, corr ) return AIlib.calcCost( predicted, corr )
def genRandomMatrix( x:int, y:int, min: float=0.0, max: float=1.0 ): # generate a matrix with x, y dimensions with random values from min-max in it def genRandomMatrix( x:int, y:int, min: float=0.0, max: float=1.0 ): # generate a matrix with x, y dimensions with random values from min-max in it
# apply ranger with * and - # apply ranger with * and -
mat = np.random.rand(x, y) - 0.25 mat = np.random.rand(x, y) - 0.25
return mat return mat
def think( inp:np.array, obj, layerIndex: int=0 ): # recursive thinking, hehe def think( inp:np.array, obj, layerIndex: int=0 ): # recursive thinking, hehe
maxLayer = len(obj.weights) - 1 maxLayer = len(obj.weights) - 1
weightedLayer = np.dot( inp, obj.weights[layerIndex] ) # dot multiply the input and the weights weightedLayer = np.dot( inp, obj.weights[layerIndex] ) # dot multiply the input and the weights
layer = AIlib.sigmoid( np.add(weightedLayer, obj.bias[layerIndex]) ) # add the biases layer = AIlib.sigmoid( np.add(weightedLayer, obj.bias[layerIndex]) ) # add the biases
if( layerIndex < maxLayer ): if( layerIndex < maxLayer ):
return AIlib.think( layer, obj, layerIndex + 1 ) return AIlib.think( layer, obj, layerIndex + 1 )
else: else:
out = np.squeeze(np.asarray(layer)) out = np.squeeze(np.asarray(layer))
return out return out
def propDer( dCost, dProp ): def propDer( dCost, dProp ):
# Calculate the partial derivative for that prop # Calculate the partial derivative for that prop
return dCost / dProp return dCost / dProp
def gradient( inp:np.array, obj, theta:float, maxLayer:int, layerIndex: int=0, grads=None, obj1=None, obj2=None ): # Calculate the gradient for that prop def gradient( inp:np.array, obj, theta:float, maxLayer:int, layerIndex: int=0, grads=None, obj1=None, obj2=None ): # Calculate the gradient for that prop
# Check if grads exists, if not create the buffer # Check if grads exists, if not create the buffer
if( not grads ): if( not grads ):
grads = [None] * (maxLayer+1) grads = [None] * (maxLayer+1)
# Create new instances of the object # Create new instances of the object
if( not obj1 or not obj2 ): if( not obj1 or not obj2 ):
obj1 = copy(obj) # annoying way to create a new instance of the object obj1 = copy(obj) # annoying way to create a new instance of the object
obj2 = copy(obj) obj2 = copy(obj)
obj2.weights[layerIndex] += theta # mutate the second object obj2.weights[layerIndex] += theta # mutate the second object
obj2.bias[layerIndex] += theta obj2.bias[layerIndex] += theta
# Compare the two instances # Compare the two instances
res1 = AIlib.think( inp, obj1 ) res1 = AIlib.think( inp, obj1 )
cost1 = AIlib.getThinkCost( inp, res1 ) # get the cost cost1 = AIlib.getThinkCost( inp, res1 ) # get the cost
res2 = AIlib.think( inp, obj2 ) res2 = AIlib.think( inp, obj2 )
cost2 = AIlib.getThinkCost( inp, res2 ) # get the second cost cost2 = AIlib.getThinkCost( inp, res2 ) # get the second cost
# Actually calculate stuff # Actually calculate stuff
dCost = cost2 - cost1 dCost = cost2 - cost1
dWeight = obj2.weights[layerIndex] - obj1.weights[layerIndex] dWeight = obj2.weights[layerIndex] - obj1.weights[layerIndex]
dBias = obj2.bias[layerIndex] - obj1.bias[layerIndex] dBias = obj2.bias[layerIndex] - obj1.bias[layerIndex]
# Calculate the gradient for the layer # Calculate the gradient for the layer
weightDer = AIlib.propDer( dCost, dWeight ) weightDer = AIlib.propDer( dCost, dWeight )
biasDer = AIlib.propDer( dCost, dBias ) biasDer = AIlib.propDer( dCost, dBias )
# Append the gradients to the list # Append the gradients to the list
grads[layerIndex] = { grads[layerIndex] = {
"weight": weightDer, "weight": weightDer,
"bias": biasDer "bias": biasDer
} }
newLayer = layerIndex + 1 newLayer = layerIndex + 1
if( newLayer <= maxLayer ): if( newLayer <= maxLayer ):
return AIlib.gradient( inp, obj, theta, maxLayer, newLayer, grads, obj1, obj2 ) return AIlib.gradient( inp, obj, theta, maxLayer, newLayer, grads, obj1, obj2 )
else: else:
return grads, res1, cost1 return grads, res1, cost1
def mutateProps( inpObj, maxLen:int, gradient:list ): def mutateProps( inpObj, maxLen:int, gradient:list ):
obj = copy(inpObj) obj = copy(inpObj)
for i in range(maxLen): for i in range(maxLen):
obj.weights[i] -= obj.learningrate * gradient[i]["weight"] # mutate the weights obj.weights[i] -= obj.learningrate * gradient[i]["weight"] # mutate the weights
obj.bias[i] -= obj.learningrate * gradient[i]["bias"] obj.bias[i] -= obj.learningrate * gradient[i]["bias"]
return obj return obj
def learn( inputNum:int, targetCost:float, obj, theta:float, curCost: float=None ): 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
# 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 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( not curCost or curCost > targetCost ): # targetCost is the target for the cost function
maxLen = len(obj.bias) maxLen = len(obj.bias)
grads, res, curCost = AIlib.gradient( inp, obj, theta, maxLen - 1 ) grads, res, curCost = AIlib.gradient( inp, obj, theta, maxLen - 1 )
obj = 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) print("Cost:", curCost, "|", inp, res)
print("DONE\n") print("DONE\n")
print(obj.weights) print(obj.weights)
print(obj.bias) print(obj.bias)

@ -3,52 +3,52 @@ import numpy as np
from lib.func import AIlib as ai from lib.func import AIlib as ai
class rgb(object): class rgb(object):
def __init__(self, loadedWeights: np.matrix=None, loadedBias: np.matrix=None): def __init__(self, loadedWeights: np.matrix=None, loadedBias: np.matrix=None):
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, 4), ai.genRandomMatrix(4, 4), ai.genRandomMatrix(4, 3) ] # array of matrices of weights self.weights = [ ai.genRandomMatrix(3, 4), ai.genRandomMatrix(4, 4), ai.genRandomMatrix(4, 3) ] # array of matrices of weights
# 3 input neurons -> 4 hidden neurons -> 4 hidden neurons -> 3 output neurons # 3 input neurons -> 4 hidden neurons -> 4 hidden neurons -> 3 output neurons
# Generate the biases # Generate the biases
self.bias = [ ai.genRandomMatrix(1, 4), ai.genRandomMatrix(1, 4), ai.genRandomMatrix(1, 3) ] self.bias = [ ai.genRandomMatrix(1, 4), ai.genRandomMatrix(1, 4), 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 self.learningrate = 0.01 # the learning rate of this ai
print( self.weights ) print( self.weights )
print( self.bias ) print( self.bias )
else: # if we want to load our progress from before then this would do it else: # if we want to load our progress from before then this would do it
self.weights = loadedWeights self.weights = loadedWeights
self.bias = loadedBias self.bias = loadedBias
def calcError( self, inp:np.array, out:np.array ): def calcError( self, inp:np.array, out:np.array ):
cost = ai.calcCost( inp, out ) cost = ai.calcCost( inp, out )
# Cost needs to get to 0, we can figure out this with backpropagation # Cost needs to get to 0, we can figure out this with backpropagation
return cost return cost
def learn( self ): def learn( self ):
ai.learn( 3, 0.0001, self, 0.001 ) ai.learn( 3, 0.0001, self, 0.001 )
def think( self, inp:np.array ): def think( self, inp:np.array ):
print("\n-Input-") print("\n-Input-")
print(inp) print(inp)
print("\n") print("\n")
res = ai.think( inp, self ) res = ai.think( inp, self )
print("\n-Output-") print("\n-Output-")
print(res) print(res)
return res return res
def init(): def init():
bot = rgb() bot = rgb()
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