Do several forward passes before one backward pass

I am looking for a way to manually call a backwards pass on a model.

This page implies that there is a sd.execBackwards() function in 1.0.0-M2.1, but it doesn’t seem to actually exist in the library (neither does sd.exec()). Has the function been renamed, or is there an equivalent?

For context, this is what I need to do: during training, make a model loop on its own output several times:
for (int i = 0; i < 10; i++) {
y = model.output(y);
}
and only after 10 iterations calculate the loss on y and label & do the backwards pass. So far I haven’t found the functions that would allow me to do that. There is a backpropGradient() function that looks like it could be useful, and a computeGradientAndScore() function… Am I on the right track?

I went with

for (int i = 0; i < 5; i++) { model.setInput(input); List<INDArray> activations = model.feedForward(true, false); input = activations.get(activations.size()-1); }

and then same as (here error is for demo purposes) deeplearning4j-examples/dl4j-examples/src/main/java/org/deeplearning4j/examples/advanced/features/externalerrors/MultiLayerNetworkExternalErrors.java at master · deeplearning4j/deeplearning4j-examples · GitHub



INDArray externalError = input.sub(Nd4j.ones(n_batch, nOut).mul(0.4));
//Calculate backprop gradient based on error array
Pair<Gradient, INDArray> p = model.backpropGradient(externalError, null);

//Update the gradient: apply learning rate, momentum, etc
//This modifies the Gradient object in-place
Gradient gradient = p.getFirst();
int iteration = 0;
int epoch = 0;
// !!! there we go
model.getUpdater().update(model, gradient, iteration, epoch, n_batch, LayerWorkspaceMgr.noWorkspaces());

//Get a row vector gradient array, and apply it to the parameters to update the model
INDArray updateVector = gradient.gradient();
model.params().subi(updateVector);

Hopefully that’s correct

So, this doesn’t work because I need to do the equivalent of tensorflow’s tf.GradientTape, ie record the operations that have happened in the loop before doing the backwards pass.
Any help on how to do that with this framework appreciated

I am stuck and could really use some basic help. How about something simple like this example from the docs:

The example does not use an error that is based on the actual output. I also don’t know if some secret thing has to be done between epochs, like gradient.clear or similar. Does anyone have a real functioning example of using external errors? I have tried getting a simple network to learn to ouptut the value 0.4, but while the inbuilt “fit” works with mse, the example based on external error (still with mse) just has the loss increasing at each epoch.

At this point I am just talking to myself, but I want to point out that this line in the code is wrong:

The function should apparently be called with epsilon = error .* activation, not directly with the error.

… I still can’t get any simple example to converge, but I thought I’d share in case anyone is trying to use the docs.

At least with that corrected, I don’t get an ever increasing loss anymore. I just get all the weights to become 0 :melting_face: