Ejemplo n.º 1
0
    def __init__(self, model_config: core.ModelConfig, backend_name: str,
                 tf_eager_execution: bool):
        """
        Args:
            model_config: defines the model and environment to be used
            backend_name: id of the backend to which this agent belongs to.
        """
        global _tf_eager_execution_active

        assert model_config is not None, "model_config not set."
        assert backend_name

        if _tf_eager_execution_active is None:
            _tf_eager_execution_active = tf_eager_execution
        assert _tf_eager_execution_active == tf_eager_execution, \
            "Due to an incompatibility between tensorforce and tfagents their agents can not be instantiated in the" +\
            "same python runtime instance (conflicting excpectations on tensorflows eager execution mode)."

        self._backend_name: str = backend_name
        self.model_config = model_config
        self._agent_context: core.AgentContext = core.AgentContext(
            self.model_config)
        self._agent_context.gym._totals = monitor._register_gym_monitor(
            self.model_config.original_env_name)
        self.model_config.gym_env_name = self._agent_context.gym._totals.gym_env_name

        self._preprocess_callbacks: List[core._PreProcessCallback] = [
            plot._PreProcess()
        ]
        self._callbacks: List[core.AgentCallback] = []
        self._postprocess_callbacks: List[core._PostProcessCallback] = [
            plot._PostProcess()
        ]

        self._train_total_episodes_on_iteration_begin: int = 0
Ejemplo n.º 2
0
    def _prepare_callbacks(self, callbacks: List[core.AgentCallback],
                           default_plots: Optional[bool],
                           default_plot_callbacks: List[plot._PlotCallback],
                           ) -> List[core.AgentCallback]:
        """Adds the default callbacks and sorts all callbacks in the order
            _PreProcessCallbacks, AgentCallbacks, _PostProcessCallbacks.

        Args:
            callbacks: existing callbacks to prepare
            default_plots: if set or if None and callbacks does not contain plots then the default plots are added
            default_plot_callbacks: plot callbacks to add.
        """
        pre_process: List[core.AgentCallback] = [plot._PreProcess()]
        agent: List[core.AgentCallback] = []
        post_process: List[core.AgentCallback] = [plot._PostProcess()]

        if default_plots is None:
            default_plots = True
            for c in callbacks:
                default_plots = default_plots and (not isinstance(c, plot._PlotCallback))
        if default_plots:
            agent = default_plot_callbacks

        for c in callbacks:
            if isinstance(c, core._PreProcessCallback):
                pre_process.append(c)
            else:
                if isinstance(c, core._PostProcessCallback):
                    post_process.append(c)
                else:
                    agent.append(c)
        result: List[core.AgentCallback] = pre_process + agent + post_process
        return result
Ejemplo n.º 3
0
    def __init__(self,
                 model_config: core.ModelConfig,
                 backend_name: str,
                 tensorflow_v2_eager: bool = True):
        """
        Args:
            model_config: defines the model and environment to be used
            backend_name: id of the backend to which this agent belongs to.
            tensorflow_v2_eager: the execution mode, enforced for this and all other backend agents.
        """
        assert model_config is not None, "model_config not set."
        assert backend_name

        self._backend_name: str = backend_name
        self._tensorflow_v2_eager = tensorflow_v2_eager
        self.model_config = model_config
        self._agent_context: core.AgentContext = core.AgentContext(
            self.model_config)
        self._agent_context.gym._totals = monitor._register_gym_monitor(
            self.model_config.original_env_name)
        self.model_config.gym_env_name = self._agent_context.gym._totals.gym_env_name

        self._preprocess_callbacks: List[core._PreProcessCallback] = [
            plot._PreProcess()
        ]
        self._callbacks: List[core.AgentCallback] = []
        self._postprocess_callbacks: List[core._PostProcessCallback] = [
            plot._PostProcess()
        ]

        self._train_total_episodes_on_iteration_begin: int = 0
        self._initialize_tensorflow()