-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
137 lines (109 loc) · 4.47 KB
/
model.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
import numpy as np
import torch
import torch.nn as nn
from config import cfg
class VAE(nn.Module):
def __init__(self):
super(VAE, self).__init__()
self.z_size = cfg.vae_z_size
# (N, 3, 64, 64) -> (N, 256, 2, 2)
self.encoder = nn.Sequential(
nn.Conv2d(3, 32, 4, 2),
nn.ReLU(True),
nn.Conv2d(32, 64, 4, 2),
nn.ReLU(True),
nn.Conv2d(64, 128, 4, 2),
nn.ReLU(True),
nn.Conv2d(128, 256, 4, 2),
nn.ReLU(True))
# (N, 256 * 4, 1, 1) -> (N, 3, 64, 64)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(256 * 4, 128, 5, 2),
nn.ReLU(True),
nn.ConvTranspose2d(128, 64, 5, 2),
nn.ReLU(True),
nn.ConvTranspose2d(64, 32, 6, 2),
nn.ReLU(True),
nn.ConvTranspose2d(32, 3, 6, 2),
nn.Sigmoid())
self.h = nn.Linear(self.z_size, 256 * 4)
self.mu = nn.Linear(256 * 4, self.z_size)
self.logvar = nn.Linear(256 * 4, self.z_size)
def forward(self, x):
x = self.encoder(x)
x = x.view(x.size(0), -1)
mu, logvar = self.mu(x), self.logvar(x)
z = mu + torch.exp(logvar / 2.0) * torch.randn_like(mu)
x = self.decode(z)
return mu, logvar, x, z
def decode(self, z):
x = self.h(z)
x = x.view(z.size(0), 256 * 4, 1, 1)
x = self.decoder(x)
return x
class RNNModel(nn.Module):
def __init__(self):
super(RNNModel, self).__init__()
self.action_embed = nn.Embedding(len(cfg.game_actions), cfg.action_embed_size)
# action_embeded + z_size -> rnn_size
self.rnn = nn.LSTMCell(cfg.vae_z_size + cfg.action_embed_size, cfg.rnn_size)
# z_size * (mix, mu, logvar) * num_mixtures + done
self.output = nn.Linear(cfg.rnn_size, cfg.vae_z_size * 3 * cfg.num_mixtures + 1)
def reset(self):
self.hx, self.cx = torch.zeros(1, cfg.rnn_size), torch.zeros(1, cfg.rnn_size)
def forward(self, z, actions, dones):
# z: (batch, seq, feat)
# actions: (batch, seq, 1) -> (batch, seq, embed_n)
# dones: (batch, seq) <- {0, 1}
actions = self.action_embed(actions.long())
dones = dones.float()
inp = torch.cat((z, actions), dim=2)
outputs = []
hx = torch.zeros(z.size(0), cfg.rnn_size).cuda()
cx = torch.zeros(z.size(0), cfg.rnn_size).cuda()
seq_length = inp.size(1)
for step in range(0, seq_length):
hx, cx = self.rnn(inp[:, step, :], (hx, cx))
outputs.append(hx.unsqueeze(1))
hx = hx * (1.0 - dones[:, step].unsqueeze(1))
cx = cx * (1.0 - dones[:, step].unsqueeze(1))
output = torch.cat(outputs, dim=1)
# (batch_size, seq_length, 961)
output = self.output(output[:, :-1, :])
logmix, mu, logstd, done_p = torch.split(output, cfg.num_mixtures * cfg.vae_z_size, 2)
logmix = logmix.contiguous().view(-1, cfg.num_mixtures)
mu = mu.contiguous().view(-1, cfg.num_mixtures)
logstd = logstd.contiguous().view(-1, cfg.num_mixtures)
return logmix, mu, logstd, done_p.squeeze()
def step(self, z, action):
action = self.action_embed(action.long())
inp = torch.cat((z, action), dim=2)
self.hx, self.cx = self.rnn(inp[:, 0, :], (self.hx, self.cx))
output = self.output(self.hx)
logmix, mu, logstd, done_p = torch.split(output, cfg.num_mixtures * cfg.vae_z_size, 1)
logmix = logmix.contiguous().view(-1, cfg.num_mixtures)
mu = mu.contiguous().view(-1, cfg.num_mixtures)
logstd = logstd.contiguous().view(-1, cfg.num_mixtures)
return logmix, mu, logstd, done_p.squeeze()
class Controller(nn.Module):
def __init__(self):
super(Controller, self).__init__()
self.fc = nn.Sequential(
# hx_size + cx_size + z_size
nn.Linear(cfg.rnn_size + cfg.rnn_size + cfg.vae_z_size, 16),
nn.ReLU(),
nn.Linear(16, 1, bias=False),
nn.Tanh()
)
def forward(self, x):
return self.fc(x)
'''
class Controller(nn.Module):
def __init__(self):
super(Controller, self).__init__()
self.fc = nn.Sequential(
nn.Linear(cfg.rnn_size + cfg.rnn_size + cfg.vae_z_size, 2),
)
def forward(self, x):
return self.fc(x)
'''