-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtrain_maskable_ppo.py
More file actions
142 lines (123 loc) · 3.86 KB
/
Copy pathtrain_maskable_ppo.py
File metadata and controls
142 lines (123 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
from typing import Optional, Tuple, Union
from datetime import datetime
import fire
import torch
import warnings
import numpy as np
from sb3_contrib.ppo_mask import MaskablePPO
from stable_baselines3.common.callbacks import BaseCallback
from alphagen.rl.env.wrapper import AlphaEnv
from alphagen.rl.policy import LSTMSharedNet, TransformerSharedNet
from alphagen.utils.random import reseed_everything
from alphagen.rl.env.core import AlphaEnvCore
class CustomCallback(BaseCallback):
def __init__(self,
save_freq: int,
show_freq: int,
save_path: str,
name_prefix: str = 'rl_model',
timestamp: Optional[str] = None,
verbose: bool = False):
super().__init__(verbose)
self.save_freq = save_freq
self.show_freq = show_freq
self.save_path = save_path
self.name_prefix = name_prefix
self.verbose = verbose
if timestamp is None:
self.timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
else:
self.timestamp = timestamp
def _init_callback(self) -> None:
if self.save_path is not None:
os.makedirs(self.save_path, exist_ok=True)
def _on_step(self) -> bool:
return True
def _on_rollout_end(self) -> None:
assert self.logger is not None
self.save_checkpoint()
def save_checkpoint(self):
path = os.path.join(self.save_path, f'{self.name_prefix}_{self.timestamp}', f'{self.num_timesteps}_steps')
self.model.save(path) # type: ignore
if self.verbose:
print(f'Saving model checkpoint to {path}')
@property
def env_core(self) -> AlphaEnvCore:
return self.training_env.envs[0].unwrapped # type: ignore
def main(
seed: int = 0,
loss_metric: str = "Sharpe",
steps: int = 100_000,
):
warnings.filterwarnings('ignore')
reseed_everything(seed)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
env = AlphaEnv(device=device, print_expr=True, loss_metric=loss_metric)
name_prefix = f"new_{loss_metric}_{seed}"
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
checkpoint_callback = CustomCallback(
save_freq=10000,
show_freq=10000,
save_path='./checkpoints',
name_prefix=name_prefix,
timestamp=timestamp,
verbose=1,
)
model = MaskablePPO(
'MlpPolicy',
env,
# UNCOMMENT FOR LSTM POLICY
# policy_kwargs=dict(
# features_extractor_class=LSTMSharedNet,
# features_extractor_kwargs=dict(
# n_layers=2,
# d_model=128,
# dropout=0.1,
# device=device,
# ),
# ),
policy_kwargs=dict(
features_extractor_class=TransformerSharedNet,
features_extractor_kwargs=dict(
n_encoder_layers=2,
d_model=128,
n_head=4,
d_ffn=256,
dropout=0.1,
device=device,
),
),
gamma=1.,
ent_coef=0.01,
batch_size=128,
tensorboard_log='./tensorboard',
device=device,
verbose=1,
)
model.learn(
total_timesteps=steps,
callback=checkpoint_callback,
tb_log_name=f'{name_prefix}_{timestamp}',
)
def fire_helper(
seed: Union[int, Tuple[int]],
loss_metric: str = "Sharpe",
steps: int = None
):
if isinstance(seed, int):
seed = (seed, )
default_steps = {
1: 100_000,
10: 250_000,
20: 300_000,
50: 350_000,
100: 400_000
}
for _seed in seed:
main(_seed,
loss_metric,
default_steps[1] if steps is None else int(steps)
)
if __name__ == '__main__':
fire.Fire(fire_helper)