-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
354 lines (295 loc) · 14.2 KB
/
train.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import argparse
import os
import numpy as np
import random
from tqdm import tqdm
import torch
import torch.optim as optim
from torch.autograd import Variable
import logging
from model_ppo_torch import Actor, Critic
from test_ppo_torch import valid, test
import env
import load_trace
RANDOM_SEED = 39
S_INFO = 6
S_LEN = 8
A_DIM = 6
LEARNING_RATE_ACTOR = 0.0003
LEARNING_RATE_CRITIC = 0.0003
# TRAIN_SEQ_LEN = 100 # take as a train batch
VIDEO_BIT_RATE = [300,750,1200,1850,2850,4300] # Kbps
BUFFER_NORM_FACTOR = 10.0
CHUNK_TIL_VIDEO_END_CAP = 48.0
M_IN_K = 1000.0
REBUF_PENALTY = 2.66 # 1 sec rebuffering -> 3 Mbps
SMOOTH_PENALTY = 1
DEFAULT_QUALITY = 1 # default video quality without agent
# RANDOM_SEED = 42
# GAMMA = 0.90
# ENTROPY_WEIGHT = 0.99
UPDATE_INTERVAL = 500
RAND_RANGE = 1000
ENTROPY_EPS = 1e-6
MIN_MOMERY = 100
LOG_FILE = './Results/sim/pppo/log'
# TEST_PATH = './models/A3C/BC/360_a3c_240000.model'
parser = argparse.ArgumentParser(description='priority_ppo')
parser.add_argument('--test', action='store_true', help='Evaluate only')
USE_CUDA = torch.cuda.is_available()
dtype = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor
dlongtype = torch.cuda.LongTensor if torch.cuda.is_available() else torch.LongTensor
class ReplayMemory(object):
def __init__(self, capacity):
self.capacity = capacity
self.memory = []
def push(self, events):
for event in zip(*events):
self.memory.append(event)
if len(self.memory) > self.capacity:
del self.memory[0]
def clear(self):
self.memory = []
def sample(self, batch_size):
samples = zip(*random.sample(self.memory, batch_size))
return map(lambda x: torch.cat(x, 0), samples)
# class ReplayMemory(object):
# def __init__(self):
# self.capacity = 0
# self.memory = []
# self.all_truples = []
# self.priority = {}
# def push(self, events):
# for event in zip(*events):
# action = event[1]
# reward = event[2]
# if (action, reward) not in self.all_truples:
# self.memory.append(event)
# self.capacity += 1
# self.priority[(action, reward)] = 1
# else:
# existing_num = self.priority[(action, reward)]
# if existing_num/self.capacity <= 0.5 or self.capacity <= MIN_MOMERY:
# self.memory.append(event)
# self.capacity += 1
# self.priority[(action, reward)] += 1
# # self.memory.append(event)
# # if len(self.memory) > self.capacity:
# # del self.memory[0]
# def clear(self):
# self.memory = []
# self.capacity = 0
# def get_capacity(self):
# return self.capacity
# def sample(self, batch_size):
# samples = zip(*random.sample(self.memory, batch_size))
# return map(lambda x: torch.cat(x, 0), samples)
def train():
logging.basicConfig(filename=LOG_FILE + '_central',
filemode='w',
level=logging.INFO)
with open(LOG_FILE + '_record', 'w') as log_file, open(LOG_FILE + '_test', 'w') as test_log_file:
# entropy_weight = ENTROPY_WEIGHT
# value_loss_coef = 0.5
torch.manual_seed(RANDOM_SEED)
all_cooked_time, all_cooked_bw, _ = load_trace.load_trace()
net_env = env.Environment(all_cooked_time=all_cooked_time,
all_cooked_bw=all_cooked_bw)
model_actor = Actor(A_DIM).type(dtype)
model_critic = Critic(A_DIM).type(dtype)
model_actor.train()
model_critic.train()
optimizer_actor = optim.Adam(model_actor.parameters(), lr=LEARNING_RATE_ACTOR)
optimizer_critic = optim.Adam(model_critic.parameters(), lr=LEARNING_RATE_CRITIC)
# max_grad_norm = MAX_GRAD_NORM
state = np.zeros((S_INFO,S_LEN))
state = torch.from_numpy(state)
last_bit_rate = DEFAULT_QUALITY
bit_rate = DEFAULT_QUALITY
# action_vec = np.zeros(A_DIM)
# action_vec[bit_rate] = 1
done = True
epoch = 0
time_stamp = 0
exploration_size = 12
episode_steps = 20
update_num = 3
batch_size = 64
gamma = 0.95
gae_param = 0.90
clip = 0.2
ent_coeff = 2.6
exploration_threhold = 0.05
memory = ReplayMemory(exploration_size * episode_steps)
# memory = ReplayMemory()
while True:
for explore in range(exploration_size):
states = []
actions = []
rewards_comparison = []
rewards = []
values = []
returns = []
advantages = []
for step in range(episode_steps):
prob = model_actor(state.unsqueeze(0).type(dtype)).detach()
action = prob.multinomial(num_samples=1)
# seed_ = np.random.uniform(0,1)
# if np.random.uniform(0,1) <= exploration_threhold:
# action = random.randint(0, 5)
# action = torch.tensor([[action]]).type(dlongtype)
# else:
# action = prob.multinomial(num_samples=1)
v = model_critic(state.unsqueeze(0).type(dtype)).detach().cpu()
values.append(v)
bit_rate = int(action.squeeze().cpu().numpy())
actions.append(torch.tensor([action]))
states.append(state.unsqueeze(0))
delay, sleep_time, buffer_size, rebuf, \
video_chunk_size, next_video_chunk_sizes, \
end_of_video, video_chunk_remain = \
net_env.get_video_chunk(bit_rate) ## sample in the environment of virtual player
time_stamp += delay # in ms
time_stamp += sleep_time # in ms
# reward is video quality - rebuffer penalty - smooth penalty
# -- lin scale reward --
# reward = VIDEO_BIT_RATE[bit_rate] / M_IN_K \
# - REBUF_PENALTY * rebuf \
# - SMOOTH_PENALTY * np.abs(VIDEO_BIT_RATE[bit_rate] -
# VIDEO_BIT_RATE[last_bit_rate]) / M_IN_K
# -- log scale reward --
log_bit_rate = np.log(VIDEO_BIT_RATE[bit_rate] / float(VIDEO_BIT_RATE[0]))
log_last_bit_rate = np.log(VIDEO_BIT_RATE[last_bit_rate] / float(VIDEO_BIT_RATE[0]))
reward = log_bit_rate \
- REBUF_PENALTY * rebuf \
- SMOOTH_PENALTY * np.abs(log_bit_rate - log_last_bit_rate)
reward_max = 2.67
reward = float(max(min(reward, reward_max), -reward_max) / reward_max)
rewards.append(reward)
rewards_comparison.append(torch.tensor([reward]))
last_bit_rate = bit_rate
# retrieve previous state
if end_of_video:
state = np.zeros((S_INFO, S_LEN))
state = torch.from_numpy(state)
last_bit_rate = DEFAULT_QUALITY
break
# dequeue history record
state = np.roll(state, -1, axis=1)
# this should be S_INFO number of terms
state[0, -1] = VIDEO_BIT_RATE[bit_rate] / float(np.max(VIDEO_BIT_RATE)) # last quality
state[1, -1] = buffer_size / BUFFER_NORM_FACTOR # 10 sec
state[2, -1] = float(video_chunk_size) / float(delay) / M_IN_K # kilo byte / ms
state[3, -1] = float(delay) / M_IN_K / BUFFER_NORM_FACTOR # 10 sec
state[4, :A_DIM] = np.array(next_video_chunk_sizes) / M_IN_K / M_IN_K # mega byte
state[5, -1] = np.minimum(video_chunk_remain, CHUNK_TIL_VIDEO_END_CAP) / float(CHUNK_TIL_VIDEO_END_CAP)
state = torch.from_numpy(state)
# log time_stamp, bit_rate, buffer_size, reward
log_file.write(str(time_stamp) + '\t' +
str(VIDEO_BIT_RATE[bit_rate]) + '\t' +
str(buffer_size) + '\t' +
str(rebuf) + '\t' +
str(video_chunk_size) + '\t' +
str(delay) + '\t' +
str(reward) + '\n')
log_file.flush()
# one last step
R = torch.zeros(1, 1)
if end_of_video == False:
v = model_critic(state.unsqueeze(0).type(dtype)).detach().cpu()
R = v.data
#================================结束一个ep========================================
# compute returns and GAE(lambda) advantages:
values.append(Variable(R))
R = Variable(R)
A = Variable(torch.zeros(1, 1))
for i in reversed(range(len(rewards))):
td = rewards[i] + gamma * values[i + 1].data[0, 0] - values[i].data[0, 0]
A = float(td) + gamma * gae_param * A
advantages.insert(0, A)
R = A + values[i]
returns.insert(0, R)
# store usefull info:
# memory.push([states[1:], actions[1:], rewards_comparison[1:], returns[1:], advantages[1:]])
memory.push([states[1:], actions[1:], returns[1:], advantages[1:]])
# policy grad updates:
model_actor_old = Actor(A_DIM).type(dtype)
model_critic_old = Critic(A_DIM).type(dtype)
model_actor_old.load_state_dict(model_actor.state_dict())
model_critic_old.load_state_dict(model_critic.state_dict())
## actor update
for update_step in range(update_num):
model_actor.zero_grad()
# model_critic.zero_grad()
# new mini_batch
# priority_batch_size = int(memory.get_capacity()/10)
# batch_states, batch_actions, _, batch_returns, batch_advantages = memory.sample(batch_size)
batch_states, batch_actions, batch_returns, batch_advantages = memory.sample(batch_size)
# old_prob
probs_old = model_actor_old(batch_states.type(dtype).detach())
# v_pre_old = model_critic_old(batch_states.type(dtype).detach())
prob_value_old = torch.gather(probs_old, dim=1, index=batch_actions.unsqueeze(1).type(dlongtype))
# new prob
probs = model_actor(batch_states.type(dtype))
# v_pre = model_critic(batch_states.type(dtype))
prob_value = torch.gather(probs, dim=1, index=batch_actions.unsqueeze(1).type(dlongtype))
# ratio
ratio = prob_value / (1e-5 + prob_value_old)
## non-clip loss
# surrogate_loss = ratio * batch_advantages.type(dtype)
# clip loss
surr1 = ratio * batch_advantages.type(dtype) # surrogate from conservative policy iteration
surr2 = ratio.clamp(1 - clip, 1 + clip) * batch_advantages.type(dtype)
loss_clip_actor = -torch.mean(torch.min(surr1, surr2))
# value loss
# vfloss1 = (v_pre - batch_returns.type(dtype)) ** 2
# v_pred_clipped = v_pre_old + (v_pre - v_pre_old).clamp(-clip, clip)
# vfloss2 = (v_pred_clipped - batch_returns.type(dtype)) ** 2
# loss_value = 0.5 * torch.mean(torch.max(vfloss1, vfloss2))
# entropy
loss_ent = ent_coeff * torch.mean(probs * torch.log(probs + 1e-5))
# total
policy_total_loss = (loss_clip_actor + loss_ent)
# print("hell0")
# copy the new model to old model?
# model_actor_old.load_state_dict(model_actor.state_dict())
# model_critic_old.load_state_dict(model_critic.state_dict())
# update
optimizer_actor.zero_grad()
# optimizer_critic.zero_grad()
policy_total_loss.backward(retain_graph=True)
# loss_clip_actor.backward(retain_graph=True)
# loss_value.backward(retain_graph=True)
optimizer_actor.step()
# optimizer_critic.step()
# print("hell0")
## critic_update
for update_step in range(update_num):
model_critic.zero_grad()
# new mini_batch
# # priority_batch_size = int(memory.get_capacity()/10)
# assert memory.get_capacity() > batch_size
batch_states, _, batch_returns, batch_advantages = memory.sample(batch_size)
# calculate loss
v_pre = model_critic(batch_states.type(dtype))
vf_loss = (v_pre - batch_returns.type(dtype)) ** 2 # V_\theta - Q'
loss_value = 0.5 * torch.mean(vf_loss)
optimizer_critic.zero_grad()
loss_value.backward(retain_graph=True)
optimizer_critic.step()
## test and save the model
epoch += 1
memory.clear()
logging.info('Epoch: ' + str(epoch) +
' Avg_policy_loss: ' + str(policy_total_loss.detach().cpu().numpy()) +
' Avg_value_loss: ' + str(loss_value.detach().cpu().numpy()) +
' Avg_entropy_loss: ' + str(loss_ent.detach().cpu().numpy()))
if epoch % UPDATE_INTERVAL == 0:
logging.info("Model saved in file")
valid(model_actor, epoch, test_log_file)
# entropy_weight = 0.95 * entropy_weight
ent_coeff = 0.95 * ent_coeff
# def main():
# train()
# if __name__ == '__main__':
# main()