|
|
@ -41,18 +41,14 @@ class AIlib: |
|
|
|
# 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 compareInstance( obj, neuronIndex ): |
|
|
|
# Check if grads exists, if not create the buffer |
|
|
|
|
|
|
|
if( not grads ): |
|
|
|
|
|
|
|
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][neuronIndex] += 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 ) |
|
|
@ -64,7 +60,14 @@ class AIlib: |
|
|
|
# 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] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return dCost, dWeight |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
if( not grads ): |
|
|
|
|
|
|
|
grads = [None] * (maxLayer+1) |
|
|
|
|
|
|
|
|
|
|
|
# Calculate the gradient for the layer |
|
|
|
# Calculate the gradient for the layer |
|
|
|
weightDer = AIlib.propDer( dCost, dWeight ) |
|
|
|
weightDer = AIlib.propDer( dCost, dWeight ) |
|
|
|