What is the basic code flow of manually forward and backward instead of directly using an optimizer?

By directly making use of an optimizer, we can write very short code to train a model. But in some extreme case, we have to write a code flow which needs to manually control forward and backward such as calling zero gradients, calculateGradients, update, etc… in a loop. How to write this flow? Could please provide code?

@TempKonduitUser1 take a look at this for workign with external gradients: deeplearning4j-examples/MultiLayerNetworkExternalErrors.java at master · deeplearning4j/deeplearning4j-examples · GitHub