Skip to content

Commit 5fc498e

Browse files
committed
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into refine_grad_clip_api
2 parents 89c591f + b156bbc commit 5fc498e

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

doc/faq/local/index_cn.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,49 @@ decoder_inputs = paddle.layer.fc(
211211
* list 中元素的个数等于网络中输出层的个数;
212212
* list 中每个元素是一个layer的输出结果矩阵,类型是numpy的ndarray;
213213
* 每一个layer输出矩阵的高度,在非序列输入时:等于样本数;序列输入时等于:输入序列中元素的总数;宽度等于配置中layer的size;
214+
215+
6. 如何在训练过程中获得某一个layer的output
216+
-----------------------------------------------
217+
218+
可以在event_handler中,通过 :code:`event.gm.getLayerOutputs("layer_name")` 获得在模型配置中某一层的name :code:`layer_name` 在当前
219+
mini-batch forward的output的值。获得的值类型均为 :code:`numpy.ndarray` ,可以通过这个输出来完成自定义的评估指标计算等功能。例如下面代码:
220+
221+
.. code-block:: python
222+
223+
def score_diff(right_score, left_score):
224+
return np.average(np.abs(right_score - left_score))
225+
226+
def event_handler(event):
227+
if isinstance(event, paddle.event.EndIteration):
228+
if event.batch_id % 25 == 0:
229+
diff = score_diff(
230+
event.gm.getLayerOutputs("right_score")["right_score"][
231+
"value"],
232+
event.gm.getLayerOutputs("left_score")["left_score"][
233+
"value"])
234+
logger.info(("Pass %d Batch %d : Cost %.6f, "
235+
"average absolute diff scores: %.6f") %
236+
(event.pass_id, event.batch_id, event.cost, diff))
237+
238+
注意:此方法不能获取 :code:`paddle.layer.recurrent_group` 里step的内容,但可以获取 :code:`paddle.layer.recurrent_group` 的输出。
239+
240+
7. 如何在训练过程中获得参数的权重和梯度
241+
-----------------------------------------------
242+
243+
在某些情况下,获得当前mini-batch的权重(或称作weights, parameters)有助于在训练时观察具体数值,方便排查以及快速定位问题。
244+
可以通过在 :code:`event_handler` 中打印其值(注意,需要使用 :code:`paddle.event.EndForwardBackward` 保证使用GPU训练时也可以获得),
245+
示例代码如下:
246+
247+
.. code-block:: python
248+
249+
...
250+
parameters = paddle.parameters.create(cost)
251+
...
252+
def event_handler(event):
253+
if isinstance(event, paddle.event.EndForwardBackward):
254+
if event.batch_id % 25 == 0:
255+
for p in parameters.keys():
256+
logger.info("Param %s, Grad %s",
257+
parameters.get(p), parameters.get_grad(p))
258+
259+
注意:“在训练过程中获得某一个layer的output”和“在训练过程中获得参数的权重和梯度”都会造成训练中的数据从C++拷贝到numpy,会对训练性能造成影响。不要在注重性能的训练场景下使用。

python/paddle/v2/fluid/layers/math_op_patch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
from ..framework import Variable, unique_name
16-
from ..registry import OpProtoHolder
16+
from layer_function_generator import OpProtoHolder
1717

1818
__all__ = ['monkey_patch_variable']
1919

python/paddle/v2/fluid/tests/book_memory_optimization/test_memopt_fit_a_line.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
import numpy as np
216
import paddle.v2 as paddle
317
import paddle.v2.fluid as fluid

python/paddle/v2/fluid/tests/book_memory_optimization/test_memopt_image_classification_train.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
from __future__ import print_function
216

317
import sys

0 commit comments

Comments
 (0)