-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
165 lines (133 loc) · 4.15 KB
/
train.py
File metadata and controls
165 lines (133 loc) · 4.15 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from dqn_custom_policies import CustomDQNPolicy
from hyperparams import HYPERPARAMETERS
from models import XGEM
from utils import VideoRecorderCallback
import argparse
import os
import numpy as np
import torch
import torch.nn as nn
import gym
from stable_baselines3 import DQN
from stable_baselines3.common.callbacks import (
EvalCallback,
EveryNTimesteps
)
from stable_baselines3.common.monitor import Monitor
from stable_baselines3.common.vec_env import DummyVecEnv
import wandb
from wandb.integration.sb3 import WandbCallback
from multiprocessing import Process
PROJECT = 'synaptogen'
CONFIG= {
'method': 'grid',
'metric': {
'name': 'eval/mean_reward',
'goal': 'maximize'
},
'parameters': {
'num_genes': {'values': [16, 32, 64]},
'num_nts': {'values': [3]},
'learning_rate': {'values': [3e-2, 3e-3, 3e-4]},
'seed': {'values': [1, 2, 3]}
}
}
HIDDEN_SIZE = 128
M = 5 # multiplicative factor for the default number of training steps
NUM_PROCESSES = 3
parser = argparse.ArgumentParser()
parser.add_argument('-q', '--qnet', type=str)
parser.add_argument('-e', '--env', type=int)
parser.add_argument('-c', '--cuda', type=int)
parser.add_argument('--bio', action='store_true')
parser.add_argument('--snn', action='store_true')
args = parser.parse_args()
# use the right model if SNNs are requested
if args.snn: from models import SpikingXGEM as XGEM
ENV_NAME = list(HYPERPARAMETERS.keys())[args.env]
# load the biological genetic rules
if args.bio:
npz = np.load('data/genetic_rules.npz', allow_pickle=True)
O = torch.tensor(npz['O']).bool()
# set the correct number of genes
CONFIG['parameters']['num_genes']['values'] = [O.shape[0]]
wandb.login()
sweep_id = wandb.sweep(CONFIG, project=PROJECT)
def make_env():
env = gym.make(ENV_NAME)
env = Monitor(env)
return env
env = DummyVecEnv([make_env])
features_dim = env.observation_space.shape[0]
action_dim = env.action_space.n
hyperparameters = HYPERPARAMETERS[ENV_NAME]
def train():
CKPTS_DIR = 'ckpts'
if args.bio: CKPTS_DIR += '-bio'
if args.snn: CKPTS_DIR += '-snn'
run = wandb.init(sync_tensorboard=True)
config = wandb.config
num_genes = config['num_genes']
num_nts = config['num_nts']
seed = config['seed']
hyperparameters['learning_rate'] = config['learning_rate']
# TODO: restore
# # restore the original eps scheduler
# hyperparameters['exploration_fraction'] = hyperparameters['exploration_fraction']/M
torch.manual_seed(seed) # weights initialization seed
if args.qnet == 'mlp':
q_net = nn.Sequential(
nn.Linear(features_dim, HIDDEN_SIZE),
nn.ReLU(),
nn.Linear(HIDDEN_SIZE, action_dim)
)
elif args.qnet == 'gem':
q_net = XGEM(
layer_sizes=[features_dim, HIDDEN_SIZE, action_dim],
num_genes=num_genes,
num_nts=num_nts,
O_temperature=.1,
C_scale=1.,
rules=O if args.bio else None # inject the genetic rules
)
else:
raise Exception('Invalid Q network')
model = DQN(
CustomDQNPolicy,
env,
verbose=1,
tensorboard_log=f'runs/{run.id}',
policy_kwargs={'q_net': q_net},
device=f'cuda:{args.cuda}',
seed=seed,
**hyperparameters
)
model.learn(
total_timesteps=M*1e5,
callback=[
WandbCallback(verbose=2),
EveryNTimesteps(
n_steps=1e4,
callback=VideoRecorderCallback(ENV_NAME)
),
EvalCallback(
eval_env=model.env,
eval_freq=1e4,
best_model_save_path=os.path.join(CKPTS_DIR, run.id)
)
]
)
run.finish()
# parallelize sweep on multiple processes
def run_agent():
wandb.agent(sweep_id, function=train, project=PROJECT)
# create a list to store the processes
processes = []
# start the parallel processes
for _ in range(NUM_PROCESSES):
process = Process(target=run_agent)
process.start()
processes.append(process)
# wait for all processes to finish
for process in processes:
process.join()