From bb7297a67605f5c0f99d667a95f2a9adcfb82949 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Wed, 30 Sep 2020 17:24:09 +0200 Subject: [PATCH] Added recursive backprop --- rgbAI/lib/func.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/rgbAI/lib/func.py b/rgbAI/lib/func.py index a01d4d8..ba501bd 100644 --- a/rgbAI/lib/func.py +++ b/rgbAI/lib/func.py @@ -34,7 +34,7 @@ class AIlib: # Calculate the partial derivative for that prop 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 # Create new instances of the object @@ -61,6 +61,18 @@ class AIlib: weightDer = AIlib.propDer( dCost, dWeight ) 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 ): newProp = [None] * len(prop)