-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trainer.py
136 lines (122 loc) · 5.27 KB
/
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
from collections import namedtuple
import numpy as np
from itertools import count
import torch
import torch.nn.functional as f
import torch.optim as opt
import matplotlib.pyplot as plt
from torch.distributions import Categorical
from AModel import AModel
from agents.ActorCriticModule import ActorCriticModule
from enums.Behavior import Behavior
from enums.Mode import Mode
from Environment import Environment
from utils.Misc import get_path
class Trainer(AModel):
def __init__(self, hidden_size, behavior=Behavior.TEACH, mode=Mode.DEMO, device="cpu"):
super(Trainer, self).__init__()
self.env = Environment(behavior=behavior, mode=mode)
self.action_num = self.env.action_space
self.device = None
if device != "cpu":
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
else:
self.device = torch.device(device)
self.model = ActorCriticModule(self.env.observation_space, hidden_size, self.action_num, self.device)\
.to(self.device)
self.optimizer = opt.Adam(self.model.parameters(), lr=3e-2)
self.eps = np.finfo(np.float32).eps.item()
self.retrain_counter = 0
def finish_episode(self, gamma):
current_reward = 0
saved_actions = self.model.saved_actions
policy_losses = []
value_losses = []
returns = []
for r in self.model.rewards[::-1]:
current_reward = r + gamma * current_reward
returns.insert(0, current_reward)
returns = torch.tensor(returns)
returns = (returns - returns.mean()) / (returns.std() + self.eps)
for (log_prob, value), current_reward in zip(saved_actions, returns):
advantage = current_reward - value.item()
policy_losses.append(-log_prob * advantage)
torch_current_reward = torch.tensor([current_reward]).to(self.device)
value_losses.append(f.smooth_l1_loss(value, torch_current_reward))
self.optimizer.zero_grad()
loss = torch.stack(policy_losses).sum() + torch.stack(value_losses).sum()
loss.backward()
self.optimizer.step()
del self.model.rewards[:]
del self.model.saved_actions[:]
def test(self, epochs, log_interval):
rewards = []
state = self.env.reset()
for i in range(1, epochs):
action = self.select_action(state)
state, reward, done, protocol = self.env.step(action)
self.model.set_protocol(protocol)
rewards.append(reward)
if i % log_interval == 0:
print('Episode {}\tLast reward: {:.2f}\tSum reward: {:.2f}'
.format(i, reward, sum(rewards)))
def retrain(self, note, gamma, suspicion):
print(f"Retrain in suspicion level: {suspicion}")
running_rewards = []
running_reward = 10
ep_reward = 0
action = self.select_action(note.get_raw_bytes())
state, reward, done, protocol = self.env.step(action)
self.model.rewards.append(reward)
self.model.set_protocol(protocol)
ep_reward += reward
running_reward = 0.1 * ep_reward + 0.9 * running_reward
running_rewards.append(running_reward)
self.finish_episode(gamma)
self.retrain_counter += 1
if self.retrain_counter == 10000:
self.save()
self.retrain_counter = 0
def save(self):
path = get_path()
torch.save(self.model.state_dict(), path)
print(f"Saving model after training: {path}")
def train(self, epoch_size, gamma, log_interval, train_episode):
running_rewards = []
running_reward = 10
for i_episode in count(1):
state, ep_reward = self.env.reset(), 0
for _ in range(epoch_size):
action = self.select_action(state)
state, reward, done, protocol = self.env.step(action)
self.model.rewards.append(reward)
self.model.set_protocol(protocol)
ep_reward += reward
if done:
break
running_reward = 0.1 * ep_reward + 0.9 * running_reward
running_rewards.append(running_reward)
self.finish_episode(gamma)
if i_episode % log_interval == 0:
print('Episode {}\tLast reward: {:.2f}\tAverage reward: {:.2f}'
.format(i_episode, ep_reward, running_reward))
if i_episode == train_episode:
plt.plot(running_rewards)
plt.xlabel('Episode')
plt.ylabel('Reward')
plt.show()
self.save()
break
def select_action(self, state):
state = torch.from_numpy(state).float()
probabilities, state_value = self.model(state)
categorical = Categorical(probabilities)
action = categorical.sample()
self.save_action(action, categorical, state_value)
answer = action.item()
if answer >= self.action_num:
answer = 0
return answer
def save_action(self, action, categorical, state_value):
action_serializer = namedtuple('action_serializer', ['log_prob', 'value'])
self.model.saved_actions.append(action_serializer(categorical.log_prob(action), state_value))