Skip to content

Commit 1a9d406

Browse files
committed
docs(networks): add docstrings to VAE reparameterize methods and tests
Signed-off-by: Lanre Shittu <136805224+Shizoqua@users.noreply.github.com>
1 parent c63785d commit 1a9d406

4 files changed

Lines changed: 33 additions & 5 deletions

File tree

monai/networks/nets/fullyconnectednet.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,18 @@ def decode_forward(self, z: torch.Tensor, use_sigmoid: bool = True) -> torch.Ten
172172
return x
173173

174174
def reparameterize(self, mu: torch.Tensor, logvar: torch.Tensor) -> torch.Tensor:
175+
"""Sample a latent code using the reparameterization trick.
176+
177+
At inference (eval mode) the posterior mean is returned directly. During
178+
training, returns ``mu + eps * std`` with ``eps ~ N(0, I)``.
179+
180+
Args:
181+
mu: Posterior mean, shape ``(batch, latent_size)``.
182+
logvar: Log-variance of the posterior, same shape as ``mu``.
183+
184+
Returns:
185+
Sampled latent code, same shape as ``mu``.
186+
"""
175187
if not self.training:
176188
# At inference the latent code is the posterior mean; the random
177189
# term is only added during training (the reparameterization trick).

monai/networks/nets/varautoencoder.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ def decode_forward(self, z: torch.Tensor, use_sigmoid: bool = True) -> torch.Ten
142142
return x
143143

144144
def reparameterize(self, mu: torch.Tensor, logvar: torch.Tensor) -> torch.Tensor:
145+
"""Sample a latent code using the reparameterization trick.
146+
147+
At inference (eval mode) the posterior mean is returned directly. During
148+
training, returns ``mu + eps * std`` with ``eps ~ N(0, I)``.
149+
150+
Args:
151+
mu: Posterior mean, shape ``(batch, latent_size)``.
152+
logvar: Log-variance of the posterior, same shape as ``mu``.
153+
154+
Returns:
155+
Sampled latent code, same shape as ``mu``.
156+
"""
145157
if not self.training:
146158
# At inference the latent code is the posterior mean; the random
147159
# term is only added during training (the reparameterization trick).

tests/networks/nets/test_fullyconnectednet.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ def test_vfc_shape(self, input_param, input_shape, expected_shape):
6565
self.assertEqual(result.shape, expected_shape)
6666

6767
def test_vfc_reparameterize_eval_returns_mu(self):
68-
# At eval the latent code must equal mu (deterministic); at train it must
69-
# be stochastic. Same #8413 reparameterize bug as VarAutoEncoder.
68+
"""A VFC latent code is deterministic at eval (equals mu) and stochastic at train.
69+
70+
Regression test for the #8413 reparameterize bug, which returned ``mu + std``
71+
at inference instead of ``mu``.
72+
"""
7073
net = VarFullyConnectedNet(
7174
in_channels=10, out_channels=10, latent_size=30, encode_channels=(15, 20, 25), decode_channels=(15, 20, 25)
7275
).to(device)

tests/networks/nets/test_varautoencoder.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,10 @@ def test_script(self):
123123
test_script_save(net, test_data, rtol=1e-3, atol=1e-3)
124124

125125
def test_reparameterize_eval_returns_mu(self):
126-
# At eval the latent code must equal mu (deterministic, no noise added);
127-
# at train it must be stochastic. Regression test for #8413, where eval
128-
# returned mu + std.
126+
"""A VarAutoEncoder latent code is deterministic at eval (equals mu) and stochastic at train.
127+
128+
Regression test for #8413, where eval returned ``mu + std`` instead of ``mu``.
129+
"""
129130
net = VarAutoEncoder(
130131
spatial_dims=2, in_shape=(1, 32, 32), out_channels=1, latent_size=4, channels=(4, 8), strides=(2, 2)
131132
).to(device)

0 commit comments

Comments
 (0)