Hi, previously you did a wonderful job of implementing a q actor critic algorithm for…
- User
Hi, previously you did a wonderful job of implementing a q actor critic algorithm for me. Here is the implementation : class QActorCritic(nn.Module): def init(self, num_inputs, num_outputs): super(QActorCritic, self).init() self.policy_net = nn.Sequential( nn.Linear(num_inputs, 64), nn.ReLU(), nn.Linear(64, 64), nn.ReLU(), nn.Linear(64, num_outputs), nn.Softmax(dim=1) )
codeself.q_net = nn.Sequential( nn.Linear(num_inputs, 64), nn.ReLU(), nn.Linear(64, 64), nn.ReLU(), nn.Linear(64, num_outputs) ) def forward(self, x): raise NotImplementedError def get_action_probs(self, x): x = torch.from_numpy(x).float().unsqueeze(0).to(device) action_probs = self.policy_net(x) return action_probs def get_q_values(self, x): x = torch.from_numpy(x).float().unsqueeze(0).to(device) q_values = self.q_net(x) return q_values and def train(env, device, model, lr=1e-3, num_episodes=500, gamma=0.99): policy_optimizer = optim.Adam(model.policy_net.parameters(), lr=lr) q_optimizer = optim.Adam(model.q_net.parameters(), lr=lr) total_rewards = [] for episode in range(num_episodes): state = env.reset() episode_reward = 0 while True: action_probs = model.get_action_probs(state) action_distr = Categorical(action_probs) action = action_distr.sample().item() next_state, reward, done, _ = env.step(action) next_action_probs = model.get_action_probs(next_state) next_action_distr = Categorical(next_action_probs) next_action = next_action_distr.sample().item() with torch.no_grad(): target = reward + gamma * model.get_q_values(next_state)[0, next_action] policy_loss = -action_distr.log_prob(torch.tensor([action]).to(device)) * (target - model.get_q_values(state)[0, action]) q_loss = (target - model.get_q_values(state)[0, action]).pow(2).to(device) policy_optimizer.zero_grad() policy_loss.backward() policy_optimizer.step() q_optimizer.zero_grad() q_loss.backward() q_optimizer.step() state = next_state episode_reward += reward if done: break total_rewards.append(episode_reward) if episode % 100 == 0: print(f"Episode: {episode}, reward: {episode_reward}") return total_rewards. This here is the calling function : # Bipedal Walker environment - Assistant
The problem is happening because the environment
BipedalWalker-v3has a continuous action space, not a discrete one. In the provided code, the action space is treated as discrete and actions are sampled using theCategoricaldistribution. The easiest way to fix the above code is to replace the action space sampling method.