class_docstr: | `alias`: ``parl.Model``
  | `alias`: ``parl.core.fluid.agent.Model``
  
  | ``Model`` is a base class of PARL for the neural network. A ``Model`` is usually a policy or Q-value function, which predicts an action or an estimate according to the environmental observation.
  
  | To track all the layers , users are required to implement neural networks with the layers from ``parl.layers`` (e.g., parl.layers.fc). These layers has the same APIs as fluid.layers.
  
  | ``Model`` supports duplicating a ``Model`` instance in a pythonic way:
  
  | ``copied_model = copy.deepcopy(model)``
  
  Example:
  
  .. code-block:: python
  
      import parl
  
      class Policy(parl.Model):
          def __init__(self):
              self.fc = parl.layers.fc(size=12, act='softmax')
  
          def policy(self, obs):
              out = self.fc(obs)
              return out
             
      policy = Policy() 
      copied_policy = copy.deepcopy(model)
  
  Attributes:
      model_id(str): each model instance has its unique model_id.
  
  Public Functions:
      - ``sync_weights_to``: synchronize parameters of the current model to another model.
      - ``get_weights``: return a list containing all the parameters of the current model.
      - ``set_weights``: copy parameters from ``set_weights()`` to the model.
      - ``forward``: define the computations of a neural network. **Should** be overridden by all subclasses.
      - ``parameters``: return a list containting names of parameters of the model. 
      - ``set_model_id``: set ``model_id`` of current model explicitly.
      - ``get_model_id``: return the ``model_id`` of current model.
class_docstr_tok: ["|","`","alias","`",":","``","parl.Model","``","|","`","alias","`",":","``","parl.core.fluid.agent.Model","``","|","``","Model","``","is","a","base","class","of","PARL","for","the","neural","network",".","A","``","Model","``","is","usually","a","policy","or","Q-value","function",",","which","predicts","an","action","or","an","estimate","according","to","the","environmental","observation",".","|","To","track","all","the","layers",",","users","are","required","to","implement","neural","networks","with","the","layers","from","``","parl.layers","``","(","e.g.",",","parl.layers.fc",")",".","These","layers","has","the","same","APIs","as","fluid.layers",".","|","``","Model","``","supports","duplicating","a","``","Model","``","instance","in","a","pythonic","way",":","|","``","copied_model","=","copy.deepcopy","(","model",")","``","Example",":","..","code-block",":",":","python","import","parl","class","Policy","(","parl.Model",")",":","def","__init__","(","self",")",":","self.fc","=","parl.layers.fc","(","size=12",",","act='softmax","'",")","def","policy","(","self",",","obs",")",":","out","=","self.fc","(","obs",")","return","out","policy","=","Policy","(",")","copied_policy","=","copy.deepcopy","(","model",")","Attributes",":","model_id","(","str",")",":","each","model","instance","has","its","unique","model_id",".","Public","Functions",":","-","``","sync_weights_to","``",":","synchronize","parameters","of","the","current","model","to","another","model",".","-","``","get_weights","``",":","return","a","list","containing","all","the","parameters","of","the","current","model",".","-","``","set_weights","``",":","copy","parameters","from","``","set_weights","(",")","``","to","the","model",".","-","``","forward","``",":","define","the","computations","of","a","neural","network",".","*","*","Should","*","*","be","overridden","by","all","subclasses",".","-","``","parameters","``",":","return","a","list","containting","names","of","parameters","of","the","model",".","-","``","set_model_id","``",":","set","``","model_id","``","of","current","model","explicitly",".","-","``","get_model_id","``",":","return","the","``","model_id","``","of","current","model","."]
docstr: Synchronize parameters of current model to another model.
  
  To speed up the synchronizing process, it will create a program implicitly to finish the process. It
  also stores a program as the cache to avoid creating program repeatedly.
  
  target_model_weights = decay * target_model_weights + (1 - decay) * current_model_weights
  
  Args:
      target_model (`parl.Model`): an instance of ``Model`` that has the same neural network architecture as the current model.
      decay (float):  the rate of decline in copying parameters. 0 if no parameters decay when synchronizing the parameters.
      share_vars_parallel_executor (fluid.ParallelExecutor): Optional. If not None, will use ``fluid.ParallelExecutor``
                                                             to run program instead of ``fluid.Executor``.
  
  Example:
  
  .. code-block:: python
  
      import copy
      # create a model that has the same neural network structures.
      target_model = copy.deepcopy(model)
  
      # after initilizing the parameters ...
      model.sync_weights_to(target_mdodel)
  
  Note:
      Before calling ``sync_weights_to``, parameters of the model must have been initialized.
docstr_tok: ["Synchronize","parameters","of","current","model","to","another","model",".","To","speed","up","the","synchronizing","process",",","it","will","create","a","program","implicitly","to","finish","the","process",".","It","also","stores","a","program","as","the","cache","to","avoid","creating","program","repeatedly",".","target_model_weights","=","decay","*","target_model_weights","+","(","1","-","decay",")","*","current_model_weights","Args",":","target_model","(","`","parl.Model","`",")",":","an","instance","of","``","Model","``","that","has","the","same","neural","network","architecture","as","the","current","model",".","decay","(","float",")",":","the","rate","of","decline","in","copying","parameters",".","0","if","no","parameters","decay","when","synchronizing","the","parameters",".","share_vars_parallel_executor","(","fluid.ParallelExecutor",")",":","Optional",".","If","not","None",",","will","use","``","fluid.ParallelExecutor","``","to","run","program","instead","of","``","fluid.Executor","``",".","Example",":","..","code-block",":",":","python","import","copy","#","create","a","model","that","has","the","same","neural","network","structures",".","target_model","=","copy.deepcopy","(","model",")","#","after","initilizing","the","parameters","...","model.sync_weights_to","(","target_mdodel",")","Note",":","Before","calling","``","sync_weights_to","``",",","parameters","of","the","model","must","have","been","initialized","."]
code: def sync_weights_to(
      self, target_model, decay=0.0, share_vars_parallel_executor=None
  ):
      """Synchronize parameters of current model to another model.
  
      To speed up the synchronizing process, it will create a program implicitly to finish the process. It
      also stores a program as the cache to avoid creating program repeatedly.
  
      target_model_weights = decay * target_model_weights + (1 - decay) * current_model_weights
  
      Args:
          target_model (`parl.Model`): an instance of ``Model`` that has the same neural network architecture as the current model.
          decay (float):  the rate of decline in copying parameters. 0 if no parameters decay when synchronizing the parameters.
          share_vars_parallel_executor (fluid.ParallelExecutor): Optional. If not None, will use ``fluid.ParallelExecutor``
                                                                 to run program instead of ``fluid.Executor``.
  
      Example:
  
      .. code-block:: python
  
          import copy
          # create a model that has the same neural network structures.
          target_model = copy.deepcopy(model)
  
          # after initilizing the parameters ...
          model.sync_weights_to(target_mdodel)
  
      Note:
          Before calling ``sync_weights_to``, parameters of the model must have been initialized.
      """
  
      args_hash_id = hashlib.md5(
          "{}_{}".format(id(target_model), decay).encode("utf-8")
      ).hexdigest()
      has_cached = False
      try:
          if self._cached_id == args_hash_id:
              has_cached = True
      except AttributeError:
          has_cached = False
  
      if not has_cached:
          # Can not run _cached program, need create a new program
          self._cached_id = args_hash_id
  
          assert (
              not target_model is self
          ), "cannot copy between identical model"
          assert isinstance(target_model, Model)
          assert (
              self.__class__.__name__ == target_model.__class__.__name__
          ), "must be the same class for params syncing!"
          assert decay >= 0 and decay <= 1
  
          param_pairs = self._get_parameter_pairs(self, target_model)
  
          self._cached_sync_weights_program = fluid.Program()
  
          with fluid.program_guard(self._cached_sync_weights_program):
              for src_var_name, target_var_name in param_pairs:
                  src_var = fetch_framework_var(src_var_name)
                  target_var = fetch_framework_var(target_var_name)
                  fluid.layers.assign(
                      decay * target_var + (1 - decay) * src_var,
                      target_var,
                  )
  
          if share_vars_parallel_executor is None:
              # use fluid.Executor
              place = (
                  fluid.CUDAPlace(0)
                  if machine_info.is_gpu_available()
                  else fluid.CPUPlace()
              )
              self._cached_fluid_executor = fluid.Executor(place)
          else:
              # use fluid.ParallelExecutor
  
              # specify strategy to make ParallelExecutor run faster
              exec_strategy = fluid.ExecutionStrategy()
              exec_strategy.use_experimental_executor = True
              exec_strategy.num_threads = 4
              build_strategy = fluid.BuildStrategy()
              build_strategy.remove_unnecessary_lock = True
  
              with fluid.scope_guard(fluid.global_scope().new_scope()):
                  self._cached_fluid_executor = fluid.ParallelExecutor(
                      use_cuda=machine_info.is_gpu_available(),
                      main_program=self._cached_sync_weights_program,
                      share_vars_from=share_vars_parallel_executor,
                      exec_strategy=exec_strategy,
                      build_strategy=build_strategy,
                  )
      if share_vars_parallel_executor is None:
          self._cached_fluid_executor.run(
              self._cached_sync_weights_program
          )
      else:
          self._cached_fluid_executor.run(fetch_list=[])
  
file_path: PARL/parl/core/fluid/model.py
imports: import hashlib
  import paddle.fluid
  from parl.core.fluid.layers.layer_wrappers import LayerFunc
  from parl.core.fluid.plutils import *
  from parl.core.model_base import ModelBase
  from parl.utils import machine_info
  from parl.core.fluid.model_helper import global_model_helper
label_desc: Use image node_id 6 for calling the Model obj's underlying member method code with example usage: obj.sync_weights_to(target_model, decay, share_vars_parallel_executor) without return types