當前位置: 首頁>>代碼示例>>Python>>正文


Python adam.AdamOptimizer方法代碼示例

本文整理匯總了Python中tensorflow.python.training.adam.AdamOptimizer方法的典型用法代碼示例。如果您正苦於以下問題:Python adam.AdamOptimizer方法的具體用法?Python adam.AdamOptimizer怎麽用?Python adam.AdamOptimizer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tensorflow.python.training.adam的用法示例。


在下文中一共展示了adam.AdamOptimizer方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_optimizer_instance

# 需要導入模塊: from tensorflow.python.training import adam [as 別名]
# 或者: from tensorflow.python.training.adam import AdamOptimizer [as 別名]
def get_optimizer_instance(opt, learning_rate=None):
  """Returns an optimizer instance.

  Supports the following types for the given `opt`:
  * An `Optimizer` instance: Returns the given `opt`.
  * A string: Creates an `Optimizer` subclass with the given `learning_rate`.
    Supported strings:
    * 'Adagrad': Returns an `AdagradOptimizer`.
    * 'Adam': Returns an `AdamOptimizer`.
    * 'Ftrl': Returns an `FtrlOptimizer`.
    * 'RMSProp': Returns an `RMSPropOptimizer`.
    * 'SGD': Returns a `GradientDescentOptimizer`.

  Args:
    opt: An `Optimizer` instance, or string, as discussed above.
    learning_rate: A float. Only used if `opt` is a string.

  Returns:
    An `Optimizer` instance.

  Raises:
    ValueError: If `opt` is an unsupported string.
    ValueError: If `opt` is a supported string but `learning_rate` was not
      specified.
    ValueError: If `opt` is none of the above types.
  """
  if isinstance(opt, six.string_types):
    if opt in six.iterkeys(_OPTIMIZER_CLS_NAMES):
      if not learning_rate:
        raise ValueError('learning_rate must be specified when opt is string.')
      return _OPTIMIZER_CLS_NAMES[opt](learning_rate=learning_rate)
    raise ValueError(
        'Unsupported optimizer name: {}. Supported names are: {}'.format(
            opt, tuple(sorted(six.iterkeys(_OPTIMIZER_CLS_NAMES)))))
  if not isinstance(opt, optimizer_lib.Optimizer):
    raise ValueError(
        'The given object is not an Optimizer instance. Given: {}'.format(opt))
  return opt 
開發者ID:PacktPublishing,項目名稱:Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda,代碼行數:40,代碼來源:optimizers.py

示例2: get

# 需要導入模塊: from tensorflow.python.training import adam [as 別名]
# 或者: from tensorflow.python.training.adam import AdamOptimizer [as 別名]
def get(self, name=None):
        params = {} if self.params is None else self.params
        if self.opt_name == "Adam":
            if name is None:
                return AdamOptimizer(**params)
            else:
                return AdamOptimizer(name=name, **params)
        elif self.opt_name == "Adadelta":
            if name is None:
                return AdadeltaOptimizer(**params)
            else:
                return AdadeltaOptimizer(name=name, **params)
        else:
            raise NotImplemented() 
開發者ID:allenai,項目名稱:document-qa,代碼行數:16,代碼來源:trainer.py

示例3: extend_with_decoupled_weight_decay

# 需要導入模塊: from tensorflow.python.training import adam [as 別名]
# 或者: from tensorflow.python.training.adam import AdamOptimizer [as 別名]
def extend_with_decoupled_weight_decay(base_optimizer):
  """Factory function returning an optimizer class with decoupled weight decay.

  Returns an optimizer class. An instance of the returned class computes the
  update step of `base_optimizer` and additionally decays the weights.
  E.g., the class returned by
  `extend_with_decoupled_weight_decay(tf.train.AdamOptimizer)` is equivalent to
  `tf.contrib.opt.AdamWOptimizer`.

  The API of the new optimizer class slightly differs from the API of the
  base optimizer:
  - The first argument to the constructor is the weight decay rate.
  - `minimize` and `apply_gradients` accept the optional keyword argument
    `decay_var_list`, which specifies the variables that should be decayed.
    If `None`, all variables that are optimized are decayed.

  Usage example:
  ```python
  # MyAdamW is a new class
  MyAdamW = extend_with_decoupled_weight_decay(tf.train.AdamOptimizer)
  # Create a MyAdamW object
  optimizer = MyAdamW(weight_decay=0.001, learning_rate=0.001)
  sess.run(optimizer.minimize(loss, decay_variables=[var1, var2]))

  Note that this extension decays weights BEFORE applying the update based
  on the gradient, i.e. this extension only has the desired behaviour for
  optimizers which do not depend on the value of'var' in the update step!
  ```

  Args:
    base_optimizer: An optimizer class that inherits from tf.train.Optimizer.

  Returns:
    A new optimizer class that inherits from DecoupledWeightDecayExtension
    and base_optimizer.
  """

  class OptimizerWithDecoupledWeightDecay(DecoupledWeightDecayExtension,
                                          base_optimizer):
    """Base_optimizer with decoupled weight decay.

    This class computes the update step of `base_optimizer` and
    additionally decays the variable with the weight decay being decoupled from
    the optimization steps w.r.t. to the loss function, as described by
    Loshchilov & Hutter (https://arxiv.org/pdf/1711.05101.pdf).
    For SGD variants, this simplifies hyperparameter search since
    it decouples the settings of weight decay and learning rate.
    For adaptive gradient algorithms, it regularizes variables with large
    gradients more than L2 regularization would, which was shown to yield
    better training loss and generalization error in the paper above.
    """

    def __init__(self, weight_decay, *args, **kwargs):
      # super delegation is necessary here
      # pylint: disable=useless-super-delegation
      super(OptimizerWithDecoupledWeightDecay, self).__init__(
          weight_decay, *args, **kwargs)
      # pylint: enable=useless-super-delegation

  return OptimizerWithDecoupledWeightDecay 
