forked from JellePiepenbrock/neurosmash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
81 lines (60 loc) · 2.3 KB
/
controller.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
import torch.nn as nn
import torch
from torch.distributions import Categorical
from torch.autograd import Variable
import numpy as np
import random
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class Controller(nn.Module):
def __init__(self, gamma):
super(Controller, self).__init__()
# 32 latent vars from VAE, 100 hidden state vars from LSTM
self.predictor = nn.Sequential(
nn.Linear((32+3*256), 256),
nn.ReLU(),
nn.Linear(256, 3)
)
self.gamma = gamma
self.policy_history = Variable(torch.Tensor()).cuda()
self.reward_episode = []
self.reward_history = []
self.loss_history = []
def forward(self, x):
x = self.predictor(x) / 1
return torch.softmax(x, dim=1)
def select_action(state, policy):
# Select an action (0 or 1) by running policy model and choosing based on the probabilities in state
# print(state)
preds = policy(state)
print(preds)
c = Categorical(preds)
action = c.sample()
# Add log probability of our chosen action to our history
if policy.policy_history.dim() != 0:
policy.policy_history = torch.cat([policy.policy_history.to(device), c.log_prob(action).to(device)])
else:
policy.policy_history = (c.log_prob(action).to(device))
return action
## TODO create replay buffer
def update_policy(policy, optimizer):
R = 0
rewards = []
# Reward discounting (i.e. smear reward back through history)
for r in policy.reward_episode[::-1]:
R = r + policy.gamma * R
rewards.insert(0, R)
# Scale rewards, just some autoscaling
rewards = torch.FloatTensor(rewards).to(device)
rewards = (rewards - rewards.mean()) / (rewards.std() + np.finfo(np.float32).eps).to(device)
# print(rewards)
# Calculate loss
loss = (torch.sum(torch.mul(policy.policy_history.to(device), Variable(rewards)).mul(-1).to(device), -1)).to(device)
# Update network weights
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Save and intialize episode history counters
policy.loss_history.append(loss.item())
policy.reward_history.append(np.sum(policy.reward_episode).item())
policy.policy_history = Variable(torch.Tensor())
policy.reward_episode = []