From b4d6e7cfc3af432338f83817d19d6122428c3fc1 Mon Sep 17 00:00:00 2001 From: typhoonzero Date: Mon, 22 Jan 2018 15:29:00 +0800 Subject: [PATCH 1/6] add v2 get layer out in faq --- doc/faq/local/index_cn.rst | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/doc/faq/local/index_cn.rst b/doc/faq/local/index_cn.rst index b331d9d36e..5e3f385f8d 100644 --- a/doc/faq/local/index_cn.rst +++ b/doc/faq/local/index_cn.rst @@ -211,3 +211,46 @@ decoder_inputs = paddle.layer.fc( * list 中元素的个数等于网络中输出层的个数; * list 中每个元素是一个layer的输出结果矩阵,类型是numpy的ndarray; * 每一个layer输出矩阵的高度,在非序列输入时:等于样本数;序列输入时等于:输入序列中元素的总数;宽度等于配置中layer的size; + +6. 如何在训练过程中获得某一个layer的output +----------------------------------------------- + +可以在event_handler中,通过 :code:`event.gm.getLayerOutputs("layer_name")` 获得在模型配置中某一层的name :code:`layer_name` 在当前 +mini-batch forward的output的值。获得的值类型均为 :code:`numpy.ndarray` ,可以通过这个输出来完成自定义的评估指标计算等功能。例如下面代码: + +.. code-block:: python + + def score_diff(right_score, left_score): + return np.average(np.abs(right_score - left_score)) + + def event_handler(event): + if isinstance(event, paddle.event.EndIteration): + if event.batch_id % 25 == 0: + diff = score_diff( + event.gm.getLayerOutputs("right_score")["right_score"][ + "value"], + event.gm.getLayerOutputs("left_score")["left_score"][ + "value"]) + logger.info(("Pass %d Batch %d : Cost %.6f, " + "average absolute diff scores: %.6f") % + (event.pass_id, event.batch_id, event.cost, diff)) + + +6. 如何在训练过程中获得参数的权重和梯度 +----------------------------------------------- + +在某些情况下,获得当前mini-batch的权重(或称作weights, parameters)有助于在训练时观察具体数值,方便排查以及快速定位问题。 +可以通过在 :code:`event_handler` 中打印其值(注意,需要使用 :code:`paddle.event.EndForwardBackward` 保证使用GPU训练时也可以获得), +示例代码如下: + +.. code-block:: python + + ... + parameters = paddle.parameters.create(cost) + ... + def event_handler(event): + if isinstance(event, paddle.event.EndForwardBackward): + if event.batch_id % 25 == 0: + for p in parameters.keys(): + logger.info("Param %s, Grad %s", + (parameters.get(p), parameters.get_grad(p)) From 1c4968ee8b89b52e78d46eff6e6f2d6bb60f3136 Mon Sep 17 00:00:00 2001 From: ying Date: Mon, 22 Jan 2018 15:34:13 +0800 Subject: [PATCH 2/6] fix copyright --- .../test_memopt_fit_a_line.py | 14 ++++++++++++++ .../test_memopt_image_classification_train.py | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/python/paddle/v2/fluid/tests/book_memory_optimization/test_memopt_fit_a_line.py b/python/paddle/v2/fluid/tests/book_memory_optimization/test_memopt_fit_a_line.py index 6206fcc4be..cf054bb0fe 100644 --- a/python/paddle/v2/fluid/tests/book_memory_optimization/test_memopt_fit_a_line.py +++ b/python/paddle/v2/fluid/tests/book_memory_optimization/test_memopt_fit_a_line.py @@ -1,3 +1,17 @@ +# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import numpy as np import paddle.v2 as paddle import paddle.v2.fluid as fluid diff --git a/python/paddle/v2/fluid/tests/book_memory_optimization/test_memopt_image_classification_train.py b/python/paddle/v2/fluid/tests/book_memory_optimization/test_memopt_image_classification_train.py index cc37f773c4..42b3cb81ce 100644 --- a/python/paddle/v2/fluid/tests/book_memory_optimization/test_memopt_image_classification_train.py +++ b/python/paddle/v2/fluid/tests/book_memory_optimization/test_memopt_image_classification_train.py @@ -1,3 +1,17 @@ +# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from __future__ import print_function import sys From ccbe7239283be2d7ba8c2eaa234378641ceee75d Mon Sep 17 00:00:00 2001 From: typhoonzero Date: Mon, 22 Jan 2018 15:34:43 +0800 Subject: [PATCH 3/6] update sample code --- doc/faq/local/index_cn.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/doc/faq/local/index_cn.rst b/doc/faq/local/index_cn.rst index 5e3f385f8d..efafaaab35 100644 --- a/doc/faq/local/index_cn.rst +++ b/doc/faq/local/index_cn.rst @@ -236,7 +236,7 @@ mini-batch forward的output的值。获得的值类型均为 :code:`numpy.ndarra (event.pass_id, event.batch_id, event.cost, diff)) -6. 如何在训练过程中获得参数的权重和梯度 +7. 如何在训练过程中获得参数的权重和梯度 ----------------------------------------------- 在某些情况下,获得当前mini-batch的权重(或称作weights, parameters)有助于在训练时观察具体数值,方便排查以及快速定位问题。 @@ -253,4 +253,7 @@ mini-batch forward的output的值。获得的值类型均为 :code:`numpy.ndarra if event.batch_id % 25 == 0: for p in parameters.keys(): logger.info("Param %s, Grad %s", - (parameters.get(p), parameters.get_grad(p)) + parameters.get(p), parameters.get_grad(p)) + +注意:“在训练过程中获得某一个layer的output”和“在训练过程中获得参数的权重和梯度”都会造成训练中的数据从C++拷贝到numpy,会对训练性能造成影响。不要在 + 注重性能的训练场景下使用。 \ No newline at end of file From ded495336e0803dfc63e7ea479d851f677d15ff7 Mon Sep 17 00:00:00 2001 From: typhoonzero Date: Mon, 22 Jan 2018 15:36:51 +0800 Subject: [PATCH 4/6] update style --- doc/faq/local/index_cn.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/faq/local/index_cn.rst b/doc/faq/local/index_cn.rst index efafaaab35..d0cdba90d8 100644 --- a/doc/faq/local/index_cn.rst +++ b/doc/faq/local/index_cn.rst @@ -255,5 +255,4 @@ mini-batch forward的output的值。获得的值类型均为 :code:`numpy.ndarra logger.info("Param %s, Grad %s", parameters.get(p), parameters.get_grad(p)) -注意:“在训练过程中获得某一个layer的output”和“在训练过程中获得参数的权重和梯度”都会造成训练中的数据从C++拷贝到numpy,会对训练性能造成影响。不要在 - 注重性能的训练场景下使用。 \ No newline at end of file +注意:“在训练过程中获得某一个layer的output”和“在训练过程中获得参数的权重和梯度”都会造成训练中的数据从C++拷贝到numpy,会对训练性能造成影响。不要在注重性能的训练场景下使用。 \ No newline at end of file From 160aa64132e5d6a371e01bf55eca668a5601c0c7 Mon Sep 17 00:00:00 2001 From: typhoonzero Date: Mon, 22 Jan 2018 16:31:20 +0800 Subject: [PATCH 5/6] follow comments --- doc/faq/local/index_cn.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/faq/local/index_cn.rst b/doc/faq/local/index_cn.rst index d0cdba90d8..0306b1e5dd 100644 --- a/doc/faq/local/index_cn.rst +++ b/doc/faq/local/index_cn.rst @@ -235,6 +235,7 @@ mini-batch forward的output的值。获得的值类型均为 :code:`numpy.ndarra "average absolute diff scores: %.6f") % (event.pass_id, event.batch_id, event.cost, diff)) +注意:此方法不能获取 :code:`paddle.layer.recurrent_group` 里step的内容,但可以获取 :code:`paddle.layer.recurrent_group` 的输出。 7. 如何在训练过程中获得参数的权重和梯度 ----------------------------------------------- From 38f47e642f277a4eba73221cd480f9e5ceda9588 Mon Sep 17 00:00:00 2001 From: Yang Yu Date: Mon, 22 Jan 2018 17:59:24 +0800 Subject: [PATCH 6/6] Fix CI --- python/paddle/v2/fluid/layers/math_op_patch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/paddle/v2/fluid/layers/math_op_patch.py b/python/paddle/v2/fluid/layers/math_op_patch.py index 11197b70a3..f359e70126 100644 --- a/python/paddle/v2/fluid/layers/math_op_patch.py +++ b/python/paddle/v2/fluid/layers/math_op_patch.py @@ -13,7 +13,7 @@ # limitations under the License. from ..framework import Variable, unique_name -from ..registry import OpProtoHolder +from layer_function_generator import OpProtoHolder __all__ = ['monkey_patch_variable']