Skip to content

Commit 7c59ac4

Browse files
committed
Refine doc and use 'raise' instead of assert
1 parent de2d729 commit 7c59ac4

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

doc/v2/api/fluid/optimizer.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,10 @@ DecayedAdagrad
4747
:members:
4848
:noindex:
4949

50+
Adadelta
51+
--------------
52+
53+
.. autoclass:: paddle.fluid.optimizer.AdadeltaOptimizer
54+
:members:
55+
:noindex:
56+

python/paddle/fluid/optimizer.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,31 +583,61 @@ def _append_optimize_op(self, block, param_and_grad):
583583

584584

585585
class AdadeltaOptimizer(Optimizer):
586-
"""Simple Adadelta optimizer with average squared grad state and
586+
"""
587+
**Adadelta Optimizer**
588+
Simple Adadelta optimizer with average squared grad state and
587589
average squared update state.
590+
The details of adadelta please refer to this
591+
`ADADELTA: AN ADAPTIVE LEARNING RATE METHOD
592+
<http://www.matthewzeiler.com/pubs/googleTR2012/googleTR2012.pdf>`_.
593+
594+
.. math::
595+
596+
E(g_t^2) &= \\rho * E(g_{t-1}^2) + (1-\\rho) * g^2 \\\\
597+
learning\\_rate &= sqrt( ( E(dx_{t-1}^2) + \\epsilon ) / ( \\
598+
E(g_t^2) + \\epsilon ) ) \\\\
599+
E(dx_t^2) &= \\rho * E(dx_{t-1}^2) + (1-\\rho) * (-g*learning\\_rate)^2
600+
601+
Args:
602+
learning_rate(float): global leraning rate
603+
rho(float): rho in equation
604+
epsilon(float): epsilon in equation
605+
606+
Examples:
607+
.. code-block:: python
608+
609+
optimizer = fluid.optimizer.Adadelta(
610+
learning_rate=0.0003, epsilon=1.0e-6, rho=0.95)
611+
_, params_grads = optimizer.minimize(cost)
588612
"""
613+
589614
_avg_squared_grad_acc_str = "_avg_squared_grad"
590615
_avg_squared_update_acc_str = "_avg_squared_update"
591616

592617
def __init__(self, learning_rate, epsilon=1.0e-6, rho=0.95, **kwargs):
593-
assert learning_rate is not None
594-
assert epsilon is not None
595-
assert rho is not None
618+
if learning_rate is None:
619+
raise ValueError("learning_rate is not set.")
620+
if epsilon is None:
621+
raise ValueError("epsilon is not set.")
622+
if rho is None:
623+
raise ValueError("rho is not set.")
596624
super(AdadeltaOptimizer, self).__init__(
597625
learning_rate=learning_rate, **kwargs)
598626
self.type = "adadelta"
599627
self._epsilon = epsilon
600628
self._rho = rho
601629

602630
def _create_accumulators(self, block, parameters):
603-
assert isinstance(block, framework.Block)
631+
if not isinstance(block, framework.Block):
632+
raise TypeError("block is not instance of framework.Block.")
604633

605634
for p in parameters:
606635
self._add_accumulator(self._avg_squared_grad_acc_str, p)
607636
self._add_accumulator(self._avg_squared_update_acc_str, p)
608637

609638
def _append_optimize_op(self, block, param_and_grad):
610-
assert isinstance(block, framework.Block)
639+
if not isinstance(block, framework.Block):
640+
raise TypeError("block is not instance of framework.Block.")
611641

612642
avg_squared_grad_acc = self._get_accumulator(
613643
self._avg_squared_grad_acc_str, param_and_grad[0])

0 commit comments

Comments
 (0)