forked from rems75/SPIBB-DQN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
133 lines (116 loc) · 6.79 KB
/
train.py
File metadata and controls
133 lines (116 loc) · 6.79 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
import click
import numpy as np
import os
import torch
import yaml
from ai import AI
from experiment import DQNExperiment, BatchExperiment
from dataset import Dataset_Counts
from environments import environment
from baseline import Baseline, ClonedBaseline, SimilarityBaseline
@click.command()
@click.option('--config_file', '-c', default=None, help="config file")
@click.option('--options', '-o', multiple=True, nargs=2, type=click.Tuple([str, str]))
def run(config_file, options):
try:
params = yaml.safe_load(open(config_file, 'r'))
except FileNotFoundError as e:
print("Configuration file not found")
raise e
# replacing params with command line options
for opt in options:
assert opt[0] in params
dtype = type(params[opt[0]])
if dtype == bool:
new_opt = False if opt[1] != 'True' else True
else:
new_opt = dtype(opt[1])
params[opt[0]] = new_opt
print('\n')
print('Parameters ')
for key in params:
print(key, params[key])
print('\n')
np.random.seed(params['seed'])
torch.manual_seed(params['seed'])
random_state = np.random.RandomState(params['seed'])
device = torch.device(params["device"])
DATA_DIR = os.path.join(params['folder_location'], params['folder_name'])
env = environment.Environment(params["domain"], params, random_state)
if params['batch']:
dataset_path = params['dataset_path']
print("\nLoading dataset from file {}".format(dataset_path), flush=True)
if not os.path.exists(dataset_path):
raise ValueError("The dataset file does not exist")
dataset = Dataset_Counts.load_dataset(dataset_path)
baseline_path = os.path.join(DATA_DIR, params['baseline_path'])
if 'behavior_cloning' in params['learning_type']:
baseline_path = os.path.join(os.path.dirname(dataset_path), 'cloned_network_weights.pt')
baseline = ClonedBaseline(
params['network_size'], network_path=baseline_path, state_shape=params['state_shape'],
nb_actions=params['nb_actions'], device=device, seed=params['seed'],
temperature=params['baseline_temp'], normalize=params['normalize'])
elif params['learning_type'] in ['pi_b', 'soft_sort']:
baseline = Baseline(params['network_size'], network_path=baseline_path, state_shape=params['state_shape'],
nb_actions=params['nb_actions'], device=device, seed=params['seed'],
temperature=params['baseline_temp'], normalize=params['normalize'])
elif 'count_based' in params['learning_type']:
baseline = SimilarityBaseline(dataset=dataset, seed=params['seed'], nb_actions=params['nb_actions'],
results_folder=os.path.dirname(dataset_path))
baseline.evaluate_baseline(env, number_of_steps=100000, number_of_epochs=1,
verbose=True, save_results=True)
else:
# no baseline, should use counters to estimate policy
baseline = None
folder_name = os.path.dirname(dataset_path)
print("Data with counts loaded: {} samples".format(dataset.size), flush=True)
expt = BatchExperiment(dataset=dataset, env=env, folder_name=folder_name, episode_max_len=params['episode_max_len'],
minimum_count=params['minimum_count'], extra_stochasticity=params['extra_stochasticity'],
history_len=params['history_len'], max_start_nullops=params['max_start_nullops'],
keep_all_logs=False)
else:
# Create experiment folder
if not os.path.exists(DATA_DIR):
os.makedirs(DATA_DIR)
folder_name = DATA_DIR
baseline = None
expt = DQNExperiment(env=env, ai=None, episode_max_len=params['episode_max_len'], annealing=params['annealing'],
history_len=params['history_len'], max_start_nullops=params['max_start_nullops'],
replay_min_size=params['replay_min_size'], test_epsilon=params['test_epsilon'],
folder_name=folder_name, network_path=params['network_path'],
extra_stochasticity=params['extra_stochasticity'], score_window_size=100,
keep_all_logs=False)
for ex in range(params['num_experiments']):
print('\n')
print('>>>>> Experiment ', ex, ' >>>>> ',
params['learning_type'], ' >>>>> Epsilon >>>>> ',
params['epsilon_soft'], ' >>>>> Minimum Count >>>>> ',
params['minimum_count'], ' >>>>> Kappa >>>>> ',
params['kappa'], ' >>>>> ', flush=True)
print('\n')
print("\nPROGRESS: {0:02.2f}%\n".format(ex / params['num_experiments'] * 100), flush=True)
ai = AI(baseline, state_shape=env.state_shape, nb_actions=env.nb_actions, action_dim=params['action_dim'],
reward_dim=params['reward_dim'], history_len=params['history_len'], gamma=params['gamma'],
learning_rate=params['learning_rate'], epsilon=params['epsilon'], final_epsilon=params['final_epsilon'],
test_epsilon=params['test_epsilon'], annealing_steps=params['annealing_steps'], minibatch_size=params['minibatch_size'],
replay_max_size=params['replay_max_size'], update_freq=params['update_freq'],
learning_frequency=params['learning_frequency'], ddqn=params['ddqn'], learning_type=params['learning_type'],
network_size=params['network_size'], normalize=params['normalize'], device=device,
kappa=params['kappa'], minimum_count=params['minimum_count'], epsilon_soft=params['epsilon_soft'])
expt.ai = ai
if not params['batch']:
# resets dataset for online experiment
expt.dataset_counter = Dataset_Counts(count_param=params['count_param'],
state_shape=env.state_shape,
nb_actions=env.nb_actions,
replay_max_size=params['replay_max_size'],
is_counting=ai.needs_state_action_counter())
env.reset()
with open(expt.folder_name + '/config.yaml', 'w') as y:
yaml.safe_dump(params, y) # saving params for reference
expt.do_epochs(number_of_epochs=params['num_epochs'], is_learning=params['is_learning'],
steps_per_epoch=params['steps_per_epoch'], is_testing=params['is_testing'],
steps_per_test=params['steps_per_test'],
passes_on_dataset=params['passes_on_dataset'], exp_id=ex)
if __name__ == '__main__':
run()