class_docstr: | `alias`: ``parl.Agent``
  | `alias`: ``parl.core.fluid.agent.Agent``
  
  | Agent is one of the three basic classes of PARL.
  
  | It is responsible for interacting with the environment and collecting data for training the policy.
  | To implement a customized ``Agent``, users can:
  
    .. code-block:: python
  
      import parl
  
      class MyAgent(parl.Agent):
          def __init__(self, algorithm, act_dim):
              super(MyAgent, self).__init__(algorithm)
              self.act_dim = act_dim
    This class will initialize the neural network parameters automatically, and provides an executor for users to run the programs (self.fluid_executor).
  
  Attributes:
      fluid_executor (fluid.Executor): executor for running programs of the agent.
      alg (parl.algorithm): algorithm of this agent.
  
  Public Functions:
      - ``build_program`` (**abstract function**): build various programs for the agent to interact with outer environment.
      - ``get_weights``: return a Python dictionary containing all the parameters of self.alg.
      - ``set_weights``: copy parameters from ``set_weights()`` to this agent.
      - ``sample``: return a noisy action to perform exploration according to the policy.
      - ``predict``: return an action given current observation.
      - ``learn``: update the parameters of self.alg using the `learn_program` defined in `build_program()`.
      - ``save``: save parameters of the ``agent`` to a given path.
      - ``restore``: restore previous saved parameters from a given path.
  
  Todo:
      - allow users to get parameters of a specified model by specifying the model's name in ``get_weights()``.
class_docstr_tok: ["|","`","alias","`",":","``","parl.Agent","``","|","`","alias","`",":","``","parl.core.fluid.agent.Agent","``","|","Agent","is","one","of","the","three","basic","classes","of","PARL",".","|","It","is","responsible","for","interacting","with","the","environment","and","collecting","data","for","training","the","policy",".","|","To","implement","a","customized","``","Agent","``",",","users","can",":","..","code-block",":",":","python","import","parl","class","MyAgent","(","parl.Agent",")",":","def","__init__","(","self",",","algorithm",",","act_dim",")",":","super","(","MyAgent",",","self",")",".__init__","(","algorithm",")","self.act_dim","=","act_dim","This","class","will","initialize","the","neural","network","parameters","automatically",",","and","provides","an","executor","for","users","to","run","the","programs","(","self.fluid_executor",")",".","Attributes",":","fluid_executor","(","fluid.Executor",")",":","executor","for","running","programs","of","the","agent",".","alg","(","parl.algorithm",")",":","algorithm","of","this","agent",".","Public","Functions",":","-","``","build_program","``","(","*","*","abstract","function","*","*",")",":","build","various","programs","for","the","agent","to","interact","with","outer","environment",".","-","``","get_weights","``",":","return","a","Python","dictionary","containing","all","the","parameters","of","self.alg",".","-","``","set_weights","``",":","copy","parameters","from","``","set_weights","(",")","``","to","this","agent",".","-","``","sample","``",":","return","a","noisy","action","to","perform","exploration","according","to","the","policy",".","-","``","predict","``",":","return","an","action","given","current","observation",".","-","``","learn","``",":","update","the","parameters","of","self.alg","using","the","`","learn_program","`","defined","in","`","build_program","(",")","`",".","-","``","save","``",":","save","parameters","of","the","``","agent","``","to","a","given","path",".","-","``","restore","``",":","restore","previous","saved","parameters","from","a","given","path",".","Todo",":","-","allow","users","to","get","parameters","of","a","specified","model","by","specifying","the","model","'s","name","in","``","get_weights","(",")","``","."]
docstr: Save parameters.
  
  Args:
      save_path(str): a directory where to save the parameters.
      program(fluid.Program): program that describes the neural network structure. If None, will all program.
  
  Raises:
      Error: if program does not exist
  
  Example:
  
  .. code-block:: python
  
      agent = AtariAgent()
      agent.save('./model_dir')
      agent.save('./model_dir', program=agent.learn_program)
docstr_tok: ["Save","parameters",".","Args",":","save_path","(","str",")",":","a","directory","where","to","save","the","parameters",".","program","(","fluid.Program",")",":","program","that","describes","the","neural","network","structure",".","If","None",",","will","all","program",".","Raises",":","Error",":","if","program","does","not","exist","Example",":","..","code-block",":",":","python","agent","=","AtariAgent","(",")","agent.save","(","'.\/model_dir","'",")","agent.save","(","'.\/model_dir","'",",","program=agent.learn_program",")"]
code: def save(self, save_path=None, program=None):
      """Save parameters.
  
      Args:
          save_path(str): a directory where to save the parameters.
          program(fluid.Program): program that describes the neural network structure. If None, will all program.
  
      Raises:
          Error: if program does not exist
  
      Example:
  
      .. code-block:: python
  
          agent = AtariAgent()
          agent.save('./model_dir')
          agent.save('./model_dir', program=agent.learn_program)
  
      """
      assert save_path is not None, "please specify `save_path` "
      if os.path.isfile(save_path):
          raise Exception(
              "can not save to {}, it is a file, not directory".format(
                  save_path
              )
          )
      if not os.path.exists(save_path):
          os.makedirs(save_path)
      all_programs = [
          (key, val)
          for (key, val) in self.__dict__.items()
          if (
              isinstance(val, fluid.framework.Program)
              or isinstance(val, fluid.compiler.CompiledProgram)
          )
      ]
  
      if program:
          filename = None
          for name, prog in all_programs:
              if program == prog:
                  filename = name
                  break
          if filename is None:
              raise Exception(
                  "can not find program {}.".format(program)
              )
          fluid.io.save_params(
              executor=self.fluid_executor,
              dirname=save_path,
              main_program=program,
              filename=filename,
          )
      else:
          for filename, program in all_programs:
              if isinstance(
                  program, fluid.framework.Program
              ) or isinstance(program, fluid.compiler.CompiledProgram):
                  fluid.io.save_params(
                      executor=self.fluid_executor,
                      dirname=save_path,
                      main_program=program,
                      filename=filename,
                  )
  
file_path: PARL/parl/core/fluid/agent.py
imports: import warnings
  import os
  import paddle
  import paddle.fluid
  from parl.core.fluid import layers
  from parl.core.agent_base import AgentBase
  from parl.core.fluid.algorithm import Algorithm
  from parl.utils import machine_info
  from parl.utils import logger
label_desc: Use image node_id 8 for calling the Agent obj's underlying member method code with example usage: obj.save(save_path, program) without return types