1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-16 17:12:20 +08:00
This commit is contained in:
Yuchen Fang
2021-01-28 14:14:33 +08:00
parent 4dc14a2489
commit 0c6cad1d7b
7 changed files with 27 additions and 13 deletions

View File

@@ -5,7 +5,7 @@ import logger
import json import json
import os import os
import agent import agent
import model import network
import policy import policy
import random import random
import tianshou as ts import tianshou as ts
@@ -48,7 +48,15 @@ def setup_seed(seed):
class BaseExecutor(object): class BaseExecutor(object):
def __init__( def __init__(
self, log_dir, resources, env_conf, optim=None, policy_conf=None, network=None, policy_path=None, seed=None, self,
log_dir,
resources,
env_conf,
optim=None,
policy_conf=None,
network_conf=None,
policy_path=None,
seed=None,
): ):
"""A base class for executor """A base class for executor
@@ -62,8 +70,8 @@ class BaseExecutor(object):
:type optim: dict, optional :type optim: dict, optional
:param policy_conf: Configurations for the RL algorithm, defaults to None :param policy_conf: Configurations for the RL algorithm, defaults to None
:type policy_conf: dict, optional :type policy_conf: dict, optional
:param network: Configurations for policy network, defaults to None :param network_conf: Configurations for policy network_conf, defaults to None
:type network: dict, optional :type network_conf: dict, optional
:param policy_path: If is not None, would load the policy from this path, defaults to None :param policy_path: If is not None, would load the policy from this path, defaults to None
:type policy_path: string, optional :type policy_path: string, optional
:param seed: Random seed, defaults to None :param seed: Random seed, defaults to None
@@ -90,17 +98,23 @@ class BaseExecutor(object):
self.policy = getattr(agent, policy_conf["name"])(policy_conf["config"]) self.policy = getattr(agent, policy_conf["name"])(policy_conf["config"])
# print(self.policy) # print(self.policy)
else: else:
assert not network is None assert not network_conf is None
if "extractor" in network.keys(): if "extractor" in network_conf.keys():
net = getattr(model, network["extractor"]["name"] + "_Extractor")( net = getattr(network, network_conf["extractor"]["name"] + "_Extractor")(
device=self.device, **network["config"] device=self.device, **network_conf["config"]
) )
else: else:
net = getattr(model, network["name"] + "_Extractor")(device=self.device, **network["config"]) net = getattr(network, network_conf["name"] + "_Extractor")(
device=self.device, **network_conf["config"]
)
net.to(self.device) net.to(self.device)
actor = getattr(model, network["name"] + "_Actor")(extractor=net, device=self.device, **network["config"]) actor = getattr(network, network_conf["name"] + "_Actor")(
extractor=net, device=self.device, **network_conf["config"]
)
actor.to(self.device) actor.to(self.device)
critic = getattr(model, network["name"] + "_Critic")(extractor=net, device=self.device, **network["config"]) critic = getattr(network, network_conf["name"] + "_Critic")(
extractor=net, device=self.device, **network_conf["config"]
)
critic.to(self.device) critic.to(self.device)
self.optim = torch.optim.Adam( self.optim = torch.optim.Adam(
list(actor.parameters()) + list(critic.parameters()), list(actor.parameters()) + list(critic.parameters()),
@@ -180,7 +194,7 @@ class Executor(BaseExecutor):
io_conf, io_conf,
optim=None, optim=None,
policy_conf=None, policy_conf=None,
network=None, network_conf=None,
policy_path=None, policy_path=None,
seed=None, seed=None,
share_memory=False, share_memory=False,
@@ -210,7 +224,7 @@ class Executor(BaseExecutor):
:param buffer_size: The size of replay buffer, defaults to 200000 :param buffer_size: The size of replay buffer, defaults to 200000
:type buffer_size: int, optional :type buffer_size: int, optional
""" """
super().__init__(log_dir, resources, env_conf, optim, policy_conf, network, policy_path, seed) super().__init__(log_dir, resources, env_conf, optim, policy_conf, network_conf, policy_path, seed)
single_env = getattr(env, env_conf["name"]) single_env = getattr(env, env_conf["name"])
env_conf = merge_dicts(env_conf, train_paths) env_conf = merge_dicts(env_conf, train_paths)
env_conf["log"] = True env_conf["log"] = True