From 0a754de2e987c91f5cc364c66809544e879ef942 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Sat, 17 Oct 2020 17:08:58 +0200 Subject: [PATCH] Added instance compairing for bias and weights --- rgbAI/lib/func.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/rgbAI/lib/func.py b/rgbAI/lib/func.py index b93a857..ca5a9e3 100644 --- a/rgbAI/lib/func.py +++ b/rgbAI/lib/func.py @@ -53,18 +53,22 @@ class AIlib: dCost = cost2 - cost1 return dCost - def compareInstance( obj, theta, layerIndex, neuronIndex_X=0, neuronIndex_Y=0 ): - # Create new instances of the object - obj2_w = copy(obj) # annoying way to create a new instance of the object - obj2_b = copy(obj) + def compareInstanceWeight( obj, theta, layerIndex, neuronIndex_X=0, neuronIndex_Y=0 ): + # Create new a instance of the object + obj2 = copy(obj) # annoying way to create a new instance of the object - obj2_w.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 + obj2.weights[layerIndex][neuronIndex_X][neuronIndex_Y] += theta # mutate the second objects neuron + 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 - dCost_bias = AIlib.compareAIobjects( obj, obj2_b ) + return dCost + + 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 ): mirrorObj = copy(obj) @@ -73,6 +77,8 @@ class AIlib: mirrorObj.weights[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 # Check if grads exists, if not create the buffer if( not grads ):