Class FeedforwardDependencyTrainer.Optimizer

java.lang.Object
opennlp.tools.depparse.FeedforwardDependencyTrainer.Optimizer
Enclosing class:
FeedforwardDependencyTrainer

private static final class FeedforwardDependencyTrainer.Optimizer extends Object
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 Details

    • network

      private final FeedforwardNetwork network
    • settings

      private final FeedforwardDependencyTrainer.Settings settings
    • embeddingSize

      private final int embeddingSize
    • hiddenSize

      private final int hiddenSize
    • outputSize

      private final int outputSize
    • embeddingAccumulator

      private final double[][] embeddingAccumulator
    • hiddenAccumulator

      private final double[][] hiddenAccumulator
    • hiddenBiasAccumulator

      private final double[] hiddenBiasAccumulator
    • outputAccumulator

      private final double[][] outputAccumulator
    • outputBiasAccumulator

      private final double[] outputBiasAccumulator
    • hiddenGradient

      private final double[][] hiddenGradient
    • hiddenBiasGradient

      private final double[] hiddenBiasGradient
    • outputGradient

      private final double[][] outputGradient
    • outputBiasGradient

      private final double[] outputBiasGradient
    • embeddingGradient

      private final double[][] embeddingGradient
      The embedding gradient rows, allocated on first touch and reused.
    • touchedRows

      private final int[] touchedRows
      The rows with a nonzero embedding gradient since the last clear.
    • touched

      private final boolean[] touched
    • touchedCount

      private int touchedCount
    • pre

      private final double[] pre
    • hidden

      private final double[] hidden
    • scores

      private final double[] scores
    • outputDelta

      private final double[] outputDelta
    • hiddenDelta

      private final double[] hiddenDelta
    • featureEmbeddings

      private final float[][] featureEmbeddings
      The embedding rows of the example being backpropagated, one per feature position.
    • featureGradients

      private final double[][] featureGradients
      The gradient rows of those embeddings, one per feature position.
    • collected

      private final List<ArcStandardBeam.Node> collected
      The nodes collected for one refinement update.
  • Constructor Details

    • Optimizer

      private Optimizer(FeedforwardNetwork network, FeedforwardDependencyTrainer.Settings settings)
      Sizes the accumulators and scratch buffers for one run.
      Parameters:
      network - The network the run updates in place.
      settings - The hyperparameters; learningRate and l2 apply.
  • 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, or null to keep all.
      keep - The probability a hidden unit is kept; 1 without dropout.
    • backward

      private void backward(int[] features, boolean[] mask, double keep)
      Propagates outputDelta, the loss gradient on the raw output scores, back through the layers of the last forward(int[], boolean[], double) pass into the gradient buffers.
      Parameters:
      features - The embedding rows of the configuration.
      mask - The hidden units kept by dropout, or null to keep all.
      keep - The probability a hidden unit is kept; 1 without 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 and ArcStandardState.canApply(Transition) disagree, so the gold derivation cannot be followed.
    • updateFromCandidates

      private double updateFromCandidates(List<ArcStandardBeam.Node> candidates)
      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.