【fluid】在两个program之间copy参数
在两个program之间copy参数不起作用,请帮忙看下
#!/usr/bin/env python
# coding=utf8
# File: copy_diff_program.py
import paddle.fluid as fluid
from rllab import lab
from rllab.utils import logger
import numpy as np
x = lab.data(name='x', shape=[5], dtype='float32')
policy_program = fluid.default_main_program().clone()
with fluid.program_guard(policy_program):
with lab.variable_scope('policy'):
y1 = lab.FullyConnected('fc', x, 10)
vars = fluid.default_main_program().list_vars()
policy_vars = filter(lambda x: 'GRAD' not in x.name and 'policy' in x.name, vars)
for each in policy_vars:
logger.info(each.name)
value_program = fluid.default_main_program().clone()
with fluid.program_guard(value_program):
with lab.variable_scope('value'):
y2 = lab.FullyConnected('fc', x, 10)
vars = fluid.default_main_program().list_vars()
value_vars = filter(lambda x: 'GRAD' not in x.name and 'value' in x.name, vars)
policy_vars.sort(key=lambda x:x.name)
value_vars.sort(key=lambda x:x.name)
sync_program = fluid.default_main_program().clone()
with fluid.program_guard(sync_program):
sync_ops = []
for i, var in enumerate(policy_vars):
logger.info("[assign] policy:{} value:{}".format(policy_vars[i].name, value_vars[i].name))
sync_op = lab.assign(policy_vars[i], value_vars[i])
sync_ops.append(sync_op)
sync_program = sync_program.prune(sync_ops)
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
w0 = fluid.global_scope().find_var("policy/fc_W").get_tensor()
w1 = fluid.global_scope().find_var("value/fc_W").get_tensor()
print np.sum(np.array(w0))
print np.sum(np.array(w1))
exe.run(sync_program)
print '-------------'
w0 = fluid.global_scope().find_var("policy/fc_W").get_tensor()
w1 = fluid.global_scope().find_var("value/fc_W").get_tensor()
print np.sum(np.array(w0))
print np.sum(np.array(w1))
输出信息如下:
-2.7668757
1.8978335
I0530 11:36:10.023646 294248 executor.cc:114] Create Variable feed global, which pointer is 0x5abd9e50
I0530 11:36:10.023655 294248 scope.cc:56] Create variable value/fc_W
I0530 11:36:10.023659 294248 executor.cc:119] Create Variable value/fc_W locally, which pointer is 0x5abdcbf0
I0530 11:36:10.023664 294248 executor.cc:114] Create Variable fetch global, which pointer is 0x5abd9df0
I0530 11:36:10.023669 294248 scope.cc:56] Create variable value/fc_b
I0530 11:36:10.023672 294248 executor.cc:119] Create Variable value/fc_b locally, which pointer is 0x5abd9cd0
I0530 11:36:10.023681 294248 executor.cc:334] CPUPlace Op(assign), inputs:{X[policy/fc_W[5, 10]({})]}, outputs:{Out[value/fc_W[0]({})]}.
I0530 11:36:10.023689 294248 tensor_util.cu:24] TensorCopy 5, 10 from CPUPlace to CPUPlace
I0530 11:36:10.023697 294248 tensor_util.cu:40] TensorCopy Done
I0530 11:36:10.023705 294248 executor.cc:334] CPUPlace Op(assign), inputs:{X[policy/fc_b[10]({})]}, outputs:{Out[value/fc_b[0]({})]}.
I0530 11:36:10.023710 294248 tensor_util.cu:24] TensorCopy 10 from CPUPlace to CPUPlace
I0530 11:36:10.023715 294248 tensor_util.cu:40] TensorCopy Done
I0530 11:36:10.023720 294248 scope.cc:40] Destroy variable value/fc_b
I0530 11:36:10.023726 294248 scope.cc:40] Destroy variable value/fc_W
-------------
-2.7668757
1.8978335
https://github.com/PaddlePaddle/Paddle/blob/71fa3ca9c4b1a7b5be569e32f8c2b5f659542f16/paddle/fluid/framework/tensor_util.cc#L24 我确认代码是执行了copy的函数了,但是并没有生效
@TomorrowIsAnOtherDay 可否把sync_program打印出来看下?另外lab.assign() 为什么可以接受fluid的var作为输入?
sync_program..
blocks {
idx: 0
parent_idx: -1
vars {
name: "value/fc_W"
type {
type: LOD_TENSOR
}
}
vars {
name: "value/fc_b"
type {
type: LOD_TENSOR
}
}
ops {
inputs {
parameter: "X"
arguments: "policy/fc_W"
}
outputs {
parameter: "Out"
arguments: "value/fc_W"
}
type: "assign"
is_target: true
}
ops {
inputs {
parameter: "X"
arguments: "policy/fc_b"
}
outputs {
parameter: "Out"
arguments: "value/fc_b"
}
type: "assign"
is_target: true
}
}
第二个问题:这个应该是可以的哈,我之前在单个program里面就是这么写的 https://github.com/PaddlePaddle/models/blob/de9e9e2057161adc8a38ab6c354e34b2dc026b05/fluid/DeepQNetwork/agent.py#L115 而且验证过是可以copy的,var应该是转换成tensor了
need to have create_parameter() in the sync_program
Solved!! 💯