Package org.nd4j.autodiff.functions
Class DifferentialFunction
- java.lang.Object
-
- org.nd4j.autodiff.functions.DifferentialFunction
-
- Direct Known Subclasses:
BaseOp,DynamicCustomOp
public abstract class DifferentialFunction extends Object
-
-
Field Summary
Fields Modifier and Type Field Description protected int[]dimensionsprotected Object[]extraArgsprotected booleaninPlaceprotected StringownNameprotected booleanownNameSetWithDefaultprotected SameDiffsameDiffprotected INDArrayscalarValue
-
Constructor Summary
Constructors Constructor Description DifferentialFunction()DifferentialFunction(boolean sameDiff)DifferentialFunction(SameDiff sameDiff, boolean inPlace, Object[] extraArgs)DifferentialFunction(SameDiff sameDiff, boolean inPlace, SDVariable[] args)Add the various arguments for this functionDifferentialFunction(SameDiff sameDiff, Object[] extraArgs)DifferentialFunction(SameDiff sameDiff, Onnx.NodeProto node, Map<String,Onnx.AttributeProto> attributesForNode, Onnx.GraphProto graph)Initialize the function from the givenOnnx.NodeProtoDifferentialFunction(SameDiff sameDiff, SDVariable[] args)DifferentialFunction(SameDiff sameDiff, NodeDef nodeDef, Map<String,AttrValue> attributesForNode, GraphDef graph)Initialize the function from the givenNodeDef
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description SDVariablearg()Return the first argumentSDVariablearg(int num)Return the specified argument for this functionString[]argNames()SDVariable[]args()Return the arguments for a given functionMap<String,Map<String,AttributeAdapter>>attributeAdaptersForFunction()Returns theAttributeAdapters for each of the possible ops for import (typically tensorflow and onnx) SeeAttributeAdapterfor more information on what the adapter does.List<DataType>calculateOutputDataTypes(List<DataType> dataTypes)Calculate the data types for the output arrays.List<LongShapeDescriptor>calculateOutputShape()Calculate the output shape for this opList<LongShapeDescriptor>calculateOutputShape(OpContext oc)abstract voidclearArrays()Clear the input and output INDArrays, if any are setStringconfigFieldName()Returns the name of the field to be used for looking up field names.voidconfigureWithSameDiff(SameDiff sameDiff)List<SDVariable>diff(List<SDVariable> i_v1)Perform automatic differentiation wrt the input variablesabstract List<SDVariable>doDiff(List<SDVariable> f1)The actual implementation for automatic differentiation.DifferentialFunctiondup()Duplicate this functionbooleanequals(Object o)protected BooleangetBooleanFromProperty(String propertyName, Map<String,Object> properties)protected DoublegetDoubleValueFromProperty(String propertyName, Map<String,Object> properties)INDArraygetInputArgument(int index)protected IntegergetIntValueFromProperty(String propertyName, Map<String,Object> properties)protected LonggetLongValueFromProperty(String propertyName, Map<String,Object> properties)intgetNumOutputs()protected StringgetStringFromProperty(String propertyName, Map<String,Object> properties)ObjectgetValue(Field property)Get the value for a given property for this functioninthashCode()abstract voidinitFromOnnx(Onnx.NodeProto node, SameDiff initWith, Map<String,Onnx.AttributeProto> attributesForNode, Onnx.GraphProto graph)Iniitialize the function from the givenOnnx.NodeProtoabstract voidinitFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String,AttrValue> attributesForNode, GraphDef graph)Initialize the function from the givenNodeDefbooleanisConfigProperties()Returns true if the fields for this class should be looked up from a configuration class.SDVariablelarg()The left argument for this functionMap<String,Map<String,PropertyMapping>>mappingsForFunction()Returns the mappings for a given function ( for tensorflow and onnx import mapping properties of this function).abstract StringonnxName()The opName of this function in onnxString[]onnxNames()The opName of this function in onnxStringopName()The name of the opintopNum()The number of the op (mainly for old legacy XYZ ops likeOp)Op.TypeopType()The type of the opList<SDVariable>outputs()SDVariableoutputVariable()SDVariable[]outputVariables()Return the output variables for this differential function.abstract SDVariable[]outputVariables(String baseName)Return the output functions for this differential function.String[]outputVariablesNames()Map<String,Object>propertiesForFunction()Returns the properties for a given functionSDVariablerarg()The right argument for this function.voidreplaceArg(int i, SDVariable newArg)Replace argument at the specified indexprotected voidsetInstanceId()voidsetPropertiesForFunction(Map<String,Object> properties)voidsetValueFor(Field target, Object value)Set the value for this function.abstract StringtensorflowName()The opName of this function tensorflowString[]tensorflowNames()The opName of this function tensorflow
-
-
-
Constructor Detail
-
DifferentialFunction
public DifferentialFunction()
-
DifferentialFunction
public DifferentialFunction(boolean sameDiff)
-
DifferentialFunction
public DifferentialFunction(SameDiff sameDiff, NodeDef nodeDef, Map<String,AttrValue> attributesForNode, GraphDef graph)
Initialize the function from the givenNodeDef- Parameters:
nodeDef-
-
DifferentialFunction
public DifferentialFunction(SameDiff sameDiff, Onnx.NodeProto node, Map<String,Onnx.AttributeProto> attributesForNode, Onnx.GraphProto graph)
Initialize the function from the givenOnnx.NodeProto- Parameters:
node-
-
DifferentialFunction
public DifferentialFunction(SameDiff sameDiff, boolean inPlace, Object[] extraArgs)
- Parameters:
sameDiff-extraArgs-
-
DifferentialFunction
public DifferentialFunction(SameDiff sameDiff, Object[] extraArgs)
- Parameters:
sameDiff-extraArgs-
-
DifferentialFunction
public DifferentialFunction(SameDiff sameDiff, SDVariable[] args)
- Parameters:
sameDiff-args-
-
DifferentialFunction
public DifferentialFunction(SameDiff sameDiff, boolean inPlace, SDVariable[] args)
Add the various arguments for this function- Parameters:
sameDiff-inPlace-args-
-
-
Method Detail
-
attributeAdaptersForFunction
public Map<String,Map<String,AttributeAdapter>> attributeAdaptersForFunction()
Returns theAttributeAdapters for each of the possible ops for import (typically tensorflow and onnx) SeeAttributeAdapterfor more information on what the adapter does. Similar tomappingsForFunction(), the returned map contains aAttributeAdapterfor each field name when one is present. (It is optional for one to exist)_- Returns:
-
mappingsForFunction
public Map<String,Map<String,PropertyMapping>> mappingsForFunction()
Returns the mappings for a given function ( for tensorflow and onnx import mapping properties of this function). The mapping is indexed by field name. If the function has no properties, this returned map will be empty. Note that some functions have multiple names. This function returns a map indexed by each alias it has for a given name. These names include both onnx and tensorflow names (which might be 1 or more)- Returns:
-
propertiesForFunction
public Map<String,Object> propertiesForFunction()
Returns the properties for a given function- Returns:
-
configureWithSameDiff
public void configureWithSameDiff(SameDiff sameDiff)
-
getBooleanFromProperty
protected Boolean getBooleanFromProperty(String propertyName, Map<String,Object> properties)
-
getStringFromProperty
protected String getStringFromProperty(String propertyName, Map<String,Object> properties)
-
getIntValueFromProperty
protected Integer getIntValueFromProperty(String propertyName, Map<String,Object> properties)
-
getLongValueFromProperty
protected Long getLongValueFromProperty(String propertyName, Map<String,Object> properties)
-
getDoubleValueFromProperty
protected Double getDoubleValueFromProperty(String propertyName, Map<String,Object> properties)
-
getValue
public Object getValue(Field property)
Get the value for a given property for this function- Parameters:
property- the property to get- Returns:
- the value for the function if it exists
-
setValueFor
public void setValueFor(Field target, Object value)
Set the value for this function. Note that if value is null anND4JIllegalStateExceptionwill be thrown.- Parameters:
target- the target fieldvalue- the value to set
-
isConfigProperties
public boolean isConfigProperties()
Returns true if the fields for this class should be looked up from a configuration class.- Returns:
-
configFieldName
public String configFieldName()
Returns the name of the field to be used for looking up field names. This should be used in conjunction withisConfigProperties()to facilitate mapping fields for model import.- Returns:
-
replaceArg
public void replaceArg(int i, SDVariable newArg)Replace argument at the specified index- Parameters:
i- the indexnewArg- the new argument
-
outputVariables
public SDVariable[] outputVariables()
Return the output variables for this differential function. Note that this op *may* dynamically generate variable outputs.- Returns:
-
outputVariable
public SDVariable outputVariable()
- Returns:
- The output variable, or the first output variable, if multiple outputs exist
-
outputs
public List<SDVariable> outputs()
-
outputVariablesNames
public String[] outputVariablesNames()
-
outputVariables
public abstract SDVariable[] outputVariables(String baseName)
Return the output functions for this differential function.- Returns:
-
doDiff
public abstract List<SDVariable> doDiff(List<SDVariable> f1)
The actual implementation for automatic differentiation.- Parameters:
f1-- Returns:
-
args
public SDVariable[] args()
Return the arguments for a given function- Returns:
- the arguments for a given function
-
arg
public SDVariable arg(int num)
Return the specified argument for this function- Parameters:
num- Number of the argument. Must be in range 0 to numArgs - 1 inclusive- Returns:
- Specified argument
-
argNames
public String[] argNames()
-
arg
public SDVariable arg()
Return the first argument- Returns:
-
diff
public List<SDVariable> diff(List<SDVariable> i_v1)
Perform automatic differentiation wrt the input variables- Parameters:
i_v1- the input variables- Returns:
- the differentiated output wrt each input variable
-
setInstanceId
protected void setInstanceId()
-
opName
public String opName()
The name of the op- Returns:
-
opType
public Op.Type opType()
The type of the op- Returns:
-
opNum
public int opNum()
The number of the op (mainly for old legacy XYZ ops likeOp)- Returns:
-
getInputArgument
public INDArray getInputArgument(int index)
-
initFromTensorFlow
public abstract void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String,AttrValue> attributesForNode, GraphDef graph)
Initialize the function from the givenNodeDef- Parameters:
nodeDef-initWith-attributesForNode-graph-
-
initFromOnnx
public abstract void initFromOnnx(Onnx.NodeProto node, SameDiff initWith, Map<String,Onnx.AttributeProto> attributesForNode, Onnx.GraphProto graph)
Iniitialize the function from the givenOnnx.NodeProto- Parameters:
node-initWith-attributesForNode-graph-
-
larg
public SDVariable larg()
The left argument for this function- Returns:
-
rarg
public SDVariable rarg()
The right argument for this function. Note that this assumes that there are 2 args for this function, if 2 are not set, it throws anND4JIllegalStateException- Returns:
-
dup
public DifferentialFunction dup()
Duplicate this function- Returns:
-
calculateOutputShape
public List<LongShapeDescriptor> calculateOutputShape()
Calculate the output shape for this op- Returns:
- List of output shape descriptors
-
calculateOutputShape
public List<LongShapeDescriptor> calculateOutputShape(OpContext oc)
-
calculateOutputDataTypes
public List<DataType> calculateOutputDataTypes(List<DataType> dataTypes)
Calculate the data types for the output arrays. Though datatypes can also be inferred fromcalculateOutputShape(), this method differs in that it does not require the input arrays to be populated. This is important as it allows us to do greedy datatype inference for the entire net - even if arrays are not available.- Parameters:
dataTypes- The data types of the inputs- Returns:
- The data types of the outputs
-
onnxNames
public String[] onnxNames()
The opName of this function in onnx- Returns:
-
tensorflowNames
public String[] tensorflowNames()
The opName of this function tensorflow- Returns:
-
onnxName
public abstract String onnxName()
The opName of this function in onnx- Returns:
-
tensorflowName
public abstract String tensorflowName()
The opName of this function tensorflow- Returns:
-
getNumOutputs
public int getNumOutputs()
-
clearArrays
public abstract void clearArrays()
Clear the input and output INDArrays, if any are set
-
-