class_docstr: ## Trainer
class_docstr_tok: ["#","#","Trainer"]
docstr: #### $\epsilon$-greedy Sampling
  When sampling actions we use a $\epsilon$-greedy strategy, where we
  take a greedy action with probabiliy $1 - \epsilon$ and
  take a random action with probability $\epsilon$.
  We refer to $\epsilon$ as `exploration_coefficient`.
docstr_tok: ["#","#","#","#","$","\\epsilon","$","-greedy","Sampling","When","sampling","actions","we","use","a","$","\\epsilon","$","-greedy","strategy",",","where","we","take","a","greedy","action","with","probabiliy","$","1","-","\\epsilon","$","and","take","a","random","action","with","probability","$","\\epsilon","$",".","We","refer","to","$","\\epsilon","$","as","`","exploration_coefficient","`","."]
code: def _sample_action(
      self, q_value: torch.Tensor, exploration_coefficient: float
  ):
      """
      #### $\epsilon$-greedy Sampling
      When sampling actions we use a $\epsilon$-greedy strategy, where we
      take a greedy action with probabiliy $1 - \epsilon$ and
      take a random action with probability $\epsilon$.
      We refer to $\epsilon$ as `exploration_coefficient`.
      """
  
      # Sampling doesn't need gradients
      with torch.no_grad():
          # Sample the action with highest Q-value. This is the greedy action.
          greedy_action = torch.argmax(q_value, dim=-1)
          # Uniformly sample and action
          random_action = torch.randint(
              q_value.shape[-1],
              greedy_action.shape,
              device=q_value.device,
          )
          # Whether to chose greedy action or the random action
          is_choose_rand = (
              torch.rand(greedy_action.shape, device=q_value.device)
              < exploration_coefficient
          )
          # Pick the action based on `is_choose_rand`
          return (
              torch.where(is_choose_rand, random_action, greedy_action)
              .cpu()
              .numpy()
          )
  
file_path: annotated_deep_learning_paper_implementations/labml_nn/rl/dqn/experiment.py
imports: import numpy
  import torch
  from labml import tracker, experiment, logger, monit
  from labml.internal.configs.dynamic_hyperparam import FloatDynamicHyperParam
  from labml_helpers.schedule import Piecewise
  from labml_nn.rl.dqn import QFuncLoss
  from labml_nn.rl.dqn.model import Model
  from labml_nn.rl.dqn.replay_buffer import ReplayBuffer
  from labml_nn.rl.game import Worker
label_desc: Use image node_id 2 for calling the Trainer obj's underlying member method code with example usage: obj._sample_action(q_value, exploration_coefficient) and returns: torch