class_docstr: Q-learning agent.
  
  Including:
  - DQN https://storage.googleapis.com/deepmind-media/dqn/DQNNaturePaper.pdf
  - Prioritized Experience Replay https://arxiv.org/pdf/1511.05952.pdf
  - Dueling Network https://arxiv.org/pdf/1511.06581.pdf
  - Double Q Learning https://arxiv.org/pdf/1509.06461.pdf
class_docstr_tok: ["Q-learning","agent",".","Including",":","-","DQN","https",":","\/\/storage.googleapis.com\/deepmind-media\/dqn\/DQNNaturePaper.pdf","-","Prioritized","Experience","Replay","https",":","\/\/arxiv.org\/pdf\/1511.05952.pdf","-","Dueling","Network","https",":","\/\/arxiv.org\/pdf\/1511.06581.pdf","-","Double","Q","Learning","https",":","\/\/arxiv.org\/pdf\/1509.06461.pdf"]
docstr: Constructor for Q learning algorithm.
  
  Widely known as DQN. Use either predefined neural network structure
  (see models.py) or customized network (see customized_models.py).
  
  Args:
      config_filename: configure file specifying training details.
      o_space: observation space, gym.spaces.tuple_space.Tuple is not
          supported.
      a_space: action space, limits to gym.spaces.discrete.Discrete.
docstr_tok: ["Constructor","for","Q","learning","algorithm",".","Widely","known","as","DQN",".","Use","either","predefined","neural","network","structure","(","see","models.py",")","or","customized","network","(","see","customized_models.py",")",".","Args",":","config_filename",":","configure","file","specifying","training","details",".","o_space",":","observation","space",",","gym.spaces.tuple_space.Tuple","is","not","supported",".","a_space",":","action","space",",","limits","to","gym.spaces.discrete.Discrete","."]
code: def __init__(self, config_filename, o_space, a_space):
      """Constructor for Q learning algorithm.
  
      Widely known as DQN. Use either predefined neural network structure
      (see models.py) or customized network (see customized_models.py).
  
      Args:
          config_filename: configure file specifying training details.
          o_space: observation space, gym.spaces.tuple_space.Tuple is not
              supported.
          a_space: action space, limits to gym.spaces.discrete.Discrete.
      """
      super(QLearning, self).__init__(o_space, a_space)
  
      self._parameters = QLearningParameters(config_filename)
  
      # Create preprocessor.
      if self._parameters.preprocessing:
          try:
              preproc = self._import_method(
                  self._parameters.preprocessing
              )
              self._preprocessor = preproc(
                  self._shape_of_inputs,
                  *ast.literal_eval(
                      self._parameters.preprocessing_args
                  ),
              )
          except ValueError:
              raise ValueError(
                  'Unknown preprocessing method: "{0}"'
                  "\n".format(self._parameters.preprocessing)
              )
  
      # Set up the Q-function.
      shape_of_inputs = (
          self._shape_of_inputs
          if self._preprocessor is None
          else self._preprocessor.output_shape()
      )
      if self._parameters.q_representation == "dqn":
          model = Models.feedforward_network(
              shape_of_inputs,
              self._num_actions,
              self._parameters.hidden_layers,
              huber_loss
              if self._parameters.use_error_clipping
              else None,
          )
      elif self._parameters.q_representation == "dueling-dqn":
          model = Models.dueling_network(
              shape_of_inputs,
              self._num_actions,
              self._parameters.hidden_layers,
              huber_loss
              if self._parameters.use_error_clipping
              else None,
          )
      else:
          try:
              model_definition_function = self._import_method(
                  self._parameters.q_representation
              )
              model = model_definition_function(
                  shape_of_inputs,
                  self._num_actions,
                  huber_loss
                  if self._parameters.use_error_clipping
                  else None,
              )
          except ValueError:
              raise ValueError(
                  'Unknown representation for Q-learning: "{0}"'
                  "\n".format(self._parameters.q_representation)
              )
  
      self._q = model["f"]
      self._input_variables = model["inputs"]
      self._output_variables = model["outputs"]
      if self._parameters.use_prioritized_replay:
          self._weight_variables = C.ops.input_variable(
              shape=(1,), dtype=np.float32
          )
          self._loss = model["loss"] * self._weight_variables
      else:
          self._loss = model["loss"]
  
      minibatch_size = int(self._parameters.minibatch_size)
      # If gradient_clipping_threshold_per_sample is inf, gradient clipping
      # will not be performed. Set gradient_clipping_with_truncation to False
      # to clip the norm.
      # TODO: allow user to specify learner through config file.
      opt = C.learners.adam(
          self._q.parameters,
          C.learners.learning_parameter_schedule_per_sample(
              self._parameters.initial_eta
          ),
          use_mean_gradient=True,
          momentum=C.learners.momentum_schedule(
              self._parameters.momentum
          ),
          variance_momentum=C.learners.momentum_schedule(0.999),
          gradient_clipping_threshold_per_sample=self._parameters.gradient_clipping_threshold,
          gradient_clipping_with_truncation=False,
      )
      self._trainer = C.train.trainer.Trainer(
          self._q, (self._loss, None), opt
      )
  
      # Initialize target Q.
      self._target_q = self._q.clone("clone")
  
      # Initialize replay memory.
      self._replay_memory = ReplayMemory(
          self._parameters.replay_memory_capacity,
          self._parameters.use_prioritized_replay,
      )
  
      print(
          "Parameterized Q-learning agent using neural networks "
          '"{0}" with {1} actions.\n'
          "".format(
              self._parameters.q_representation, self._num_actions
          )
      )
  
      self.episode_count = 0
      self.step_count = 0
  
imports: import math
  import cntk
  import numpy
  import ast
  from .agent import AgentBaseClass
  from .shared.cntk_utils import huber_loss
  from .shared.models import Models
  from .shared.qlearning_parameters import QLearningParameters
  from .shared.replay_memory import ReplayMemory
label_desc: Use image node_id 1 to create a new QLearning object from inherited base classes: AgentBaseClass with example: obj = QLearning(config_filename, o_space, a_space)