-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_actor.py
83 lines (64 loc) · 2.89 KB
/
train_actor.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
import os
import json
import utils
import torch
import numpy as np
from glob import glob
from tqdm import tqdm
from FARC import FARCtor, FARCritic
BS = 256
T = 2000
obs_dim = 150
hidden_size = 128
epochs = 100
# discount factor for true capacity reward
F_CONSERVE = 0.96
data_dir = "./Data/emulated_dataset/train/"
def train():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
actor = FARCtor(input_size=obs_dim, hidden_size=hidden_size).to(device)
critic = FARCritic(input_dim=obs_dim).to(device)
# load the final critic model
critic = utils.load_checkpoint(critic, "./model_checkpoints/critic_best.pth")
# optimizers
optimizer = torch.optim.Adam(actor.parameters(), lr=1e-3)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)
loss_fn = torch.nn.MSELoss()
trace_files = glob(os.path.join(data_dir, '*.json'), recursive=True)
for epoch in range(epochs):
print("Epoch {}/{}".format(epoch + 1, epochs))
actor.train()
critic.zero_grad()
for filename in tqdm(trace_files):
with open(filename, "r") as file:
call_data = json.load(file)
optimizer.zero_grad()
observations = np.asarray(call_data['observations'], dtype=np.float32)
true_capacities = np.asarray(call_data['true_capacity'], dtype=np.float32)
batched_observations = utils.prepare_batches(observations, BS)
batched_capacities = utils.prepare_batches(true_capacities, BS)
for batch_obs, batch_caps in zip(batched_observations, batched_capacities):
optimizer.zero_grad()
state = torch.tensor(batch_obs, dtype=torch.float32).reshape(-1, 1, obs_dim).to(device)
target = torch.tensor(batch_caps, dtype=torch.float32).unsqueeze(0).reshape(-1, 1).to(device)
h = torch.zeros((BS, hidden_size))
c = torch.zeros((BS, hidden_size))
action, h, c = actor(state, h, c)
bw_prediction = action[:, :, 0]
# gets the critic reward for the actor prediction
actor_reward = critic(state, bw_prediction)
# gets the discounted critic reward for the true capacity
max_reward = critic(state, target[:, :] * F_CONSERVE)
loss = loss_fn(actor_reward, max_reward)
if torch.isnan(loss):
continue
loss.backward()
optimizer.step()
scheduler.step()
if epoch % 10 == 0:
print("Saving model...")
utils.save_checkpoint(actor, optimizer, epoch,
filename="./model_checkpoints/actor_{}_{}.pth".format(actor.name, epoch))
utils.save_onnx_model(actor, actor.name + "_{}".format(epoch), BS, T, obs_dim, hidden_size)
if __name__ == '__main__':
train()