pull/1/head
E. Almqvist 4 years ago
parent 26d8e06230
commit 3081b06253
  1. 22
      rgbAI/lib/func.py
  2. 2
      rgbAI/main.py

@ -4,6 +4,9 @@ class AIlib:
def sigmoid(x): def sigmoid(x):
return 1/(1 + np.exp(-x)) return 1/(1 + np.exp(-x))
def sigmoid_der(x):
return AIlib.sigmoid(x) * (1 - AIlib.sigmoid(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.array( [inp[2], inp[1], inp[0]] ) # basically invert the rgb values return np.array( [inp[2], inp[1], inp[0]] ) # basically invert the rgb values
@ -13,8 +16,8 @@ class AIlib:
correctOut = AIlib.correctFunc(inp) # the "correct" output correctOut = AIlib.correctFunc(inp) # the "correct" output
for i in range(outLen): diff = (out - outLen)**2
sumC += (out[i] - correctOut[i])**2 # get the difference of every value sumC = diff.sum()
return sumC / outLen # return the cost return sumC / outLen # return the cost
@ -37,8 +40,9 @@ class AIlib:
print("\n") print("\n")
return out return out
def gradient( dCost:float, dx:float, prop:list ): def gradient( dCost:float, out:np.array, inp:np.array ):
# Calculate the gradient # Calculate the gradient
print("")
def mutateProp( prop:list, gradient:list ): def mutateProp( prop:list, gradient:list ):
newProp = [None] * len(gradient) newProp = [None] * len(gradient)
@ -56,16 +60,16 @@ class AIlib:
res1 = AIlib.think( inp, obj.weights, obj.bias ) # Think the first result res1 = AIlib.think( inp, obj.weights, obj.bias ) # Think the first result
cost1 = AIlib.calcCost( inp, res1 ) # Calculate the cost of the thought result cost1 = AIlib.calcCost( inp, res1 ) # Calculate the cost of the thought result
inp2 = np.asarray( inp + theta ) # make the new input with `theta` as diff #inp2 = np.asarray( inp + theta ) # make the new input with `theta` as diff
res2 = AIlib.think( inp2, obj.weights, obj.bias ) # Think the second result #res2 = AIlib.think( inp2, obj.weights, obj.bias ) # Think the second result
cost2 = AIlib.calcCost( inp2, res2 ) # Calculate the cost #cost2 = AIlib.calcCost( inp2, res2 ) # Calculate the cost
dCost = cost2 - cost1 # get the difference dCost = cost1 # get the difference # cost2 - cost1
weightDer = AIlib.gradient( dCost, theta, obj.weights ) weightDer = AIlib.gradient( dCost, theta, obj.weights )
biasDer = AIlib.gradient( dCost, theta, obj.bias ) biasDer = AIlib.gradient( dCost, theta, obj.bias )
#obj.weights = AIlib.mutateProp( obj.weights, weightDer ) obj.weights = AIlib.mutateProp( obj.weights, weightDer )
#obj.bias = AIlib.mutateProp( obj.bias, biasDer ) obj.bias = AIlib.mutateProp( obj.bias, biasDer )
print("Cost: ", cost1) print("Cost: ", cost1)

@ -56,7 +56,7 @@ class rgb(object):
def init(): # init def init(): # init
bot = rgb() bot = rgb()
bot.traintimes = 10 bot.traintimes = 100
bot.train() bot.train()

Loading…
Cancel
Save