src/core/layer/OutputLayer.java
public void backwardProcess() throws MatrixException {
        resetLayerGradients();
        for (Integer sampleIndex : targets.keySet()) {
            for (Integer matrixIndex : targets.entryKeySet()) {
                Matrix currentOutputs = getLayerOutputs().get(sampleIndex, matrixIndex);
                Matrix currentTargets = targets.get(sampleIndex, matrixIndex);
                Matrix outputGradient;
                if (multiOutput) {
                    if (currentOutputs.getSubMatrices().size() != lossFunctions.size()) throw new MatrixException("Number of outputs is not matching with number of loss functions");
                    ArrayList<Matrix> totalGradient = new ArrayList<>();
                    int lossFunctionsSize = lossFunctions.size();
                    for (int index = 0; index < lossFunctionsSize; index++) {
                        LossFunction lossFunction = lossFunctions.get(index);
                        Matrix subOutputs = currentOutputs.getSubMatrices().get(index);
                        Matrix subTargets = currentTargets.getSubMatrices().get(index);
                        Matrix subOutputGradient = lossFunction.getGradient(subOutputs, subTargets);
                        totalGradient.add(subOutputGradient);
                    }
                    outputGradient = new JMatrix(totalGradient, true);
                }
                else {
                    outputGradient = lossFunction.getGradient(currentOutputs, currentTargets);
                }
                if (importanceSamplingWeights != null) outputGradient.multiply(importanceSamplingWeights.get(matrixIndex), outputGradient);
                getLayerGradients().put(sampleIndex, matrixIndex, outputGradient);
            }
        }
    }
/**
     * Executes backward step of output layer.
     *
     * @throws MatrixException throws exception if matrix operation fails.
     */
Executes backward step of output layer.

@throws MatrixException throws exception if matrix operation fails.
Executes backward step of output layer . @throws MatrixException throws exception if matrix operation fails .
public void backwardProcess() throws MatrixException {
        resetLayerGradients();
        for (Integer sampleIndex : targets.keySet()) {
            for (Integer matrixIndex : targets.entryKeySet()) {
                Matrix currentOutputs = getLayerOutputs().get(sampleIndex, matrixIndex);
                Matrix currentTargets = targets.get(sampleIndex, matrixIndex);
                Matrix outputGradient;
                if (multiOutput) {
                    if (currentOutputs.getSubMatrices().size() != lossFunctions.size()) throw new MatrixException("Number of outputs is not matching with number of loss functions");
                    ArrayList<Matrix> totalGradient = new ArrayList<>();
                    int lossFunctionsSize = lossFunctions.size();
                    for (int index = 0; index < lossFunctionsSize; index++) {
                        LossFunction lossFunction = lossFunctions.get(index);
                        Matrix subOutputs = currentOutputs.getSubMatrices().get(index);
                        Matrix subTargets = currentTargets.getSubMatrices().get(index);
                        Matrix subOutputGradient = lossFunction.getGradient(subOutputs, subTargets);
                        totalGradient.add(subOutputGradient);
                    }
                    outputGradient = new JMatrix(totalGradient, true);
                }
                else {
                    outputGradient = lossFunction.getGradient(currentOutputs, currentTargets);
                }
                if (importanceSamplingWeights != null) outputGradient.multiply(importanceSamplingWeights.get(matrixIndex), outputGradient);
                getLayerGradients().put(sampleIndex, matrixIndex, outputGradient);
            }
        }
    }
public void backwardProcess ( ) throws MatrixException { resetLayerGradients ( ) ; for ( Integer sampleIndex : targets . keySet ( ) ) { for ( Integer matrixIndex : targets . entryKeySet ( ) ) { Matrix currentOutputs = getLayerOutputs ( ) . get ( sampleIndex , matrixIndex ) ; Matrix currentTargets = targets . get ( sampleIndex , matrixIndex ) ; Matrix outputGradient ; if ( multiOutput ) { if ( currentOutputs . getSubMatrices ( ) . size ( ) != lossFunctions . size ( ) ) throw new MatrixException ( "Number of outputs is not matching with number of loss functions" ) ; ArrayList < Matrix > totalGradient = new ArrayList < > ( ) ; int lossFunctionsSize = lossFunctions . size ( ) ; for ( int index = 0 ; index < lossFunctionsSize ; index ++ ) { LossFunction lossFunction = lossFunctions . get ( index ) ; Matrix subOutputs = currentOutputs . getSubMatrices ( ) . get ( index ) ; Matrix subTargets = currentTargets . getSubMatrices ( ) . get ( index ) ; Matrix subOutputGradient = lossFunction . getGradient ( subOutputs , subTargets ) ; totalGradient . add ( subOutputGradient ) ; } outputGradient = new JMatrix ( totalGradient , true ) ; } else { outputGradient = lossFunction . getGradient ( currentOutputs , currentTargets ) ; } if ( importanceSamplingWeights != null ) outputGradient . multiply ( importanceSamplingWeights . get ( matrixIndex ) , outputGradient ) ; getLayerGradients ( ) . put ( sampleIndex , matrixIndex , outputGradient ) ; } } }
Executes backward step of output layer.
Executes backward step of output layer .