AI4Finance-Foundation / ElegantRL

Massively Parallel Deep Reinforcement Learning. 🔥
https://ai4finance.org
Other
3.75k stars 850 forks source link

AttributeError: 'QNet' object has no attribute 'explore_action' in helloworld_DQN_single_file.py #365

Closed HiFei4869 closed 3 months ago

HiFei4869 commented 3 months ago

When I run the helloworld/helloworld_DQN_single_file.py, it throws the following error:

  File "/mnt/.../helloworld/helloworld_DQN_single_file.py", line 178, in explore_env
  get_action = self.act.explore_action
File "/mnt/.../miniconda3/envs/sd_py10/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1709, in __getattr__
  raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
AttributeError: 'QNet' object has no attribute 'explore_action'

When I check helloworld/helloworld_DQN_single_file.py, it just doesn't have explore_action

class QNet(nn.Module):  # `nn.Module` is a PyTorch module for neural network
def __init__(self, dims: [int], state_dim: int, action_dim: int):
    super().__init__()
    self.net = build_mlp(dims=[state_dim, *dims, action_dim])
    self.explore_rate = None
    self.action_dim = action_dim

def forward(self, state: Tensor) -> Tensor:
    return self.net(state)  # Q values for multiple actions

def get_action(self, state: Tensor) -> Tensor:  # return the index [int] of discrete action for exploration
    if self.explore_rate < torch.rand(1):
        action = self.net(state).argmax(dim=1, keepdim=True)
    else:
        action = torch.randint(self.action_dim, size=(state.shape[0], 1))
    return action

It may be a bug. It should be get_action

zhumingpassional commented 3 months ago

thanks for reporting it. we will check it.

cotZhong commented 3 months ago

Oh I meet the same error

zhumingpassional commented 3 months ago

@HiFei4869 could you pls submit a PR for it?

cotZhong commented 3 months ago

@HiFei4869 could you pls submit a PR for it?

Hello. I find the problem in both DQN hello world and PPO hello world and I try to submit the PR for both (to master branch)

HiFei4869 commented 3 months ago

@HiFei4869 could you pls submit a PR for it?

Already fix it in a PR 2 days ago