Paddle 说到做不到之模型参数不初始化-exe.run(fluid.default_start_program())

xuo3flqw  于 2022-04-21  发布在  Java
关注(0)|答案(4)|浏览(182)

为什么**fluid.default_start_program()不能初始化全部参数?说好的exe.run(fluid.default_start_program())用来初始化参数,但是会经常性的报错XXX is not initialized。隔壁的tf.global_variables_initializer()**可是说到做到了的。

xpcnnkqh

xpcnnkqh1#

你好,可不可以提供一下可以问题的代码

6l7fqoea

6l7fqoea2#

你好,可不可以提供一下可以问题的代码

import numpy as np
import paddle.fluid as fluid
import gym

class Critic(object):
def init(self, exe, gamma, state_dim, lr):

# self.exe = fluid.Executor(fluid.CPUPlace())

self.exe = exe
self.state_dim = state_dim
self.gamma = gamma
self.lr = lr
self._built_net()

def _built_net(self):
    self.critic_program = fluid.Program()
    with fluid.program_guard(self.critic_program):
        state_data = fluid.data(name='c_state', shape=[None, self.state_dim], dtype='float32')
        r = fluid.data(name='c_reward', shape=[None, 1], dtype='float32')
        v_ = fluid.data(name='c_next_reward', shape=[None, 1], dtype='float32')
        # 创建网络
        h1 = fluid.layers.fc(input=state_data, size=24, act='relu')
        self.v = fluid.layers.fc(input=h1, size=1)
        self.td_error = r + self.gamma * v_ - self.v  # td_error = reward + gamma * next_value - value
        self.critic_program_test = self.critic_program.clone(for_test=True)
        # cost = fluid.layers.reduce_mean(fluid.layers.square_error_cost(r+self.gamma*v_, self.v), dim=0)
        cost = fluid.layers.reduce_mean(fluid.layers.square(self.td_error))
        opt = fluid.optimizer.AdamOptimizer(learning_rate=self.lr).minimize(cost)

def train(self, state, reward, next_state):
    v_ = self.exe.run(self.critic_program_test,
                      feed={'c_state': next_state},
                      fetch_list=[self.v])[0]
    td_error = self.exe.run(self.critic_program,
                            feed={'c_state': state,
                                  'c_next_reward': v_,
                                  'c_reward': reward},
                            fetch_list=[self.td_error])[0]
    return td_error
def run_episode(env, actor=None, critic=None, render=True):
s = env.reset()

while True:
    # act = actor.choose_action(dat(s))
    s_, r, done, info = env.step(0)
    if render:
        env.render()
    if done:
        r = -10
    td_error = critic.train(dat(s), dat(r), dat(s_))
    s = s_
    if done:
        break
def dat(data):
return np.expand_dims(data, axis=0).astype(np.float32)

def main():
exe = fluid.Executor(fluid.CPUPlace())
env = gym.make('CartPole-v1')

# actor = Actor(exe, 4, 2, lr=0.001)

critic = Critic(exe, gamma=0.9, state_dim=4, lr=0.005)
exe.run(fluid.default_startup_program())
run_episode(env, critic=critic, render=True)

env.close()
if name == 'main':
main()
ac1kyiln

ac1kyiln3#

代码改一下,就不会报错。但是为不是说fluid.default_startup_program会初始化所有参数吗?tf。gloable_variable_initiailzer()不会出现这样的问题

yquaqz18

yquaqz184#

我发现paddle静态图下会非常容易出现这种错误,而且有的时候不好排查错误,希望官方能够修正这个错误。

相关问题