def set_agent(self,
                  agent_class: Callable,
                  agent_config: ConfigBase,
                  agent_kwargs: Dict[str, any],
                  gpu_memory_limit: int = 512):
        self.gpu_memory_limit = gpu_memory_limit
        VirtualGPU(gpu_device_id=self.device_id,
                   gpu_memory_limit=self.gpu_memory_limit)

        self.agent_class = agent_class
        config = agent_config.build()
        config.update(agent_kwargs)
        self.agent_config = config
        self.agent = agent_class(**self.agent_config)
    def example(cls,
                config: ConfigBase,
                render: bool = True,
                n_episodes: int = 500,
                max_episode_steps: int = 500) -> "RandomAgent":
        """Create, train, and save agent for a given config."""
        config_dict = config.build()

        agent = cls(**config_dict)

        agent.train(verbose=True,
                    render=render,
                    n_episodes=n_episodes,
                    max_episode_steps=max_episode_steps)
        agent.save()

        return agent
示例#3
0
    def _fit_agent(agent_class: Callable, agent_config: ConfigBase, training_options: Dict[str, Any],
                   gpu_memory_per_agent: int = 512) -> AgentBase:
        VirtualGPU(gpu_device_id=0, gpu_memory_limit=gpu_memory_per_agent)

        with warnings.catch_warnings():
            warnings.simplefilter('ignore', FutureWarning)

            config_dict = agent_config.build()
            # Give each agent a unique name for easier tracking with verbose and multiprocessing
            config_dict["name"] = f"{config_dict.get('name', 'Agent')}_{np.random.randint(0, 2 ** 16)}"

            agent: AgentBase = agent_class(**config_dict)
            agent.train(**training_options)
            # Might as well save agent. This will also unready and save buffers, models, etc.
            agent.save()
            agent.unready()

        return agent
    def example(cls,
                config: ConfigBase,
                render: bool = True,
                n_episodes: int = 500,
                max_episode_steps: int = 500,
                update_every: int = 1) -> "ReinforceAgent":
        """Create, train, and save agent for a given config."""
        VirtualGPU(config.gpu_memory)
        config_dict = config.build()

        agent = cls(**config_dict)

        agent.train(verbose=True,
                    render=render,
                    n_episodes=n_episodes,
                    max_episode_steps=max_episode_steps,
                    update_every=update_every)
        agent.save()

        return agent
    def example(cls, config: ConfigBase, render: bool = True,
                n_episodes: int = 500, max_episode_steps: int = 500, update_every: int = 10,
                checkpoint_every: int = 100) -> "DeepQAgent":
        """For a given config, create new, or load existing agent. Then train and save agent."""

        VirtualGPU(config.gpu_memory)

        config_dict = config.build()
        if os.path.exists(config_dict['name']):
            agent = cls.load(config_dict['name'])
            warnings.warn('Loaded existing agent.')
        else:
            agent = cls(**config_dict)

        agent.train(verbose=True, render=render,
                    n_episodes=n_episodes, max_episode_steps=max_episode_steps, update_every=update_every,
                    checkpoint_every=checkpoint_every)
        agent.save()

        return agent
示例#6
0
    def example(cls, config: ConfigBase, render: bool = True,
                n_episodes: int = 500, max_episode_steps: int = 500,
                checkpoint_every: int = 100, **kwargs) -> "AgentBase":
        """
        Default example runner.

        kwargs should contain algo specific settings and will be passed to .train. eg, update_every for dqn.
        """
        VirtualGPU(config.gpu_memory)

        config_dict = config.build()
        if os.path.exists(config_dict['name']):
            agent = cls.load(config_dict['name'])
            warnings.warn('Loaded existing agent.')
        else:
            agent = cls(**config_dict)  # noqa - __init__ will be available in non abstract children

        agent.train(verbose=True, render=render,
                    n_episodes=n_episodes, max_episode_steps=max_episode_steps,
                    checkpoint_every=checkpoint_every, **kwargs)
        agent.save()

        return agent