-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencoder.py
195 lines (155 loc) · 7.52 KB
/
encoder.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import torch.types
from models.mlp_network import MLPNetwork
class EncoderBase(torch.nn.Module):
def __init__(self):
super(EncoderBase, self).__init__()
self.device = torch.device('cpu')
class EncoderEmpty(EncoderBase):
def __init__(self):
super(EncoderEmpty, self).__init__()
def parameters(self, recursive=True):
return []
def encode_state(self, state):
return state
def encode(self, state, action):
return torch.cat((state, action), dim=-1)
def to(self, device):
return
def save(self, path, version):
return
def load(self, path, version, **kwargs):
return
def state_dict(self, destination=None, prefix='', keep_vars=False):
return dict()
def load_state_dict(self, state_dict,
strict: bool = True):
return
class Encoder(EncoderBase):
def __init__(self, obs_dim, act_dim,
state_embedding_size, action_embedding_size,
state_embedding_hidden_size,
state_embedding_activations, action_embedding_hidden_size,
action_embedding_activations, name_suffix=''):
super(Encoder, self).__init__()
self.name_suffix = name_suffix
self.state_encoder = MLPNetwork(obs_dim, state_embedding_size, state_embedding_hidden_size,
state_embedding_activations, 'state_encoder'+name_suffix)
self.action_embedding_size = action_embedding_size
self.action_encoder = MLPNetwork(act_dim, action_embedding_size, action_embedding_hidden_size,
action_embedding_activations, 'action_encoder'+name_suffix)
def parameters(self, recursive=True):
return [*self.state_encoder.parameters(True)] + [*self.action_encoder.parameters(True)]
def split_encode(self, state, action):
return self.state_encoder.meta_forward(state), self.action_encoder.meta_forward(action)
def encode(self, state, action):
action_mask = 1.0 if self.action_embedding_size > 1 else 0.0
return torch.cat((self.state_encoder.meta_forward(state), self.action_encoder.meta_forward(action) * action_mask), dim=-1)
def to(self, device):
if not self.device == device:
self.device = device
self.state_encoder.to(device)
self.action_encoder.to(device)
def save(self, path, version):
self.state_encoder.save(path, version)
self.action_encoder.save(path, version)
def load(self, path, version, **kwargs):
self.state_encoder.load(path, version, **kwargs)
self.action_encoder.load(path, version, **kwargs)
def state_dict(self, destination=None, prefix='', keep_vars=False):
return dict(
state_encoder=self.state_encoder.state_dict(destination, prefix, keep_vars),
action_encoder=self.action_encoder.state_dict(destination, prefix, keep_vars)
)
def load_state_dict(self, state_dict,
strict: bool = True):
if 'state_encoder' in state_dict:
self.state_encoder.load_state_dict(state_dict['state_encoder'], strict)
else:
print(f'state encoder is not founded in the state dict')
if 'action_encoder' in state_dict:
self.action_encoder.load_state_dict(state_dict['action_encoder'], strict)
else:
print(f'action encoder is not founded in the state dict')
class EncoderVanilla(EncoderBase):
def __init__(self, obs_dim, act_dim, embedding_size, hidden_size, hidden_activations, name_suffix=''):
super(EncoderVanilla, self).__init__()
self.encoder = MLPNetwork(obs_dim + act_dim, embedding_size, hidden_size,
hidden_activations, 'vanilla_encoder' + name_suffix)
def parameters(self, recursive=True):
return [*self.encoder.parameters(True)]
def encode(self, state, action):
return self.encoder.meta_forward(torch.cat((state, action), dim=-1))
def to(self, device):
if not self.device == device:
self.device = device
self.encoder.to(device)
def save(self, path, version):
self.encoder.save(path, version)
def load(self, path, version, **kwargs):
self.encoder.load(path, version, **kwargs)
def state_dict(self, destination=None, prefix='', keep_vars=False):
return dict(
vanilla_encoder=self.encoder.state_dict(destination, prefix, keep_vars),
)
def load_state_dict(self, state_dict,
strict: bool = True):
if 'vanilla_encoder' in state_dict:
self.encoder.load_state_dict(state_dict['vanilla_encoder'], strict)
else:
print(f'vanilla encoder is not founded in the state dict')
class EncoderVDB(EncoderBase):
def __init__(self, obs_dim, act_dim, embedding_size, hidden_size, hidden_activations, name_suffix=''):
super(EncoderVDB, self).__init__()
self.fc = MLPNetwork(obs_dim + act_dim, hidden_size[-1], hidden_size[:-1],
hidden_activations[:-1], 'VDB_encoder_fc' + name_suffix)
self.mu = MLPNetwork(hidden_size[-1], embedding_size, [],
hidden_activations[-1:], 'VDB_encoder_mu' + name_suffix)
self.logvar = MLPNetwork(hidden_size[-1], embedding_size, [],
hidden_activations[-1:], 'VDB_encoder_logvar' + name_suffix)
def parameters(self, recursive=True):
return [*self.fc.parameters(True)] + [*self.mu.parameters(True)] + [*self.logvar.parameters(True)]
def encode_z(self, state, action):
x = self.fc.meta_forward(torch.cat((state, action), dim=-1))
mu = self.mu.meta_forward(x)
logvar = self.logvar.meta_forward(x)
std = torch.exp(logvar/2)
eps = torch.randn_like(std)
return mu + std * eps, mu, logvar
def encode(self, state, action):
x = self.fc.meta_forward(torch.cat((state, action), dim=-1))
mu = self.mu.meta_forward(x)
return mu
def to(self, device):
if not self.device == device:
self.device = device
self.fc.to(device)
self.mu.to(device)
self.logvar.to(device)
def save(self, path, version):
self.fc.save(path, version)
self.mu.save(path, version)
self.logvar.save(path, version)
def load(self, path, version, **kwargs):
self.fc.load(path, version, **kwargs)
self.mu.load(path, version, **kwargs)
self.logvar.load(path, version, **kwargs)
def state_dict(self, destination=None, prefix='', keep_vars=False):
return dict(
encoder_fc=self.fc.state_dict(destination, prefix, keep_vars),
encoder_mu=self.mu.state_dict(destination, prefix, keep_vars),
encoder_logvar=self.logvar.state_dict(destination, prefix, keep_vars)
)
def load_state_dict(self, state_dict,
strict: bool = True):
if 'encoder_fc' in state_dict:
self.fc.load_state_dict(state_dict['encoder_fc'], strict)
else:
print(f'encoder fc is not founded in the state dict')
if 'encoder_mu' in state_dict:
self.mu.load_state_dict(state_dict['encoder_mu'], strict)
else:
print(f'encoder mu is not founded in the state dict')
if 'encoder_logvar' in state_dict:
self.logvar.load_state_dict(state_dict['encoder_logvar'], strict)
else:
print(f'encoder logvar is not founded in the state dict')