code: public SDVariable getGradForVariable(String varName) {
          Preconditions.checkState(variables.containsKey(varName), "No variable with name \"%s\" exists", varName);
          SDVariable v = getVariable(varName);
          Preconditions.checkState(v.dataType().isFPType(), "Cannot get gradient of %s variable \"%s\": only floating" +
                  " point variables have gradients", varName, v.dataType());
          //Gradients are being placed in the inner "grad" function SameDiff instance, but not the outer one
          if (variables.containsKey(varName) && variables.get(varName).getGradient() != null) {
              return variables.get(varName).getGradient();
          } else if(sameDiffFunctionInstances.containsKey("grad") && sameDiffFunctionInstances.get("grad").variables.containsKey(varName)){
              return sameDiffFunctionInstances.get("grad").variables.get(varName).getGradient();
          }
          return null;
      }
docs: Get the gradient for the variable with the specified name.<br>
  The gradient variable is the variable that represents the derivative of the loss function with respect
  to the output of this variable. I.e., if this variable is X and loss function is L, then gradient() returns the
  variable representing dL/dX<br>
  Note that only floating point variables can have gradients.<br>
  Note also that a gradient may not yet be defined, and/or if no loss function variables have been set.<br>
  You can set the loss function variables using {@link SameDiff#setLossVariables(String...)} and then create the
  gradient functions using {@link SameDiff#createGradFunction()}. Alternatively, the gradient function will be
  created automatically when training is performed.
  
  @param varName the vertex id
  @return the gradient for this variable or null