无法将非矩形Python序列转换为Tensor错误,当按照Keras网站上的示例操作时

ha5z0ras  于 2023-01-17  发布在  Python
关注(0)|答案(1)|浏览(190)

我将以https://keras.io/examples/rl/ddpg_pendulum/为例
我收到一个错误
我使用的是tensorflow版本:2.10.0和健身房0.26.2

tf_prev_state = tf.expand_dims(tf.convert_to_tensor(prev_state), 0)
Exception has occurred: ValueError
Can't convert non-rectangular Python sequence to Tensor.
  File "C:\Users\vlad.nanu\Documents\GitHub\ml-hub\pendulum.py", line 236, in <module>
    tf_prev_state = tf.expand_dims(tf.convert_to_tensor(prev_state), 0)
ValueError: Can't convert non-rectangular Python sequence to Tensor.
g9icjywg

g9icjywg1#

阅读最新gym版本(0.26.x)的release notes,您将发现2个影响pendulum代码的突破性更改,分别涉及env.Stepenv.Reset
可以更改以下三行(请参见#changed):

for ep in range(total_episodes):

    prev_state, _ = env.reset() # changed
    episodic_reward = 0

    while True:
        # Uncomment this to see the Actor in action
        # But not in a python notebook.
        # env.render()

        tf_prev_state = tf.expand_dims(tf.convert_to_tensor(prev_state), 0)

        action = policy(tf_prev_state, ou_noise)
        # Recieve state and reward from environment.
        state, reward, terminated, truncated, info = env.step(action) # changed

        buffer.record((prev_state, action, reward, state))
        episodic_reward += reward

        buffer.learn()
        update_target(target_actor.variables, actor_model.variables, tau)
        update_target(target_critic.variables, critic_model.variables, tau)

        # End this episode when `done` is True
        if terminated or truncated: # changed
            break

        prev_state = state

    ep_reward_list.append(episodic_reward)

    # Mean of last 40 episodes
    avg_reward = np.mean(ep_reward_list[-40:])
    print("Episode * {} * Avg Reward is ==> {}".format(ep, avg_reward))
    avg_reward_list.append(avg_reward)

相关问题