CommencerCommencer gratuitement

Sélection d'actions dans REINFORCE

Écrivez la fonction REINFORCE select_action, qui sera utilisée par votre agent REINFORCE pour sélectionner une action à chaque étape.

Dans DQN, le passage en avant du réseau renvoyait des valeurs Q ; dans REINFORCE, il renvoie des probabilités d'action, à partir desquelles une action peut être directement échantillonnée.

Un réseau de politiques et un État ont été chargés dans votre environnement.

torch.distributions.Categorical a été importé en tant que Categorical.

Cet exercice fait partie du cours

Apprentissage par renforcement profond en Python

Afficher le cours

Instructions

  • Obtenez les probabilités d'action sous forme de tenseur torch.
  • Obtenez la distribution des torches correspondant aux probabilités d'action.
  • Essayez une action de la distribution.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

def select_action(policy_network, state):
  # Obtain the action probabilities
  action_probs = ____
  print('Action probabilities:', action_probs)
  # Instantiate the action distribution
  action_dist = Categorical(____)
  # Sample an action from the distribution
  action = ____
  log_prob = action_dist.log_prob(action)
  return action.item(), log_prob.reshape(1)

state = torch.rand(8)
action, log_prob = select_action(policy_network, state)
print('Sampled action index:', action)
print(f'Log probability of sampled action: {log_prob.item():.2f}')
Modifier et exécuter le code