code: def test_bce_loss(device):
      from speechbrain.nnet.losses import bce_loss
  
      # Ensure this works both with and without singleton dimension
      predictions_singleton = torch.zeros(4, 10, 1, device=device)
      predictions_match = torch.zeros(4, 10, device=device)
      targets = torch.ones(4, 10, device=device)
      lengths = torch.ones(4, device=device)
      out_cost_singleton = bce_loss(
          predictions_singleton, targets, lengths
      )
      out_cost_match = bce_loss(predictions_match, targets, lengths)
      assert torch.allclose(
          torch.exp(out_cost_singleton),
          torch.tensor(2.0, device=device),
      )
      assert torch.allclose(
          torch.exp(out_cost_match), torch.tensor(2.0, device=device)
      )
  
      # How about one dimensional inputs
      predictions = torch.zeros(5, 1, device=device)
      targets = torch.ones(5, device=device)
      out_cost = bce_loss(predictions, targets)
      assert torch.allclose(
          torch.exp(out_cost), torch.tensor(2.0, device=device)
      )
  
      # Can't pass lengths in 1D case
      with pytest.raises(ValueError):
          bce_loss(
              predictions, targets, length=torch.ones(5, device=device)
          )
  
file_path: speechbrain/tests/unittests/test_losses.py
imports: import torch
  import pytest
label_desc: Use image node_id 4 for calling a global function with example usage: test_bce_loss(device) without return types