Class JacobianSerialChain<T>
- All Implemented Interfaces:
VerbosePrint
Sequential chain of operations for forward and backwards calculations used to compute Jacobians using the chain rule. The first input for all operators is where the output of the previous operator is passed in. All other inputs for an operator must be externally handled. When adding a new operator you then specify where the input came from so that it can be passed into the larger system Jacobian
Usage:
- Call
invalid reference
#setPublishJac
- Call
add(CROperatorBackwardsJac)for each operator in the chain in order from first to last - Call
in(int, int)after a call toadd(CROperatorBackwardsJac)and specify where the input came from - Call
chain(int...)for each chain of ops you wish to process - Call
backwardsF(int, CRArray)performs the backward pass to get the Jacobian, which invokesforward(int, CRArray, CRArray)internally
Constraints:
- An operator instance may appear at most once in a chain.
savedInputsis overwritten on each call to forward.backwards(int)only produces valid results if the previous call toforward(int, CRArray, CRArray)was done on the same chain. UsebackwardsF(int, CRArray)instead to avoid this bookkeeping issue.
Explanation:
- Operators keep their inputs as state, so reusing one would have the second copy overwrite the first,
and the backwards pass would then differentiate the wrong inputs.
chain(int...)rejects it
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceUsed to pass a locally computed Jacobian into the system Jacobianprotected static classStorage for an operator an any associated adatastatic interfaceprotected classDescribes a sequence of blocks that will be processedstatic interfaceCalled once every input is set and before the operator runs. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(CROperatorBackwardsJac<T> op) Adds a new operation to the chain and internally creates a blockvoidbackwards(int chainID) Performs backwards pass and computes Jacobians for each operator in the chain.voidbackwardsF(int chainID, CRArray<T> input) Same asbackwards(int), except that it runs a forward pass to get the inputs then runs the backwards pass to compute Jacobians.intblockInputSize(int chainID, int sequenceIdx) Size of first input to the specified block in the chainintchain(int... ids) Creates a new chain that can be invoked later on.voidPerforms the chain in the forward directly while saving all the intermediate inputs insavedInputsarrayintgetOutputSize(int chainID) Size of output vector from the chainin(int input, int parameter) Specifies if an input is being optimized and where the optimized parameter is stored in the system's parameter vectorinputIdx(int idx) Used to specify which input should the output from the previous block be passed into.voidinsertInputs(int chainID, T params) Helper function that will automatically pass in all mapped inputsintReturns the ID for the most recently added blockvoidsetCapture(int blockIdx, int input, JacobianSerialChain.CaptureJacobian<T> capture) Specifies a custom function for handling a block's Jacobian.voidsetInputMap(int blockIdx, int input, int parameter) Used to set the input mapping for a block after it was first addedvoidsetPreForward(int blockIdx, JacobianSerialChain.PreForward<T> preForward) Specifies a block's pre-forward hook.voidsetVerbose(@Nullable PrintStream out, @Nullable Set<String> configuration) If set to a non-null output then extra information will be printed to the specified stream.
-
Constructor Details
-
JacobianSerialChain
-
-
Method Details
-
add
Adds a new operation to the chain and internally creates a block -
lastBlockId
public int lastBlockId()Returns the ID for the most recently added block -
inputIdx
Used to specify which input should the output from the previous block be passed into. By default, input[0] is used. This allows you to specify input[i]. Modifies the most recently added block. -
in
Specifies if an input is being optimized and where the optimized parameter is stored in the system's parameter vector- Parameters:
input- Which input in the array corresponds to this parameterparameter- Index in the entire system's parameter vector
-
setInputMap
public void setInputMap(int blockIdx, int input, int parameter) Used to set the input mapping for a block after it was first added -
setCapture
Specifies a custom function for handling a block's Jacobian. This is needed when there are multiple chains that form a graph. I.e. the output of a block in chain A is an input to a block in chain B.
A capture on the block's chain input is called once at the end of
backwards(int). Every other input's capture is called as its own block is differentiated -
setPreForward
Specifies a block's pre-forward hook. SeeJacobianSerialChain.Block.preForward -
chain
public int chain(int... ids) Creates a new chain that can be invoked later on. You pass in IDs for all the blocks you wish to add. It will then check the sequence for consistency and precompute anything that's needed. Returns the ID of the new chain that you will need to call it late ron.- Parameters:
ids- Sequence of block IDs.
-
insertInputs
Helper function that will automatically pass in all mapped inputs- Parameters:
chainID- Which chain are you inserting inputs forparams- Parameters from the system array
-
forward
Performs the chain in the forward directly while saving all the intermediate inputs insavedInputsarray- Parameters:
chainID- Which chain it should runinput- Initial input that's fed into the first blockoutput- Storage for output. If null output will not be computed.
-
backwardsF
Same asbackwards(int), except that it runs a forward pass to get the inputs then runs the backwards pass to compute Jacobians. This makes the pair self-contained when several chains are being driven. -
backwards
public void backwards(int chainID) Performs backwards pass and computes Jacobians for each operator in the chain. You must callforward(int, CRArray, CRArray)first to get the inputs for each operation.- Parameters:
chainID- Which chain it should perform the backwards pass on
-
getOutputSize
public int getOutputSize(int chainID) Size of output vector from the chain -
blockInputSize
public int blockInputSize(int chainID, int sequenceIdx) Size of first input to the specified block in the chain -
setVerbose
public void setVerbose(@Nullable @Nullable PrintStream out, @Nullable @Nullable Set<String> configuration) Description copied from interface:VerbosePrintIf set to a non-null output then extra information will be printed to the specified stream.- Specified by:
setVerbosein interfaceVerbosePrint- Parameters:
out- Stream that is printed to. Set to null to disableconfiguration- (Future use) Set which specifies flags that can be used to turn on and off different output
-