Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何打印Variable的内容 #28139

Closed
starsblinking opened this issue Oct 20, 2020 · 6 comments
Closed

如何打印Variable的内容 #28139

starsblinking opened this issue Oct 20, 2020 · 6 comments

Comments

@starsblinking
Copy link

starsblinking commented Oct 20, 2020

静态图训练完成后,我想打印所有参数的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的值啊。求指导

@GaoWei8
Copy link
Contributor

GaoWei8 commented Oct 20, 2020

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

@starsblinking
Copy link
Author

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

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

@GaoWei8
Copy link
Contributor

GaoWei8 commented Oct 20, 2020

直接使用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]

@starsblinking
Copy link
Author

直接使用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参数,应该有更方便的获取方式吧?

@GaoWei8
Copy link
Contributor

GaoWei8 commented Oct 21, 2020

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

@starsblinking
Copy link
Author

多谢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 中补充说明!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants