-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathmeta_trainer.py
199 lines (161 loc) · 9.19 KB
/
meta_trainer.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
194
195
196
197
import tensorflow as tf
import numpy as np
import time
from utils import logger
class Trainer(object):
def __init__(self,algo,
env,
sampler,
sample_processor,
policy,
n_itr,
greedy_finish_time,
start_itr=0,
inner_batch_size = 500,
save_interval = 100):
self.algo = algo
self.env = env
self.sampler = sampler
self.sampler_processor = sample_processor
self.policy = policy
self.n_itr = n_itr
self.start_itr = start_itr
self.inner_batch_size = inner_batch_size
self.greedy_finish_time = greedy_finish_time
self.save_interval = save_interval
def train(self):
"""
Implement the MRLCO training process for task offloading problem
"""
start_time = time.time()
avg_ret = []
avg_loss = []
avg_latencies = []
for itr in range(self.start_itr, self.n_itr):
itr_start_time = time.time()
logger.log("\n ---------------- Iteration %d ----------------" % itr)
logger.log("Sampling set of tasks/goals for this meta-batch...")
task_ids = self.sampler.update_tasks()
paths = self.sampler.obtain_samples(log=False, log_prefix='')
#print("sampled path length is: ", len(paths[0]))
greedy_run_time = [self.greedy_finish_time[x] for x in task_ids]
logger.logkv('Average greedy latency,', np.mean(greedy_run_time))
""" ----------------- Processing Samples ---------------------"""
logger.log("Processing samples...")
samples_data = self.sampler_processor.process_samples(paths, log=False, log_prefix='')
""" ------------------- Inner Policy Update --------------------"""
policy_losses, value_losses = self.algo.UpdatePPOTarget(samples_data, batch_size=self.inner_batch_size )
#print("task losses: ", losses)
print("average task losses: ", np.mean(policy_losses))
avg_loss.append(np.mean(policy_losses))
print("average value losses: ", np.mean(value_losses))
""" ------------------ Resample from updated sub-task policy ------------"""
print("Evaluate the one-step update for sub-task policy")
new_paths = self.sampler.obtain_samples(log=True, log_prefix='')
new_samples_data = self.sampler_processor.process_samples(new_paths, log="all", log_prefix='')
""" ------------------ Outer Policy Update ---------------------"""
logger.log("Optimizing policy...")
self.algo.UpdateMetaPolicy()
""" ------------------- Logging Stuff --------------------------"""
ret = np.array([])
for i in range(5):
ret = np.concatenate((ret, np.sum(new_samples_data[i]['rewards'], axis=-1)), axis=-1)
avg_reward = np.mean(ret)
latency = np.array([])
for i in range(5):
latency = np.concatenate((latency, new_samples_data[i]['finish_time']), axis=-1)
avg_latency = np.mean(latency)
avg_latencies.append(avg_latency)
logger.logkv('Itr', itr)
logger.logkv('Average reward, ', avg_reward)
logger.logkv('Average latency,', avg_latency)
logger.dumpkvs()
avg_ret.append(avg_reward)
if itr % self.save_interval == 0:
self.policy.core_policy.save_variables(save_path="./meta_model_inner_step1/meta_model_"+str(itr)+".ckpt")
self.policy.core_policy.save_variables(save_path="./meta_model_inner_step1/meta_model_final.ckpt")
return avg_ret, avg_loss, avg_latencies
if __name__ == "__main__":
from env.mec_offloaing_envs.offloading_env import Resources
from env.mec_offloaing_envs.offloading_env import OffloadingEnvironment
from policies.meta_seq2seq_policy import MetaSeq2SeqPolicy
from samplers.seq2seq_meta_sampler import Seq2SeqMetaSampler
from samplers.seq2seq_meta_sampler_process import Seq2SeqMetaSamplerProcessor
from baselines.vf_baseline import ValueFunctionBaseline
from meta_algos.MRLCO import MRLCO
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
logger.configure(dir="./meta_offloading20_log-inner_step1/", format_strs=['stdout', 'log', 'csv'])
META_BATCH_SIZE = 10
resource_cluster = Resources(mec_process_capable=(10.0 * 1024 * 1024),
mobile_process_capable=(1.0 * 1024 * 1024),
bandwidth_up=7.0, bandwidth_dl=7.0)
env = OffloadingEnvironment(resource_cluster=resource_cluster,
batch_size=100,
graph_number=100,
graph_file_paths=[
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_1/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_2/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_3/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_5/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_6/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_7/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_9/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_10/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_11/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_13/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_14/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_15/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_17/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_18/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_19/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_21/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_22/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_23/random.20.",
"./env/mec_offloaing_envs/data/meta_offloading_20/offload_random20_25/random.20.",
],
time_major=False)
action, greedy_finish_time = env.greedy_solution()
print("avg greedy solution: ", np.mean(greedy_finish_time))
print()
finish_time = env.get_all_mec_execute_time()
print("avg all remote solution: ", np.mean(finish_time))
print()
finish_time = env.get_all_locally_execute_time()
print("avg all local solution: ", np.mean(finish_time))
print()
baseline = ValueFunctionBaseline()
meta_policy = MetaSeq2SeqPolicy(meta_batch_size=META_BATCH_SIZE, obs_dim=17, encoder_units=128, decoder_units=128,
vocab_size=2)
sampler = Seq2SeqMetaSampler(
env=env,
policy=meta_policy,
rollouts_per_meta_task=1, # This batch_size is confusing
meta_batch_size=META_BATCH_SIZE,
max_path_length=20000,
parallel=False,
)
sample_processor = Seq2SeqMetaSamplerProcessor(baseline=baseline,
discount=0.99,
gae_lambda=0.95,
normalize_adv=True,
positive_adv=False)
algo = MRLCO(policy=meta_policy,
meta_sampler=sampler,
meta_sampler_process=sample_processor,
inner_lr=5e-4,
outer_lr=5e-4,
meta_batch_size=META_BATCH_SIZE,
num_inner_grad_steps=1,
clip_value = 0.3)
trainer = Trainer(algo = algo,
env=env,
sampler=sampler,
sample_processor=sample_processor,
policy=meta_policy,
n_itr=2000,
greedy_finish_time= greedy_finish_time,
start_itr=0,
inner_batch_size=1000)
with tf.compat.v1.Session() as sess:
sess.run(tf.global_variables_initializer())
avg_ret, avg_loss, avg_latencies = trainer.train()