Class Optimizer

java.lang.Object
org.tensorflow.framework.optimizers.Optimizer
Direct Known Subclasses:
AdaDelta, AdaGrad, AdaGradDA, Adam, Adamax, Ftrl, GradientDescent, Momentum, Nadam, RMSProp

public abstract class Optimizer extends Object
Base class for gradient optimizers.
  • Field Details

    • VARIABLE_V2

      public static final String VARIABLE_V2
      See Also:
    • globals

      protected final List<Variable<?>> globals
      Global state variables
    • graph

      protected final Graph graph
      The Graph this optimizer is operating on.
    • tf

      protected final Ops tf
      The ops builder for the graph.
  • Constructor Details

    • Optimizer

      protected Optimizer(Graph graph)
      Builds an optimizer for the supplied graph.

      Uses the name from getOptimizerName() to name the operations.

      Parameters:
      graph - The graph to optimize.
    • Optimizer

      protected Optimizer(Graph graph, String name)
      Builds an optimizer for the supplied graph.
      Parameters:
      graph - The graph to optimize.
      name - The base name for the operations.
  • Method Details

    • createName

      public static String createName(Output<? extends TType> variable, String slotName)
      Creates a name by combining a variable name and a slot name
      Parameters:
      variable - the variable
      slotName - the name of the slot
      Returns:
      the combined name
    • getTF

      public final Ops getTF()
      Gets the Optimizer's Ops instance
      Returns:
      the Optimizer's Ops instance
    • minimize

      public Op minimize(Operand<?> loss)
      Minimizes the loss by updating the variables
      Parameters:
      loss - the loss operation that returns the value to minimize
      Returns:
      returns op that minimizes the loss by updating the listed variables
    • minimize

      public Op minimize(Operand<?> loss, String name)
      Minimizes the loss by updating the variables
      Parameters:
      loss - the loss operation that returns the value to minimize
      name - the name for the minimize operation
      Returns:
      op that minimizes the loss by updating the listed variables
    • computeGradients

      public <T extends TType> List<Optimizer.GradAndVar<?>> computeGradients(Operand<?> loss)
      Computes the gradients based on a loss operand.
      Type Parameters:
      T - the data type of the loss, gradients and variables.
      Parameters:
      loss - the loss operation
      Returns:
      the computed gradients
    • applyGradients

      public Op applyGradients(List<Optimizer.GradAndVar<? extends TType>> gradsAndVars, String name)
      Applies gradients to variables
      Parameters:
      gradsAndVars - the list of (gradient, variable) pairs.
      name - the name of the apply gradients operation
      Returns:
      an Op that applies the gradients to the variables.
    • getSlot

      public <T extends TType> Optional<Variable<T>> getSlot(Output<T> var, String slotName)
      Gets the slot associated with the specified variable and slot name.
      Type Parameters:
      T - a tensor type
      Parameters:
      var - The variable to lookup.
      slotName - The slot name.
      Returns:
      The slot or Optional.empty().
    • createSlot

      protected <T extends TType> void createSlot(Output<T> variable, String slotName, Operand<T> initializer)
      Creates a slot in the graph for the specified variable with the specified name. Adds the slot's initializer to the graph's initializers, and the slot to the Optimizer's slot map.
      Type Parameters:
      T - The type of the variable.
      Parameters:
      variable - The variable to create the slot for.
      slotName - The name of the slot.
      initializer - The initializer for the slot.
    • prepare

      protected Optional<Op> prepare(String scopeName)
      Returns a No-op prepare.
      Parameters:
      scopeName - The scope name to use for any variable creations.
      Returns:
      a No-op to prepare this optimizer, or empty if none.
    • createSlots

      protected void createSlots(List<Output<? extends TType>> variables)
      Performs a No-op slot creation method.
      Parameters:
      variables - The variables to create slots for.
    • applyDense

      protected abstract <T extends TType> Op applyDense(Ops opDependencies, Output<T> gradient, Output<T> variable)
      Generates the gradient update operations for the specific variable and gradient.
      Type Parameters:
      T - The type of the variable.
      Parameters:
      gradient - The gradient to use.
      variable - The variable to update.
      Returns:
      An operand which applies the desired optimizer update to the variable.
    • finish

      protected Op finish(List<Op> updateOperations, String name)
      Gathers up the update operations into a single op that can be used as a run target.
      Parameters:
      updateOperations - The update operations.
      name - The name of the run target.
      Returns:
      A NoOp with a control dependency on each update operation.
    • getOptimizerName

      public abstract String getOptimizerName()
      Get the Name of the optimizer.
      Returns:
      The optimizer name.