|
|
|
@ -21,28 +21,23 @@ class AIlib: |
|
|
|
|
mat = np.random.rand(x, y) - 0.25 |
|
|
|
|
return mat |
|
|
|
|
|
|
|
|
|
def think( inp:np.array, weights:list, bias:list, layerIndex: int=0 ): # recursive thinking, hehe |
|
|
|
|
def think( inp:np.array, weights:list, bias:list, layerIndex: int=0, layers: list=[] ): # recursive thinking, hehe |
|
|
|
|
maxLayer = len(weights) - 1 |
|
|
|
|
weightedInput = np.dot( inp, weights[layerIndex] ) # dot multiply the input and the weights |
|
|
|
|
layer = AIlib.sigmoid( np.add(weightedInput, bias[layerIndex]) ) # add the biases |
|
|
|
|
weightedLayer = np.dot( inp, weights[layerIndex] ) # dot multiply the input and the weights |
|
|
|
|
layer = AIlib.sigmoid( np.add(weightedLayer, bias[layerIndex]) ) # add the biases |
|
|
|
|
layers[layerIndex] = layer # save it to the layer buffer |
|
|
|
|
|
|
|
|
|
if( layerIndex < maxLayer ): |
|
|
|
|
return AIlib.think( layer, weights, bias, layerIndex + 1 ) |
|
|
|
|
return AIlib.think( layer, weights, bias, layerIndex + 1, layers ) |
|
|
|
|
else: |
|
|
|
|
out = np.squeeze(np.asarray(layer)) |
|
|
|
|
print("-Result-") |
|
|
|
|
print(out) |
|
|
|
|
print("\n") |
|
|
|
|
return out |
|
|
|
|
return out, layers |
|
|
|
|
|
|
|
|
|
def gradient( prop, cost:float, inp:np.array, predicted:np.array, correct:np.array ): |
|
|
|
|
# Calculate the gradient |
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -63,14 +58,8 @@ class AIlib: |
|
|
|
|
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 |
|
|
|
|
#res2 = AIlib.think( inp2, obj.weights, obj.bias ) # Think the second result |
|
|
|
|
#cost2 = AIlib.calcCost( inp2, res2 ) # Calculate the cost |
|
|
|
|
|
|
|
|
|
gradientWeight = AIlib.gradient( obj.weights, cost, inp, predicted, correct ) |
|
|
|
|
gradientBias = AIlib.gradient( obj.bias, cost, inp, predicted, correct ) |
|
|
|
|
|
|
|
|
|
obj.weights = AIlib.mutateProp( obj.weights, obj.learningrate, gradientWeight ) |
|
|
|
|
obj.bias = AIlib.mutateProp( obj.bias, obj.learningrate, gradientBias ) |
|
|
|
|
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 |
|
|
|
|
cost2 = AIlib.calcCost( inp2, res2 ) # Calculate the cost |
|
|
|
|
|
|
|
|
|
print("Cost: ", cost1) |
|
|
|
|