Made the gradient calculations much better

pull/1/head
E. Almqvist 4 years ago
parent 0a754de2e9
commit 6d5fc63e05
  1. 47
      rgbAI/lib/func.py

@ -41,7 +41,7 @@ class AIlib:
# Calculate the partial derivative for that prop # Calculate the partial derivative for that prop
return dCost / dProp return dCost / dProp
def compareAIobjects( obj1, obj2 ): def compareAIobjects( inp, obj1, obj2 ):
# Compare the two instances # Compare the two instances
res1 = AIlib.think( inp, obj1 ) res1 = AIlib.think( inp, obj1 )
cost1 = AIlib.getThinkCost( inp, res1 ) # get the cost cost1 = AIlib.getThinkCost( inp, res1 ) # get the cost
@ -51,31 +51,46 @@ class AIlib:
# Actually calculate stuff # Actually calculate stuff
dCost = cost2 - cost1 dCost = cost2 - cost1
return dCost return dCost, cost1
def compareInstanceWeight( obj, theta, layerIndex, neuronIndex_X=0, neuronIndex_Y=0 ): def compareInstanceWeight( obj, inp, theta:float, layerIndex:int, neuronIndex_X:int, neuronIndex_Y:int ):
# Create new a instance of the object # Create new a instance of the object
obj2 = 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.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 = AIlib.compareAIobjects( obj, obj2 ) # compare the two and get the dCost with respect to the weights dCost, curCost = AIlib.compareAIobjects( inp, obj, obj2 ) # compare the two and get the dCost with respect to the weights
return dCost return dCost, curCost
def compareInstanceBias( obj, theta, layerIndex, biasIndex ): def compareInstanceBias( obj, inp, theta:float, layerIndex:int, biasIndex:int ):
obj2 = copy(obj) obj2 = copy(obj)
obj2.bias[layerIndex][biasIndex] += theta # do the same thing for the bias obj2.bias[layerIndex][0][biasIndex] += theta # do the same thing for the bias
dCost = AIlib.compareAIobjects( obj, obj2 ) dCost, curCost = AIlib.compareAIobjects( inp, obj, obj2 )
return dCost return dCost, curCost
def getChangeInCost( obj, theta, layerIndex ): def getChangeInCost( obj, inp, theta, layerIndex ):
mirrorObj = copy(obj) mirrorObj = copy(obj)
# Fill the buffer with None so that the dCost can replace it later # Fill the buffer with None so that the dCost can replace it later
mirrorObj.weights[layerIndex].fill(None) dCost_W = np.zeros( shape = mirrorObj.weights[layerIndex].shape ) # fill it with a placeholder
mirrorObj.bias[layerIndex].fill(None) dCost_B = np.zeros( shape = mirrorObj.bias[layerIndex].shape )
# Get the cost change for the weights
weightLenX = len(dCost_W)
weightLenY = len(dCost_W[0])
for x in range(weightLenX): # get the dCost for each x,y
for y in range(weightLenY):
dCost_W[x][y], curCostWeight = AIlib.compareInstanceWeight( obj, inp, theta, layerIndex, x, y )
# Get the cost change for the biases
biasLenY = len(dCost_B[0])
for index in range(biasLenY):
dCost_B[0][index], curCostBias = AIlib.compareInstanceBias( obj, inp, theta, layerIndex, index )
return dCost_W, dCost_B, (curCostBias + curCostWeight)/2
@ -84,6 +99,8 @@ class AIlib:
if( not grads ): if( not grads ):
grads = [None] * (maxLayer+1) grads = [None] * (maxLayer+1)
dCost_W, dCost_B, meanCurCost = AIlib.getChangeInCost( obj, inp, theta, layerIndex )
# Calculate the gradient for the layer # Calculate the gradient for the layer
weightDer = AIlib.propDer( dCost_W, theta ) weightDer = AIlib.propDer( dCost_W, theta )
biasDer = AIlib.propDer( dCost_B, theta ) biasDer = AIlib.propDer( dCost_B, theta )
@ -98,7 +115,7 @@ class AIlib:
if( newLayer <= maxLayer ): if( newLayer <= maxLayer ):
return AIlib.gradient( inp, obj, theta, maxLayer, newLayer, grads, obj1, obj2 ) return AIlib.gradient( inp, obj, theta, maxLayer, newLayer, grads, obj1, obj2 )
else: else:
return grads, res1, cost1 return grads, meanCurCost
def mutateProps( inpObj, maxLen:int, gradient:list ): def mutateProps( inpObj, maxLen:int, gradient:list ):
obj = copy(inpObj) obj = copy(inpObj)
@ -120,10 +137,10 @@ class AIlib:
while( not curCost or curCost > targetCost ): # targetCost is the target for the cost function while( not curCost or curCost > targetCost ): # targetCost is the target for the cost function
maxLen = len(obj.bias) maxLen = len(obj.bias)
grads, res, curCost = AIlib.gradient( inp, obj, theta, maxLen - 1 ) grads, curCost = AIlib.gradient( inp, obj, theta, maxLen - 1 )
obj = AIlib.mutateProps( obj, maxLen, grads ) # mutate the props for next round obj = AIlib.mutateProps( obj, maxLen, grads ) # mutate the props for next round
print("Cost:", curCost, "|", inp, res) print("Cost:", curCost)
print("DONE\n") print("DONE\n")

Loading…
Cancel
Save