Repository: pyro-ppl/pyro

Issue:
Bug in `CVAE` example
There is a bug in `CVAE` example. The `target` input to the `MaskedBCELoss` is not binary (it has values of -1, 0, 1). This was discovered by the PyTorch 2.1 update which started to validate the inputs of `F.binary_cross_entropy_loss`.

> FAILED tests/test_examples.py::test_cpu[cvae/main.py --num-quadrant-inputs=1 --num-epochs=1] - subprocess.CalledProcessError: Command '['/opt/hostedtoolcache/Python/3.8.18/x64/bin/python', '/home/runner/work/pyro/pyro/examples/cvae/main.py', '--num-quadrant-inputs=1', '--num-epochs=1']' returned non-zero exit status 1.
= 1 failed, 148 passed, 97 skipped, 26558 deselected, 2 warnings in 1948.89s (0:32:28) =


Discussion:
In baseline.py:

Change code 
```
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
        return loss.sum()
```

to

```
def forward(self, input, target):
        target = target.view(input.shape)
        # 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')
        # loss[target == self.masked_with] = 0
        return loss.sum()
```

After that, I have successfully ran the code in python==2.2.0 and pyro=1.8.6
@FDUguchunhui thank you! Could you submit a PR with the fix and remove the `pytest.mark.skip` in the test please?

https://github.com/pyro-ppl/pyro/blob/dev/tests/test_examples.py#L126-L129
Hi @FDUguchunhui . We would like to have this fixed before the 1.9 release next week. If you can't get to it in the next couple of days I would be happy to fix it myself. Let me know.
Hi Yerdos:

Sorry for the late reply. I will submit a pull request this weekend.

Thank you.

Best regards,
Chunhui

On Fri, Feb 9, 2024 at 09:54 Yerdos Ordabayev ***@***.***>
wrote:

> Hi @FDUguchunhui <https://github.com/FDUguchunhui> . We would like to
> have this fixed before the 1.9 release next week. If you can't get to it in
> the next couple of days I would be happy to fix it myself. Let me know.
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/pyro-ppl/pyro/issues/3273#issuecomment-1936174105>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AED3Y7CWALZI5JRGO4PM5J3YSZBE7AVCNFSM6AAAAAA5SY7PIOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZWGE3TIMJQGU>
> .
> You are receiving this because you were mentioned.Message ID:
> ***@***.***>
>


Resolving patch:
diff --git a/examples/cvae/baseline.py b/examples/cvae/baseline.py
--- a/examples/cvae/baseline.py
+++ b/examples/cvae/baseline.py
@@ -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()
 
 


Test patch:
diff --git a/tests/test_examples.py b/tests/test_examples.py
--- a/tests/test_examples.py
+++ b/tests/test_examples.py
@@ -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 ",