From 252c84dbe1e4659e5a3b6f10fe80a893e88ecb5c Mon Sep 17 00:00:00 2001 From: xuwei06 Date: Thu, 1 Jun 2017 16:20:38 -0700 Subject: [PATCH] Correctly handle print_layer in V2 API print_layer is used to print the values of a layer when evaluating the model for debugging purpose. No layer depends on print_layer. It should be added to the topology if it is used to print some layer already in the topology. --- python/paddle/trainer_config_helpers/layers.py | 10 +++++++++- python/paddle/v2/layer.py | 14 ++++++++++++++ python/paddle/v2/tests/test_layer.py | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/python/paddle/trainer_config_helpers/layers.py b/python/paddle/trainer_config_helpers/layers.py index 81cce31fecc..5762e1d159e 100755 --- a/python/paddle/trainer_config_helpers/layers.py +++ b/python/paddle/trainer_config_helpers/layers.py @@ -111,6 +111,7 @@ __all__ = [ 'block_expand_layer', 'maxout_layer', 'out_prod_layer', + 'printer_layer', 'print_layer', 'priorbox_layer', 'cross_channel_norm_layer', @@ -969,7 +970,7 @@ def fc_layer(input, @wrap_name_default("print") -def print_layer(input, name=None): +def printer_layer(input, name=None): """ Print the output value of input layers. This layer is useful for debugging. @@ -991,6 +992,13 @@ def print_layer(input, name=None): inputs=[l.name for l in input], ) # this layer don't return anything, can not be input of other layer. +# Keep print_layer for compatibility with V1 API. +# 'print_layer' does not work for V2 API because it will be changed to +# 'print' for V2 API. But 'print' is a reserved key word in python. + + +print_layer = printer_layer + @wrap_name_default("priorbox") def priorbox_layer(input, diff --git a/python/paddle/v2/layer.py b/python/paddle/v2/layer.py index da2abdd2d1e..815635f5dd4 100644 --- a/python/paddle/v2/layer.py +++ b/python/paddle/v2/layer.py @@ -149,6 +149,20 @@ def __get_used_layers__(output_layers, extra_layers=None): for layer in output_layers: dfs_travel(layer.full_name) + # print layer needs to be specially handled because no other + # layer depends on it. It is used to print the result of some + # layers when running the model for debug purpose. So we explicitly + # add a print layer to the topolty if its input is in the toplogy. + for layer in cp.g_config.model_config.layers: + if layer.type == 'print': + used = True + for inp in layer.inputs: + if inp.input_layer_name not in layer_names: + used = False + break + if used: + layer_names.add(layer.name) + return layer_names diff --git a/python/paddle/v2/tests/test_layer.py b/python/paddle/v2/tests/test_layer.py index 2d25b1a9dca..f2097e195f4 100644 --- a/python/paddle/v2/tests/test_layer.py +++ b/python/paddle/v2/tests/test_layer.py @@ -164,6 +164,7 @@ class OtherLayerTest(unittest.TestCase): maxid = layer.max_id(input=inference) sampling_id = layer.sampling_id(input=inference) eos = layer.eos(input=maxid, eos_id=5) + layer.printer(maxid) print layer.parse_network([maxid, sampling_id, eos]) def test_slicing_joining_layer(self): -- GitLab