Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

如何打印Variable的内容

Open starsblinking opened this issue 6 years ago • 6 comments

静态图训练完成后,我想打印所有参数的data值。于是调用 var_list = fluid.io.get_program_persistable_vars(default_main_program) 获取需要打印的var列表,那接下来怎么打印var内容呢? 如果用 print(numpy.array(var_list[i])) 那么只能得到类似如下内容:

name: "emb_21" type { type: LOD_TENSOR lod_tensor { tensor { data_type: FP32 dims: 100 dims: 17 } } } persistable: true

这看起来只是 var.to_string 里的内容,而我要的是 100*17个 FP32的值啊。求指导

starsblinking avatar Oct 20 '20 12:10 starsblinking

直接使用print(var_list[i])尝试下。

GaoWei8 avatar Oct 20 '20 12:10 GaoWei8

直接使用print(var_list[i])尝试下。

print(var_list[i])与print(numpy.array(var_list[i]))的结果是一样的

starsblinking avatar Oct 20 '20 12:10 starsblinking

直接使用print(var_list[i])尝试下。

print(var_list[i])与print(numpy.array(var_list[i]))的结果是一样的

试一下Print这个op。 示例:

           import paddle

           paddle.enable_static()
        
           x = paddle.full(shape=[2, 3], fill_value=3, dtype='int64')
           out = paddle.static.Print(x, message="The content of input layer:")

           main_program = paddle.static.default_main_program()
           exe = paddle.static.Executor(place=paddle.CPUPlace())
           res = exe.run(main_program, fetch_list=[out])
           # Variable: fill_constant_1.tmp_0
           #   - message: The content of input layer:
           #   - lod: {}
           #   - place: CPUPlace
           #   - shape: [2, 3]
           #   - layout: NCHW
           #   - dtype: long
           #   - data: [3 3 3 3 3 3]

GaoWei8 avatar Oct 20 '20 12:10 GaoWei8

直接使用print(var_list[i])尝试下。

print(var_list[i])与print(numpy.array(var_list[i]))的结果是一样的

试一下Print这个op。 示例:

           import paddle

           paddle.enable_static()
        
           x = paddle.full(shape=[2, 3], fill_value=3, dtype='int64')
           out = paddle.static.Print(x, message="The content of input layer:")

           main_program = paddle.static.default_main_program()
           exe = paddle.static.Executor(place=paddle.CPUPlace())
           res = exe.run(main_program, fetch_list=[out])
           # Variable: fill_constant_1.tmp_0
           #   - message: The content of input layer:
           #   - lod: {}
           #   - place: CPUPlace
           #   - shape: [2, 3]
           #   - layout: NCHW
           #   - dtype: long
           #   - data: [3 3 3 3 3 3]

您好,这样好像也不行,会报错说网络的输入为空。按照 https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-beta/api/paddle/fluid/executor/Executor_cn.html#run 里面的说法,"执行器会执行Program或CompiledProgram中的所有算子",所以没有输入不行。您给出的例子能跑通,是因为您的main_program里没有额外的输入需要feed吧? 如果为了打印一个与输入无关的网络参数,却要求构造一个batch_size为1的假输入并运行计算整个网络,感觉这种方式不合理。理论上,这种exe.run不应该是为了获取与输入有关的非持久化参数的结果吗?既然是persistable参数,应该有更方便的获取方式吧?

starsblinking avatar Oct 21 '20 03:10 starsblinking

尝试下:fluid.global_scope().find_var("weight_name").get_tensor()

GaoWei8 avatar Oct 21 '20 04:10 GaoWei8

多谢gaowei,问题解决了。 可行的实现方式为: print(numpy.array(fluid.global_scope().find_var(var_list[i].name).get_tensor()))

ps: var_list[i]的类型为paddle.fluid.framework.Parameter,而不是find_var得到的paddle.fluid.core_avx.Variable;其中Variable类型具有get_tensor()成员函数,建议在 https://www.paddlepaddle.org.cn/documentation/docs/zh/2.0-beta/api/paddle/fluid/framework/Variable_cn.html#variable 中补充说明!

starsblinking avatar Oct 21 '20 04:10 starsblinking