開發者ID:yyht,項目名稱:BERT,代碼行數:62,代碼來源:adam_weight_decay_exclude_utils.py

示例4: extend_with_decoupled_weight_decay

# 需要導入模塊: from tensorflow.python.training import adam [as 別名]
# 或者: from tensorflow.python.training.adam import AdamOptimizer [as 別名]
def extend_with_decoupled_weight_decay(base_optimizer):
	"""Factory function returning an optimizer class with decoupled weight decay.
	Returns an optimizer class. An instance of the returned class computes the
	update step of `base_optimizer` and additionally decays the weights.
	E.g., the class returned by
	`extend_with_decoupled_weight_decay(tf.train.AdamOptimizer)` is equivalent to
	`tf.contrib.opt.AdamWOptimizer`.
	The API of the new optimizer class slightly differs from the API of the
	base optimizer:
	- The first argument to the constructor is the weight decay rate.
	- `minimize` and `apply_gradients` accept the optional keyword argument
		`decay_var_list`, which specifies the variables that should be decayed.
		If `None`, all variables that are optimized are decayed.
	Usage example:
	```python
	# MyAdamW is a new class
	MyAdamW = extend_with_decoupled_weight_decay(tf.train.AdamOptimizer)
	# Create a MyAdamW object
	optimizer = MyAdamW(weight_decay=0.001, learning_rate=0.001)
	sess.run(optimizer.minimize(loss, decay_variables=[var1, var2]))
	Note that this extension decays weights BEFORE applying the update based
	on the gradient, i.e. this extension only has the desired behaviour for
	optimizers which do not depend on the value of'var' in the update step!
	```
	Args:
		base_optimizer: An optimizer class that inherits from tf.train.Optimizer.
	Returns:
		A new optimizer class that inherits from DecoupledWeightDecayExtension
		and base_optimizer.
	"""

	class OptimizerWithDecoupledWeightDecay(DecoupledWeightDecayExtension,
																					base_optimizer):
		"""Base_optimizer with decoupled weight decay.
		This class computes the update step of `base_optimizer` and
		additionally decays the variable with the weight decay being decoupled from
		the optimization steps w.r.t. to the loss function, as described by
		Loshchilov & Hutter (https://arxiv.org/pdf/1711.05101.pdf).
		For SGD variants, this simplifies hyperparameter search since
		it decouples the settings of weight decay and learning rate.
		For adaptive gradient algorithms, it regularizes variables with large
		gradients more than L2 regularization would, which was shown to yield
		better training loss and generalization error in the paper above.
		"""

		def __init__(self, weight_decay, *args, **kwargs):
			# super delegation is necessary here
			# pylint: disable=useless-super-delegation
			super(OptimizerWithDecoupledWeightDecay, self).__init__(
					weight_decay, *args, **kwargs)
			# pylint: enable=useless-super-delegation

	return OptimizerWithDecoupledWeightDecay


# @tf_export("contrib.opt.MomentumWOptimizer") 
開發者ID:yyht,項目名稱:BERT,代碼行數:58,代碼來源:adam_weight_decay_utils.py

示例5: extend_with_decoupled_weight_decay

