-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
executable file
·135 lines (101 loc) · 4.29 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
import torch
import torch.nn as nn
from torch.nn import functional as F
import numpy as np
import matplotlib.pyplot as plt
class CNN_model(nn.Module):
def __init__(self, arg):
self.arg = arg
self.H, self.W = self.arg.frame_height, self.arg.frame_width
self.H = int(self.H * self.image_down_sample)
self.W = int(self.W * self.image_down_sample)
class Goal_Oriented_Semantic_Policy(NNBase):
def __init__(self, input_shape, recurrent=False, hidden_size=512,
num_sem_categories=16):
super(Goal_Oriented_Semantic_Policy, self).__init__(
recurrent, hidden_size, hidden_size)
out_size = int(input_shape[1] / 16.) * int(input_shape[2] / 16.)
self.main = nn.Sequential(
nn.MaxPool2d(2),
nn.Conv2d(num_sem_categories + 8, 32, 3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(64, 128, 3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(128, 64, 3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(64, 32, 3, stride=1, padding=1),
nn.ReLU(),
Flatten()
)
self.linear1 = nn.Linear(out_size * 32 + 8 * 2, hidden_size)
self.linear2 = nn.Linear(hidden_size, 256)
self.critic_linear = nn.Linear(256, 1)
self.orientation_emb = nn.Embedding(72, 8)
self.goal_emb = nn.Embedding(num_sem_categories, 8)
self.train()
def forward(self, inputs, rnn_hxs, masks, extras):
x = self.main(inputs)
orientation_emb = self.orientation_emb(extras[:, 0])
goal_emb = self.goal_emb(extras[:, 1])
x = torch.cat((x, orientation_emb, goal_emb), 1)
x = nn.ReLU()(self.linear1(x))
if self.is_recurrent:
x, rnn_hxs = self._forward_gru(x, rnn_hxs, masks)
x = nn.ReLU()(self.linear2(x))
return self.critic_linear(x).squeeze(-1), x, rnn_hxs
# https://github.com/ikostrikov/pytorch-a2c-ppo-acktr-gail/blob/master/a2c_ppo_acktr/model.py#L15
class RL_Policy(nn.Module):
def __init__(self, obs_shape, action_space, model_type=0,
base_kwargs=None):
super(RL_Policy, self).__init__()
if base_kwargs is None:
base_kwargs = {}
if model_type == 1:
self.network = Goal_Oriented_Semantic_Policy(
obs_shape, **base_kwargs)
else:
raise NotImplementedError
if action_space.__class__.__name__ == "Discrete":
num_outputs = action_space.n
self.dist = Categorical(self.network.output_size, num_outputs)
elif action_space.__class__.__name__ == "Box":
num_outputs = action_space.shape[0]
self.dist = DiagGaussian(self.network.output_size, num_outputs)
else:
raise NotImplementedError
self.model_type = model_type
@property
def is_recurrent(self):
return self.network.is_recurrent
@property
def rec_state_size(self):
"""Size of rnn_hx."""
return self.network.rec_state_size
def forward(self, inputs, rnn_hxs, masks, extras):
if extras is None:
return self.network(inputs, rnn_hxs, masks)
else:
return self.network(inputs, rnn_hxs, masks, extras)
def act(self, inputs, rnn_hxs, masks, extras=None, deterministic=False):
value, actor_features, rnn_hxs = self(inputs, rnn_hxs, masks, extras)
dist = self.dist(actor_features)
if deterministic:
action = dist.mode()
else:
action = dist.sample()
action_log_probs = dist.log_probs(action)
return value, action, action_log_probs, rnn_hxs
def get_value(self, inputs, rnn_hxs, masks, extras=None):
value, _, _ = self(inputs, rnn_hxs, masks, extras)
return value
def evaluate_actions(self, inputs, rnn_hxs, masks, action, extras=None):
value, actor_features, rnn_hxs = self(inputs, rnn_hxs, masks, extras)
dist = self.dist(actor_features)
action_log_probs = dist.log_probs(action)
dist_entropy = dist.entropy().mean()
return value, action_log_probs, dist_entropy, rnn_hxs