@@ -147,15 +147,15 @@ def _addup_repetitive_outputs_(op_descs):
147
147
else :
148
148
if len (renamed_vars [var_name ]) == 1 :
149
149
new_name = var_name + "@RENAME@" + \
150
- str (var_rename_count [var_name ])
150
+ str (var_rename_count [var_name ])
151
151
var_rename_count [var_name ] += 1
152
152
# rename original var_name
153
153
renamed_vars [var_name ][0 ] = new_name
154
154
_rename_arg_ (op_descs , var_name , new_name , 0 , idx )
155
155
_rename_arg_ (pending_sum_ops , var_name , new_name )
156
156
157
157
new_name = var_name + "@RENAME@" + \
158
- str (var_rename_count [var_name ])
158
+ str (var_rename_count [var_name ])
159
159
var_rename_count [var_name ] += 1
160
160
op_desc .rename_output (var_name , new_name )
161
161
renamed_vars [var_name ].append (new_name )
@@ -435,18 +435,65 @@ def _get_stop_gradients_(program):
435
435
def append_backward (loss , parameter_list = None , no_grad_set = None ,
436
436
callbacks = None ):
437
437
"""
438
- Append backward part to main_program
438
+ Append backward part to main_program.
439
439
440
- Args:
441
- loss(Variable): The variable generated by cost function.
442
- parameter_list(list[string]): Parameters that need to be updated by
443
- optimizer. If None, it means all parameters need to be updated.
444
- no_grad_set(set): Variables that have no gradients in Block 0.
445
- All variables with `step_gradient=True` from all blocks will be
446
- automatically added.
440
+ A complete neural network training is made up of forward and backward
441
+ propagation. However, when we configure a network, we only need to
442
+ specify its forwrd part. The backward part is generated automatically
443
+ according to the forward part by this function.
447
444
448
- Return:
449
- (list[(Variable,Variable)]): list of (parameter, gradient) pair.
445
+ In most cases, users do not need to invoke this function manually. It
446
+ will be automatically invoked by the optimizer's `minimize` function.
447
+
448
+ Args:
449
+ loss(Variable): The loss variable of the network.
450
+ parameter_list(list[string]|None): Names of parameters that need
451
+ to be updated by optimizers.
452
+ If it is None, all parameters
453
+ will be updated.
454
+ Default: None
455
+ no_grad_set(set|None): Variables in the Block 0 whose gradients
456
+ should be ignored. All variables with
457
+ `step_gradient=True` from all blocks will
458
+ be automatically added into this set.
459
+ Default: None
460
+ callbacks(list[callable object]|None): The callbacks are used for
461
+ doing some custom jobs during
462
+ backward part building. All
463
+ callable objects in it will
464
+ be invoked once each time a
465
+ new gradient operator is added
466
+ into the program. The callable
467
+ object must has two input
468
+ parameters: 'block' and 'context'.
469
+ The 'block' is the block which
470
+ the new gradient operator will
471
+ be added to. The 'context' is a
472
+ map, whose keys are gradient
473
+ variable names and values are
474
+ corresponding original variables.
475
+ In addition to this, the 'context'
476
+ has another special key-value pair:
477
+ the key is string '__current_op_desc__'
478
+ and the value is the op_desc of the
479
+ gradient operator who has just
480
+ triggered the callable object.
481
+
482
+ Returns:
483
+ list[(Variable,Variable)]: Pairs of parameter and its
484
+ corresponding gradients. The key is the parameter and the
485
+ value is gradient variable.
486
+
487
+ Raises:
488
+ AssertionError: If `loss` is not an instance of Variable.
489
+
490
+ Examples:
491
+ .. code-block:: python
492
+
493
+ # network configuration code
494
+ # ...
495
+ avg_loss = fluid.layers.mean(loss)
496
+ param_grad_list = fluid.backward.append_backward(loss=avg_loss)
450
497
"""
451
498
assert isinstance (loss , framework .Variable )
452
499
0 commit comments