# 需要導入模塊: from tensorflow.python.training import adam [as 別名]
# 或者: from tensorflow.python.training.adam import AdamOptimizer [as 別名]
def extend_with_decoupled_weight_decay(base_optimizer):
  """Factory function returning an optimizer class with decoupled weight decay.
  Returns an optimizer class. An instance of the returned class computes the
  update step of `base_optimizer` and additionally decays the weights.
  E.g., the class returned by
  `extend_with_decoupled_weight_decay(tf.train.AdamOptimizer)` is equivalent to
  `tf.contrib.opt.AdamWOptimizer`.
  The API of the new optimizer class slightly differs from the API of the
  base optimizer:
  - The first argument to the constructor is the weight decay rate.
  - `minimize` and `apply_gradients` accept the optional keyword argument
    `decay_var_list`, which specifies the variables that should be decayed.
    If `None`, all variables that are optimized are decayed.
  Usage example:
  ```python
  # MyAdamW is a new class
  MyAdamW = extend_with_decoupled_weight_decay(tf.train.AdamOptimizer)
  # Create a MyAdamW object
  optimizer = MyAdamW(weight_decay=0.001, learning_rate=0.001)
  sess.run(optimizer.minimize(loss, decay_variables=[var1, var2]))
  Note that this extension decays weights BEFORE applying the update based
  on the gradient, i.e. this extension only has the desired behaviour for
  optimizers which do not depend on the value of'var' in the update step!
  ```
  Args:
    base_optimizer: An optimizer class that inherits from tf.train.Optimizer.
  Returns:
    A new optimizer class that inherits from DecoupledWeightDecayExtension
    and base_optimizer.
  """

  class OptimizerWithDecoupledWeightDecay(DecoupledWeightDecayExtension,
                                          base_optimizer):
    """Base_optimizer with decoupled weight decay.
    This class computes the update step of `base_optimizer` and
    additionally decays the variable with the weight decay being decoupled from
    the optimization steps w.r.t. to the loss function, as described by
    Loshchilov & Hutter (https://arxiv.org/pdf/1711.05101.pdf).
    For SGD variants, this simplifies hyperparameter search since
    it decouples the settings of weight decay and learning rate.
    For adaptive gradient algorithms, it regularizes variables with large
    gradients more than L2 regularization would, which was shown to yield
    better training loss and generalization error in the paper above.
    """

    def __init__(self, weight_decay, *args, **kwargs):
      # super delegation is necessary here
      # pylint: disable=useless-super-delegation
      super(OptimizerWithDecoupledWeightDecay, self).__init__(
          weight_decay, *args, **kwargs)
      # pylint: enable=useless-super-delegation

  return OptimizerWithDecoupledWeightDecay


# @tf_export("contrib.opt.MomentumWOptimizer") 
開發者ID:SeanLee97,項目名稱:QANet_dureader,代碼行數:58,代碼來源:optimizer.py

示例6: extend_with_decoupled_weight_decay

# 需要導入模塊: from tensorflow.python.training import adam [as 別名]
# 或者: from tensorflow.python.training.adam import AdamOptimizer [as 別名]
def extend_with_decoupled_weight_decay(base_optimizer):
  """Factory function returning an optimizer class with decoupled weight decay.
  Returns an optimizer class. An instance of the returned class computes the
  update step of `base_optimizer` and additionally decays the weights.
  E.g., the class returned by
  `extend_with_decoupled_weight_decay(tf.train.AdamOptimizer)` is equivalent to
  `tf.contrib.opt.AdamWOptimizer`.
  The API of the new optimizer class slightly differs from the API of the
  base optimizer:
  - The first argument to the constructor is the weight decay rate.
  - `minimize` and `apply_gradients` accept the optional keyword argument
    `decay_var_list`, which specifies the variables that should be decayed.
    If `None`, all variables that are optimized are decayed.
  Usage example:
  ```python
  # MyAdamW is a new class
  MyAdamW = extend_with_decoupled_weight_decay(tf.train.AdamOptimizer)
  # Create a MyAdamW object
  optimizer = MyAdamW(weight_decay=0.001, learning_rate=0.001)
  sess.run(optimizer.minimize(loss, decay_variables=[var1, var2]))
  Note that this extension decays weights BEFORE applying the update based
  on the gradient, i.e. this extension only has the desired behaviour for
  optimizers which do not depend on the value of'var' in the update step!
  ```
  Args:
    base_optimizer: An optimizer class that inherits from tf.train.Optimizer.
  Returns:
    A new optimizer class that inherits from DecoupledWeightDecayExtension
    and base_optimizer.
  """
  class OptimizerWithDecoupledWeightDecay(DecoupledWeightDecayExtension,
                                          base_optimizer):
    """Base_optimizer with decoupled weight decay.
    This class computes the update step of `base_optimizer` and
    additionally decays the variable with the weight decay being decoupled from
    the optimization steps w.r.t. to the loss function, as described by
    Loshchilov & Hutter (https://arxiv.org/pdf/1711.05101.pdf).
    For SGD variants, this simplifies hyperparameter search since
    it decouples the settings of weight decay and learning rate.
    For adaptive gradient algorithms, it regularizes variables with large
    gradients more than L2 regularization would, which was shown to yield
    better training loss and generalization error in the paper above.
    """

    def __init__(self, weight_decay, *args, **kwargs):
      # super delegation is necessary here
      # pylint: disable=useless-super-delegation
      super(OptimizerWithDecoupledWeightDecay, self).__init__(
          weight_decay, *args, **kwargs)
      # pylint: enable=useless-super-delegation

  return OptimizerWithDecoupledWeightDecay 
開發者ID:freelunchtheorem,項目名稱:Conditional_Density_Estimation,代碼行數:54,代碼來源:adamW.py


注:本文中的tensorflow.python.training.adam.AdamOptimizer方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。