forked from miyosuda/unreal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
193 lines (149 loc) · 4.89 KB
/
main.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import threading
import numpy as np
import signal
import random
import math
import os
import time
from environment.environment import Environment
from model.model import UnrealModel
from train.trainer import Trainer
from train.rmsprop_applier import RMSPropApplier
from constants import *
def log_uniform(lo, hi, rate):
log_lo = math.log(lo)
log_hi = math.log(hi)
v = log_lo * (1-rate) + log_hi * rate
return math.exp(v)
device = "/cpu:0"
if USE_GPU:
device = "/gpu:0"
initial_learning_rate = log_uniform(INITIAL_ALPHA_LOW,
INITIAL_ALPHA_HIGH,
INITIAL_ALPHA_LOG_RATE)
global_t = 0
stop_requested = False
terminate_reqested = False
action_size = Environment.get_action_size()
global_network = UnrealModel(action_size, -1, device)
trainers = []
learning_rate_input = tf.placeholder("float")
grad_applier = RMSPropApplier(learning_rate = learning_rate_input,
decay = RMSP_ALPHA,
momentum = 0.0,
epsilon = RMSP_EPSILON,
clip_norm = GRAD_NORM_CLIP,
device = device)
for i in range(PARALLEL_SIZE):
trainer = Trainer(i,
global_network,
initial_learning_rate,
learning_rate_input,
grad_applier,
MAX_TIME_STEP,
device = device)
trainers.append(trainer)
# prepare session
config = tf.ConfigProto(log_device_placement=False, allow_soft_placement=True)
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
init = tf.global_variables_initializer()
# init = tf.initialize_all_variables()
sess.run(init)
# summary for tensorboard
score_input = tf.placeholder(tf.int32)
tf.summary.scalar("score", score_input)
summary_op = tf.summary.merge_all()
summary_writer = tf.summary.FileWriter(LOG_FILE, sess.graph)
# init or load checkpoint with saver
saver = tf.train.Saver()
checkpoint = tf.train.get_checkpoint_state(CHECKPOINT_DIR)
if checkpoint and checkpoint.model_checkpoint_path:
saver.restore(sess, checkpoint.model_checkpoint_path)
print("checkpoint loaded:", checkpoint.model_checkpoint_path)
tokens = checkpoint.model_checkpoint_path.split("-")
# set global step
global_t = int(tokens[1])
print(">>> global step set: ", global_t)
# set wall time
wall_t_fname = CHECKPOINT_DIR + '/' + 'wall_t.' + str(global_t)
with open(wall_t_fname, 'r') as f:
wall_t = float(f.read())
next_save_steps = (global_t + SAVE_INTERVAL_STEP) // SAVE_INTERVAL_STEP * SAVE_INTERVAL_STEP
else:
print("Could not find old checkpoint")
# set wall time
wall_t = 0.0
next_save_steps = SAVE_INTERVAL_STEP
def save(current_global_step):
""" Save checkpoint.
Called from therad-0.
"""
global next_save_steps
global train_threads
global trainers
global saver
global stop_requested
stop_requested = True
# Wait for all other threads to stop
for (i, t) in enumerate(train_threads):
if i != 0:
t.join()
# Save
if not os.path.exists(CHECKPOINT_DIR):
os.mkdir(CHECKPOINT_DIR)
# Write wall time
wall_t = time.time() - start_time
wall_t_fname = CHECKPOINT_DIR + '/' + 'wall_t.' + str(global_t)
with open(wall_t_fname, 'w') as f:
f.write(str(wall_t))
print('Start saving.')
saver.save(sess, CHECKPOINT_DIR + '/' + 'checkpoint', global_step = global_t)
print('End saving.')
stop_requested = False
next_save_steps += SAVE_INTERVAL_STEP
# Restart other threads
for i in range(PARALLEL_SIZE):
if i != 0:
thread = threading.Thread(target=train_function, args=(i,))
train_threads[i] = thread
thread.start()
def train_function(parallel_index):
""" Train each environment. """
global global_t
trainer = trainers[parallel_index]
# set start_time
start_time = time.time() - wall_t
trainer.set_start_time(start_time)
while True:
if stop_requested:
break
if terminate_reqested:
break
if global_t > MAX_TIME_STEP:
break
if parallel_index == 0 and global_t > next_save_steps:
# Save checkpoint
save(global_t)
diff_global_t = trainer.process(sess, global_t, summary_writer,
summary_op, score_input)
global_t += diff_global_t
def signal_handler(signal, frame):
global terminate_reqested
print('You pressed Ctrl+C!')
terminate_reqested = True
train_threads = []
for i in range(PARALLEL_SIZE):
train_threads.append(threading.Thread(target=train_function, args=(i,)))
signal.signal(signal.SIGINT, signal_handler)
# set start time
start_time = time.time() - wall_t
for t in train_threads:
t.start()
print('Press Ctrl+C to stop')
signal.pause()