|
|
@ -54,29 +54,41 @@ class AIlib: |
|
|
|
def gradient( dCost:float, prop:list ): |
|
|
|
def gradient( dCost:float, prop:list ): |
|
|
|
propLen = len(prop) |
|
|
|
propLen = len(prop) |
|
|
|
gradient = [None] * propLen |
|
|
|
gradient = [None] * propLen |
|
|
|
for i in range( propLen, -1, -1 ): |
|
|
|
for i in range( propLen - 1, -1, -1 ): |
|
|
|
if( i == propLen ): |
|
|
|
# if( i == propLen - 1 ): |
|
|
|
|
|
|
|
# gradient[i] = dCost / prop[i] |
|
|
|
|
|
|
|
# else: |
|
|
|
|
|
|
|
# gradient[i] = dCost / (prop[i] + gradient[i+1]) |
|
|
|
gradient[i] = dCost / prop[i] |
|
|
|
gradient[i] = dCost / prop[i] |
|
|
|
else: |
|
|
|
|
|
|
|
gradient[i] = dCost / (prop[i] + gradient[i+1]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return gradient |
|
|
|
return gradient |
|
|
|
|
|
|
|
|
|
|
|
def learn( inp:np.array, weights:list, bias:list, theta:float ): |
|
|
|
def mutateProp( prop:list, gradient:list ): |
|
|
|
|
|
|
|
newProp = [None] * len(gradient) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i in range(len(gradient)): |
|
|
|
|
|
|
|
newProp[i] = prop[i] - gradient[i] # * theta (relative to slope or something) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return newProp |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def learn( inp:np.array, obj, theta:float ): |
|
|
|
# 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 |
|
|
|
|
|
|
|
|
|
|
|
res1 = AIlib.think( inp, weights, 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, weights, 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 = cost2 - cost1 # get the difference |
|
|
|
|
|
|
|
|
|
|
|
weightDer = AIlib.gradient( dCost, weights ) |
|
|
|
weightDer = AIlib.gradient( dCost, obj.weights ) |
|
|
|
biasDer = AIlib.gradient( dCost, bias ) |
|
|
|
biasDer = AIlib.gradient( dCost, obj.bias ) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
obj.weights = AIlib.mutateProp( obj.weights, weightDer ) |
|
|
|
|
|
|
|
obj.bias = AIlib.mutateProp( obj.bias, biasDer ) |
|
|
|
|
|
|
|
|
|
|
|
print(weights, len(weights)) |
|
|
|
print("Cost: ", cost1) |
|
|
|