Package opennlp.tools.depparse
Class FeedforwardDependencyTrainer.Optimizer
java.lang.Object
opennlp.tools.depparse.FeedforwardDependencyTrainer.Optimizer
- Enclosing class:
FeedforwardDependencyTrainer
The forward pass, backward pass, gradient buffers, and AdaGrad state shared by
local training and beam refinement. Dropout is an optional mask; without it the
pass is the inference computation of
FeedforwardNetwork.score(int[], opennlp.tools.depparse.ContributionCache, double[], double[]).-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List<ArcStandardBeam.Node> The nodes collected for one refinement update.private final double[][]private final double[][]The embedding gradient rows, allocated on first touch and reused.private final intprivate final float[][]The embedding rows of the example being backpropagated, one per feature position.private final double[][]The gradient rows of those embeddings, one per feature position.private final double[]private final double[][]private final double[]private final double[]private final double[]private final double[][]private final intprivate final FeedforwardNetworkprivate final double[][]private final double[]private final double[]private final double[]private final double[][]private final intprivate final double[]private final double[]private final FeedforwardDependencyTrainer.Settingsprivate final boolean[]private intprivate final int[]The rows with a nonzero embedding gradient since the last clear. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateOptimizer(FeedforwardNetwork network, FeedforwardDependencyTrainer.Settings settings) Sizes the accumulators and scratch buffers for one run. -
Method Summary
Modifier and TypeMethodDescriptionprivate doubleaccumulate(int[] features, int gold, boolean[] mask, double keep) Runs one example forward and adds its cross-entropy gradient to the buffers.private voidapplyUpdates(int batch) Applies one AdaGrad step from the gradient buffers to every parameter.private voidbackpropagate(int[] features, int chosen, double weight) Adds the gradient of one decoded step to the buffers: the weighted difference between the step's softmax and its chosen transition.private voidbackward(int[] features, boolean[] mask, double keep) PropagatesoutputDelta, the loss gradient on the raw output scores, back through the layers of the lastforward(int[], boolean[], double)pass into the gradient buffers.private voidZeroes the gradient buffers before a batch.private voidforward(int[] features, boolean[] mask, double keep) Computes hidden activations and raw output scores into the scratch buffers.private doublerefineSentence(DependencySample sample, int[] oracle, Transition[] transitions, int beamSize) Decodes one sentence with the beam, updating on the early-update point or the final beam.private double[]touch(int row) Marks an embedding row as touched and returns its gradient row.private doubleupdateFromCandidates(List<ArcStandardBeam.Node> candidates) Applies the conditional-likelihood update over the candidate paths.
-
Field Details
-
network
-
settings
-
embeddingSize
private final int embeddingSize -
outputSize
private final int outputSize -
embeddingAccumulator
private final double[][] embeddingAccumulator -
outputAccumulator
private final double[][] outputAccumulator -
outputBiasAccumulator
private final double[] outputBiasAccumulator -
outputGradient
private final double[][] outputGradient -
outputBiasGradient
private final double[] outputBiasGradient -
embeddingGradient
private final double[][] embeddingGradientThe embedding gradient rows, allocated on first touch and reused. -
touchedRows
private final int[] touchedRowsThe rows with a nonzero embedding gradient since the last clear. -
touched
private final boolean[] touched -
touchedCount
private int touchedCount -
pre
private final double[] pre -
scores
private final double[] scores -
outputDelta
private final double[] outputDelta -
featureEmbeddings
private final float[][] featureEmbeddingsThe embedding rows of the example being backpropagated, one per feature position. -
featureGradients
private final double[][] featureGradientsThe gradient rows of those embeddings, one per feature position. -
collected
The nodes collected for one refinement update.
-
-
Constructor Details
-
Optimizer
Sizes the accumulators and scratch buffers for one run.- Parameters:
network- The network the run updates in place.settings- The hyperparameters;learningRateandl2apply.
-
-
Method Details
-
clearGradients
private void clearGradients()Zeroes the gradient buffers before a batch. -
accumulate
private double accumulate(int[] features, int gold, boolean[] mask, double keep) Runs one example forward and adds its cross-entropy gradient to the buffers.- Parameters:
features- The embedding rows of the configuration.gold- The gold transition index.mask- The hidden units kept by dropout.keep- The probability a hidden unit is kept.- Returns:
- The example's negative log-likelihood.
-
forward
private void forward(int[] features, boolean[] mask, double keep) Computes hidden activations and raw output scores into the scratch buffers.- Parameters:
features- The embedding rows of the configuration.mask- The hidden units kept by dropout, ornullto keep all.keep- The probability a hidden unit is kept;1without dropout.
-
backward
private void backward(int[] features, boolean[] mask, double keep) PropagatesoutputDelta, the loss gradient on the raw output scores, back through the layers of the lastforward(int[], boolean[], double)pass into the gradient buffers.- Parameters:
features- The embedding rows of the configuration.mask- The hidden units kept by dropout, ornullto keep all.keep- The probability a hidden unit is kept;1without dropout.
-
touch
private double[] touch(int row) Marks an embedding row as touched and returns its gradient row.- Parameters:
row- The embedding row.- Returns:
- The gradient row, zeroed on first touch. Never
null.
-
applyUpdates
private void applyUpdates(int batch) Applies one AdaGrad step from the gradient buffers to every parameter.- Parameters:
batch- The number of examples the gradients sum over.
-
refineSentence
private double refineSentence(DependencySample sample, int[] oracle, Transition[] transitions, int beamSize) Decodes one sentence with the beam, updating on the early-update point or the final beam.- Parameters:
sample- The sentence.oracle- The gold transition indexes.transitions- The decoded transition inventory.beamSize- The beam width.- Returns:
- The sentence loss.
- Throws:
IllegalStateException- Thrown if the oracle andArcStandardState.canApply(Transition)disagree, so the gold derivation cannot be followed.
-
updateFromCandidates
Applies the conditional-likelihood update over the candidate paths. Each node is backpropagated once with the weight summed over the candidates passing through it, so shared prefixes whose weights cancel cost nothing.- Parameters:
candidates- The surviving paths plus, on early update, the gold path.- Returns:
- The negative log-probability of the gold path under the candidates.
-
backpropagate
private void backpropagate(int[] features, int chosen, double weight) Adds the gradient of one decoded step to the buffers: the weighted difference between the step's softmax and its chosen transition.- Parameters:
features- The step's input features.chosen- The transition the path took at this step.weight- The step's summed weight in the candidate distribution.
-