|
|
@ -53,18 +53,22 @@ class AIlib: |
|
|
|
dCost = cost2 - cost1 |
|
|
|
dCost = cost2 - cost1 |
|
|
|
return dCost |
|
|
|
return dCost |
|
|
|
|
|
|
|
|
|
|
|
def compareInstance( obj, theta, layerIndex, neuronIndex_X=0, neuronIndex_Y=0 ): |
|
|
|
def compareInstanceWeight( obj, theta, layerIndex, neuronIndex_X=0, neuronIndex_Y=0 ): |
|
|
|
# Create new instances of the object |
|
|
|
# Create new a instance of the object |
|
|
|
obj2_w = copy(obj) # annoying way to create a new instance of the object |
|
|
|
obj2 = copy(obj) # annoying way to create a new instance of the object |
|
|
|
obj2_b = copy(obj) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
obj2_w.weights[layerIndex][neuronIndex_X][neuronIndex_Y] += theta # mutate the second objects neuron |
|
|
|
obj2.weights[layerIndex][neuronIndex_X][neuronIndex_Y] += theta # mutate the second objects neuron |
|
|
|
dCost_weight = AIlib.compareAIobjects( obj, obj2_w ) # compare the two and get the dCost with respect to the weights |
|
|
|
dCost = AIlib.compareAIobjects( obj, obj2 ) # compare the two and get the dCost with respect to the weights |
|
|
|
|
|
|
|
|
|
|
|
obj2_b.bias[layerIndex][neuronIndex_X][neuronIndex_Y] += theta # do the same thing for the bias |
|
|
|
return dCost |
|
|
|
dCost_bias = AIlib.compareAIobjects( obj, obj2_b ) |
|
|
|
|
|
|
|
|
|
|
|
def compareInstanceBias( obj, theta, layerIndex, biasIndex ): |
|
|
|
|
|
|
|
obj2 = copy(obj) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
obj2.bias[layerIndex][biasIndex] += theta # do the same thing for the bias |
|
|
|
|
|
|
|
dCost = AIlib.compareAIobjects( obj, obj2 ) |
|
|
|
|
|
|
|
|
|
|
|
return dCost_weight, dCost_bias |
|
|
|
return dCost |
|
|
|
|
|
|
|
|
|
|
|
def getChangeInCost( obj, theta, layerIndex ): |
|
|
|
def getChangeInCost( obj, theta, layerIndex ): |
|
|
|
mirrorObj = copy(obj) |
|
|
|
mirrorObj = copy(obj) |
|
|
@ -73,6 +77,8 @@ class AIlib: |
|
|
|
mirrorObj.weights[layerIndex].fill(None) |
|
|
|
mirrorObj.weights[layerIndex].fill(None) |
|
|
|
mirrorObj.bias[layerIndex].fill(None) |
|
|
|
mirrorObj.bias[layerIndex].fill(None) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 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 |
|
|
|
# Check if grads exists, if not create the buffer |
|
|
|
if( not grads ): |
|
|
|
if( not grads ): |
|
|
|