-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathicvf_networks.py
159 lines (121 loc) · 5.4 KB
/
icvf_networks.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
from jaxrl_m.typing import *
from jaxrl_m.networks import MLP, get_latent, default_init, ensemblize
import flax.linen as nn
import jax.numpy as jnp
class LayerNormMLP(nn.Module):
hidden_dims: Sequence[int]
activations: Callable[[jnp.ndarray], jnp.ndarray] = nn.gelu
activate_final: bool = False
kernel_init: Callable[[PRNGKey, Shape, Dtype], Array] = default_init()
@nn.compact
def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
for i, size in enumerate(self.hidden_dims):
x = nn.Dense(size, kernel_init=self.kernel_init)(x)
if i + 1 < len(self.hidden_dims) or self.activate_final:
x = self.activations(x)
x = nn.LayerNorm()(x)
return x
class ICVFWithEncoder(nn.Module):
encoder: nn.Module
vf: nn.Module
def get_encoder_latent(self, observations: jnp.ndarray) -> jnp.ndarray:
return get_latent(self.encoder, observations)
def get_phi(self, observations: jnp.ndarray) -> jnp.ndarray:
latent = get_latent(self.encoder, observations)
return self.vf.get_phi(latent)
def __call__(self, observations, outcomes, intents):
latent_s = get_latent(self.encoder, observations)
latent_g = get_latent(self.encoder, outcomes)
latent_z = get_latent(self.encoder, intents)
return self.vf(latent_s, latent_g, latent_z)
def get_info(self, observations, outcomes, intents):
latent_s = get_latent(self.encoder, observations)
latent_g = get_latent(self.encoder, outcomes)
latent_z = get_latent(self.encoder, intents)
return self.vf.get_info(latent_s, latent_g, latent_z)
def create_icvf(icvf_cls_or_name, encoder=None, ensemble=True, **kwargs):
if isinstance(icvf_cls_or_name, str):
icvf_cls = icvfs[icvf_cls_or_name]
else:
icvf_cls = icvf_cls_or_name
if ensemble:
vf = ensemblize(icvf_cls, 2, methods=['__call__', 'get_info', 'get_phi'])(**kwargs)
else:
vf = icvf_cls(**kwargs)
if encoder is None:
return vf
return ICVFWithEncoder(encoder, vf)
##
#
# Actual ICVF definitions below
##
class ICVFTemplate(nn.Module):
def get_info(self, observations: jnp.ndarray, outcomes: jnp.ndarray, z: jnp.ndarray) -> Dict[str, jnp.ndarray]:
# Returns useful metrics
raise NotImplementedError
def get_phi(self, observations):
# Returns phi(s) for downstream use
raise NotImplementedError
def __call__(self, observations: jnp.ndarray, outcomes: jnp.ndarray, z: jnp.ndarray) -> jnp.ndarray:
# Returns V(s, g, z)
raise NotImplementedError
class MonolithicVF(nn.Module):
hidden_dims: Sequence[int]
use_layer_norm: bool = False
def setup(self):
network_cls = LayerNormMLP if self.use_layer_norm else MLP
self.net = network_cls((*self.hidden_dims, 1), activate_final=False)
def get_info(self, observations: jnp.ndarray, outcomes: jnp.ndarray, z: jnp.ndarray) -> Dict[str, jnp.ndarray]:
x = jnp.concatenate([observations, outcomes, z], axis=-1)
v = self.net(x)
return {
'v': jnp.squeeze(v, -1),
'psi': outcomes,
'z': z,
'phi': observations,
}
def get_phi(self, observations):
print('Warning: StandardVF does not define a state representation phi(s). Returning phi(s) = s')
return observations
def __call__(self, observations: jnp.ndarray, outcomes: jnp.ndarray, z: jnp.ndarray) -> jnp.ndarray:
x = jnp.concatenate([observations, outcomes, z], axis=-1)
v = self.net(x)
return jnp.squeeze(v, -1)
class MultilinearVF(nn.Module):
hidden_dims: Sequence[int]
use_layer_norm: bool = False
def setup(self):
network_cls = LayerNormMLP if self.use_layer_norm else MLP
self.phi_net = network_cls(self.hidden_dims, activate_final=True, name='phi')
self.psi_net = network_cls(self.hidden_dims, activate_final=True, name='psi')
self.T_net = network_cls(self.hidden_dims, activate_final=True, name='T')
self.matrix_a = nn.Dense(self.hidden_dims[-1], name='matrix_a')
self.matrix_b = nn.Dense(self.hidden_dims[-1], name='matrix_b')
def __call__(self, observations: jnp.ndarray, outcomes: jnp.ndarray, intents: jnp.ndarray) -> jnp.ndarray:
return self.get_info(observations, outcomes, intents)['v']
def get_phi(self, observations):
return self.phi_net(observations)
def get_info(self, observations: jnp.ndarray, outcomes: jnp.ndarray, intents: jnp.ndarray) -> Dict[str, jnp.ndarray]:
phi = self.phi_net(observations)
psi = self.psi_net(outcomes)
z = self.psi_net(intents)
Tz = self.T_net(z)
# T(z) should be a dxd matrix, but having a network output d^2 parameters is inefficient
# So we'll make a low-rank approximation to T(z) = (diag(Tz) * A * B * diag(Tz))
# where A and B are (fixed) dxd matrices and Tz is a d-dimensional parameter dependent on z
phi_z = self.matrix_a(Tz * phi)
psi_z = self.matrix_b(Tz * psi)
v = (phi_z * psi_z).sum(axis=-1)
return {
'v': v,
'phi': phi,
'psi': psi,
'Tz': Tz,
'z': z,
'phi_z': phi_z,
'psi_z': psi_z,
}
icvfs = {
'multilinear': MultilinearVF,
'monolithic': MonolithicVF,
}