You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix VarAutoEncoder reparameterize returning mu+std at inference (#8413) (#8933)
### Description
Fixes#8413. `VarAutoEncoder.reparameterize` computed `std =
exp(0.5*logvar)`, added the random noise term only when training, and
then returned `std.add_(mu)`. At inference (`eval()`), `std` is still
the standard deviation, so the method returned `mu + std` instead of the
posterior mean `mu`. The reparameterization trick's stochastic term
should apply during training only; at inference the latent code should
be `mu`.
The fix returns `mu` directly when not training, and computes `mu + eps
* std` out-of-place otherwise (also avoids the in-place `add_`).
`VarFullyConnectedNet.reparameterize`
(`monai/networks/nets/fullyconnectednet.py`) contained the identical
bug, so it is fixed the same way in this PR.
### Types of changes
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [x] New tests added to cover the changes.
### Testing
Added regression tests asserting that in eval mode the returned latent
equals `mu` and is deterministic, while training stays stochastic, for
both `VarAutoEncoder` and `VarFullyConnectedNet`.
```
python -m unittest tests.networks.nets.test_varautoencoder tests.networks.nets.test_fullyconnectednet
# Ran 12 tests ... OK
```
Signed-off-by: Lanre Shittu <136805224+Shizoqua@users.noreply.github.com>
Co-authored-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
0 commit comments