huggingface/deep-rl-class

Unit 8 Hands On : 'if "episode" in item.keys(): AttributeError: 'str' object has no attribute 'keys''

Pregonas opened this issue · 2 comments

Running the ppo.py final file there is a problem with the "info" param from the gym.step( ) return:

if "episode" in item.keys():
AttributeError: 'str' object has no attribute 'keys'

The original code:

  for item in info:
      if "episode" in item.keys():
          print(f"global_step={global_step}, episodic_return={item['episode']['r']}")
          writer.add_scalar("charts/episodic_return", item["episode"]["r"], global_step)
          writer.add_scalar("charts/episodic_length", item["episode"]["l"], global_step)
          break

In this line:

for item in info:

The "info" param is a dict, and the item are the keys in that dict. For that reason, it gets a string for the "item" variable and a string has not the *.keys( ) argument.

I have changed the code as follows and it works:

for item in info:
                if "episode" == item:
                    for i in info["episode"]:
                      if i != None:
                        break
                    print(f"global_step={global_step}, episodic_return={i['r']}")
                    writer.add_scalar("charts/episodic_return", i["r"], global_step)
                    writer.add_scalar("charts/episodic_length", i["l"], global_step)
                    break

Hey there 👋 what version of gym do you use?

Closing the issue 🤗