|
|
@ -10,16 +10,11 @@ class AIlib: |
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
def calcCost( inp:np.array, out: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 |
|
|
|
sumC = 0 |
|
|
|
return (predicted - correct)**2 |
|
|
|
outLen = len(out) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
correctOut = AIlib.correctFunc(inp) # the "correct" output |
|
|
|
def calcCost_derv( predicted:np.array, correct:np.array ): |
|
|
|
|
|
|
|
return (predicted - correct)*2 |
|
|
|
diff = (out - outLen)**2 |
|
|
|
|
|
|
|
sumC = diff.sum() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sumC / outLen # return the cost |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 - |
|
|
@ -40,15 +35,22 @@ class AIlib: |
|
|
|
print("\n") |
|
|
|
print("\n") |
|
|
|
return out |
|
|
|
return out |
|
|
|
|
|
|
|
|
|
|
|
def gradient( dCost:float, out:np.array, inp:np.array ): |
|
|
|
def gradient( cost:float, inp:np.array, predicted:np.array, correct:np.array ): |
|
|
|
# Calculate the gradient |
|
|
|
# Calculate the gradient |
|
|
|
print("") |
|
|
|
derv1 = AIlib.calcCost_derv( predicted, correct ) |
|
|
|
|
|
|
|
derv2 = AIlib.sigmoid_der( predicted ) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gradient = np.transpose( np.asmatrix(derv1 * derv2 * inp) ) |
|
|
|
|
|
|
|
print("Inp:", inp) |
|
|
|
|
|
|
|
print("Grad:", gradient) |
|
|
|
|
|
|
|
return gradient |
|
|
|
|
|
|
|
|
|
|
|
def mutateProp( prop:list, gradient:list ): |
|
|
|
|
|
|
|
newProp = [None] * len(gradient) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i in range(len(gradient)): |
|
|
|
def mutateProp( prop:list, lr, gradient ): |
|
|
|
newProp[i] = prop[i] - gradient[i] # * theta (relative to slope or something) |
|
|
|
newProp = [None] * len(prop) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i in range(len(prop)): |
|
|
|
|
|
|
|
newProp[i] = prop[i] - (lr*gradient) |
|
|
|
|
|
|
|
|
|
|
|
return newProp |
|
|
|
return newProp |
|
|
|
|
|
|
|
|
|
|
@ -57,19 +59,17 @@ class AIlib: |
|
|
|
# Cost in respect to weights |
|
|
|
# Cost in respect to weights |
|
|
|
# Cost in respect to biases |
|
|
|
# Cost in respect to biases |
|
|
|
|
|
|
|
|
|
|
|
res1 = AIlib.think( inp, obj.weights, obj.bias ) # Think the first result |
|
|
|
predicted = AIlib.think( inp, obj.weights, obj.bias ) # Think the first result |
|
|
|
cost1 = AIlib.calcCost( inp, res1 ) # Calculate the cost of the thought result |
|
|
|
correct = AIlib.correctFunc( inp ) |
|
|
|
|
|
|
|
cost = AIlib.calcCost( predicted, correct ) # 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 = cost1 # get the difference # cost2 - cost1 |
|
|
|
gradient = AIlib.gradient( cost, inp, predicted, correct ) |
|
|
|
|
|
|
|
|
|
|
|
weightDer = AIlib.gradient( dCost, theta, obj.weights ) |
|
|
|
|
|
|
|
biasDer = AIlib.gradient( dCost, theta, obj.bias ) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
obj.weights = AIlib.mutateProp( obj.weights, weightDer ) |
|
|
|
obj.weights = AIlib.mutateProp( obj.weights, obj.learningrate, gradient ) |
|
|
|
obj.bias = AIlib.mutateProp( obj.bias, biasDer ) |
|
|
|
obj.bias = AIlib.mutateProp( obj.bias, obj.learningrate, gradient ) |
|
|
|
|
|
|
|
|
|
|
|
print("Cost: ", cost1) |
|
|
|
print("Cost: ", cost1) |
|
|
|