Skip to content

Commit

Permalink
Fix bug in CVAE example (#3325)
Browse files Browse the repository at this point in the history
The bug is caused by input validation (only accept value between 0 or 1 otherwise raise error) of F.binary_cross_entropy_loss after Pytorch 2.1 update. The fix is to manually mask the input of the loss function to avoid invalid value rather than removing the loss of invalid input from the final loss result.
Please refer to:#3273 for the initial report of the bug
  • Loading branch information
FDUguchunhui committed Feb 10, 2024
1 parent ca2f93c commit b696ccf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 6 additions & 2 deletions examples/cvae/baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ def __init__(self, masked_with=-1):

def forward(self, input, target):
target = target.view(input.shape)
loss = F.binary_cross_entropy(input, target, reduction="none")
loss[target == self.masked_with] = 0
# only calculate loss on target pixels (value = -1)
loss = F.binary_cross_entropy(
input[target != self.masked_with],
target[target != self.masked_with],
reduction="none",
)
return loss.sum()


Expand Down
5 changes: 1 addition & 4 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@
"vae/ss_vae_M2.py --num-epochs=1 --enum-discrete=sequential",
"vae/vae.py --num-epochs=1",
"vae/vae_comparison.py --num-epochs=1",
pytest.param(
"cvae/main.py --num-quadrant-inputs=1 --num-epochs=1",
marks=pytest.mark.skip(reason="https://github.com/pyro-ppl/pyro/issues/3273"),
),
"cvae/main.py --num-quadrant-inputs=1 --num-epochs=1",
"contrib/funsor/hmm.py --num-steps=1 --truncate=10 --model=0 ",
"contrib/funsor/hmm.py --num-steps=1 --truncate=10 --model=1 ",
"contrib/funsor/hmm.py --num-steps=1 --truncate=10 --model=2 ",
Expand Down

0 comments on commit b696ccf

Please sign in to comment.