public List < SDVariable > diff ( List < SDVariable > i_v1 ) { List < SDVariable > vals = doDiff ( i_v1 ) ; if ( vals == null ) { throw new IllegalStateException ( "Error executing diff operation: doDiff returned null for op: " + this . opName ( ) ) ; } val outputVars = args ( ) ; for ( int i = 0 ; i < vals . size ( ) ; i ++ ) { SDVariable var = outputVars [ i ] ; SDVariable grad = var . getGradient ( ) ; if ( grad != null ) { SDVariable gradVar = f ( ) . add ( grad , vals . get ( i ) ) ; try { vals . set ( i , gradVar ) ; } catch ( UnsupportedOperationException e ) { throw new UnsupportedOperationException ( "Use a mutable list when returning values from " + this . getClass ( ) . getSimpleName ( ) + ".doDiff (e.g. Arrays.asList instead of Collections.singletonList)" , e ) ; } sameDiff . setGradientForVariableName ( var . getVarName ( ) , gradVar ) ; } else { SDVariable gradVar = vals . get ( i ) ; sameDiff . updateVariableNameAndReference ( gradVar , var . getVarName ( ) + "-grad" ) ; sameDiff . setGradientForVariableName ( var . getVarName ( ) , gradVar ) ; sameDiff . setForwardVariableForVarName ( gradVar . getVarName ( ) , var ) ; } } return vals ; }
Perform automatic differentiation wrt the input variables