-
Notifications
You must be signed in to change notification settings - Fork 161
/
test_network.py
165 lines (129 loc) · 4.98 KB
/
test_network.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import torch
from torch import nn
import torch.nn.functional as F
from rl_games.algos_torch.network_builder import NetworkBuilder
class TestNet(NetworkBuilder.BaseNetwork):
def __init__(self, params, **kwargs):
nn.Module.__init__(self)
actions_num = kwargs.pop('actions_num')
input_shape = kwargs.pop('input_shape')
num_inputs = 0
assert(type(input_shape) is dict)
for k,v in input_shape.items():
num_inputs +=v[0]
self.central_value = params.get('central_value', False)
self.value_size = kwargs.pop('value_size', 1)
self.linear1 = nn.Linear(num_inputs, 256)
self.linear2 = nn.Linear(256, 128)
self.linear3 = nn.Linear(128, 64)
self.mean_linear = nn.Linear(64, actions_num)
self.value_linear = nn.Linear(64, 1)
def is_rnn(self):
return False
def forward(self, obs):
obs = obs['obs']
obs = torch.cat([obs['pos'], obs['info']], axis=-1)
x = F.relu(self.linear1(obs))
x = F.relu(self.linear2(x))
x = F.relu(self.linear3(x))
action = self.mean_linear(x)
value = self.value_linear(x)
if self.central_value:
return value, None
return action, value, None
class TestNetBuilder(NetworkBuilder):
def __init__(self, **kwargs):
NetworkBuilder.__init__(self)
def load(self, params):
self.params = params
def build(self, name, **kwargs):
return TestNet(self.params, **kwargs)
def __call__(self, name, **kwargs):
return self.build(name, **kwargs)
class TestNetWithAuxLoss(NetworkBuilder.BaseNetwork):
def __init__(self, params, **kwargs):
nn.Module.__init__(self)
actions_num = kwargs.pop('actions_num')
input_shape = kwargs.pop('input_shape')
num_inputs = 0
self.target_key = 'aux_target'
assert(type(input_shape) is dict)
for k,v in input_shape.items():
if self.target_key == k:
self.target_shape = v[0]
else:
num_inputs +=v[0]
self.central_value = params.get('central_value', False)
self.value_size = kwargs.pop('value_size', 1)
self.linear1 = nn.Linear(num_inputs, 256)
self.linear2 = nn.Linear(256, 128)
self.linear3 = nn.Linear(128, 64)
self.mean_linear = nn.Linear(64, actions_num)
self.value_linear = nn.Linear(64, 1)
self.aux_loss_linear = nn.Linear(64, self.target_shape)
self.aux_loss_map = {
'aux_dist_loss' : None
}
def is_rnn(self):
return False
def get_aux_loss(self):
return self.aux_loss_map
def forward(self, obs):
obs = obs['obs']
target_obs = obs[self.target_key]
obs = torch.cat([obs['pos'], obs['info']], axis=-1)
x = F.relu(self.linear1(obs))
x = F.relu(self.linear2(x))
x = F.relu(self.linear3(x))
action = self.mean_linear(x)
value = self.value_linear(x)
y = self.aux_loss_linear(x)
self.aux_loss_map['aux_dist_loss'] = torch.nn.functional.mse_loss(y, target_obs)
if self.central_value:
return value, None
return action, value, None
class TestNetAuxLossBuilder(NetworkBuilder):
def __init__(self, **kwargs):
NetworkBuilder.__init__(self)
def load(self, params):
self.params = params
def build(self, name, **kwargs):
return TestNetWithAuxLoss(self.params, **kwargs)
def __call__(self, name, **kwargs):
return self.build(name, **kwargs)
class SimpleNet(NetworkBuilder.BaseNetwork):
def __init__(self, params, **kwargs):
nn.Module.__init__(self)
actions_num = kwargs.pop('actions_num')
input_shape = kwargs.pop('input_shape')
num_inputs =input_shape[0]
self.actions_num = actions_num
self.central_value = params.get('central_value', False)
self.value_size = kwargs.pop('value_size', 1)
self.linear = torch.nn.Sequential(
nn.Linear(num_inputs, 512),
nn.SiLU(),
nn.Linear(512, 256),
nn.SiLU(),
nn.Linear(256, 128),
nn.SiLU(),
nn.Linear(128, actions_num + 1),
)
self.sigma = nn.Parameter(torch.zeros(actions_num, requires_grad=True, dtype=torch.float32), requires_grad=True)
def is_rnn(self):
return False
@torch.compile
def forward(self, obs):
obs = obs['obs']
x = self.linear(obs)
mu, value = torch.split(x, [self.actions_num, 1], dim=-1)
return mu, self.sigma.unsqueeze(0).expand(mu.size()[0], self.actions_num), value, None
class SimpleNetBuilder(NetworkBuilder):
def __init__(self, **kwargs):
NetworkBuilder.__init__(self)
def load(self, params):
self.params = params
def build(self, name, **kwargs):
return SimpleNet(self.params, **kwargs)
def __call__(self, name, **kwargs):
return self.build(name, **kwargs)