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?
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);
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.