Skip to content

Commit 9de779f

Browse files
committed
update switch class
1 parent 3a25cee commit 9de779f

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

python/paddle/fluid/layers/control_flow.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,16 +1156,14 @@ def complete(self):
11561156

11571157
class Switch(object):
11581158
"""
1159-
**Switch Class**
1160-
1161-
Many programming languages provide `switch` as a generalization of `if-elif-else`.
1162-
Switch class works just like a `if-elif-else`.
1159+
Switch class works just like a `if-elif-else`. Can be used in learning rate scheduler
1160+
to modify learning rate
11631161
11641162
The Semantics:
11651163
11661164
1. A `switch` control-flow checks cases one-by-one.
11671165
1168-
2. The condition of each case is a boolean value, which is a scalar.
1166+
2. The condition of each case is a boolean value, which is a scalar Variable.
11691167
11701168
3. It runs the first matched case, or the default case if there is one.
11711169
@@ -1174,9 +1172,22 @@ class Switch(object):
11741172
Examples:
11751173
.. code-block:: python
11761174
1177-
with fluid.control_flow.Switch() as switch:
1175+
lr = fluid.layers.tensor.create_global_var(
1176+
shape=[1],
1177+
value=0.0,
1178+
dtype='float32',
1179+
persistable=True,
1180+
name="learning_rate")
1181+
one_var = tensor.fill_constant(
1182+
shape=[1], dtype='float32', value=1.0)
1183+
two_var = tensor.fill_constant(
1184+
shape=[1], dtype='float32', value=2.0)
1185+
1186+
with fluid.layers.control_flow.Switch() as switch:
11781187
with switch.case(global_step == zero_var):
1179-
fluid.tensor.assign(input=one_var, output=div_res)
1188+
fluid.layers.tensor.assign(input=one_var, output=lr)
1189+
with switch.default():
1190+
fluid.layers.tensor.assign(input=two_var, output=lr)
11801191
11811192
"""
11821193

0 commit comments

Comments
 (0)