Added recursive backprop

pull/1/head
E. Almqvist 4 years ago
parent d928c454e1
commit bb7297a676
  1. 14
      rgbAI/lib/func.py

@ -34,7 +34,7 @@ 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, layerIndex: int=0, obj1: None, obj2: None ): def gradient( inp:np.array, obj, theta:float, maxLayer:int, layerIndex: int=0, grads: list=[], obj1=None, obj2=None ):
# Calculate the gradient for that prop # Calculate the gradient for that prop
# Create new instances of the object # Create new instances of the object
@ -61,6 +61,18 @@ class AIlib:
weightDer = AIlib.propDer( dCost, dWeight ) weightDer = AIlib.propDer( dCost, dWeight )
biasDer = AIlib.propDer( dCost, dBias ) biasDer = AIlib.propDer( dCost, dBias )
# Append the gradients to the list
grads[layerIndex] = {
"weight": weightDer,
"bias": biasDer
}
newLayer = layerIndex + 1
if( newLayer <= maxLayer ):
return AIlib.gradient( inp, obj, theta, maxLayer, newLayer, grads, obj1, obj2 )
else:
return grads
def mutateProp( prop:list, lr:float, gradient ): def mutateProp( prop:list, lr:float, gradient ):
newProp = [None] * len(prop) newProp = [None] * len(prop)

Loading…
Cancel
Save