Update binary tensor during training

I want to minimize 2 variables, and one is binary. It is not a probability vector for binary classification, but an input to my network who must only contain values 0 and 1. How can I achieve this? I am doing this, but I get the error RuntimeError: a leaf Variable that requires grad is being used in an in-place operation.. The binary variable is mask.

mask = torch.ones(32, 32)
mask.requires_grad = True
for epoch in range(epochs):
    for d, t in dataloader:
        optimizer.zero_grad()
        output = net(mask*d)
        loss = criterion(output, t)
        loss.backward()
        optimizer.step()
        mask.data[mask >= mask.float().mean()] = torch.Tensor([1])
        mask.data[mask < mask.float().mean()] = torch.Tensor([0])

I would like that before each forward pass, mask is binary (only 0 and 1 values). What is the best way to do this?

Thanks!

Hi Fabiola!

Two comments:

Don’t use .data. It is deprecated and can break things.

Wrap your modification of mask in a no_grad() block to eliminate the
leaf Variable error:

    with torch.no_grad():
        mask.data[mask >= mask.float().mean()] = torch.Tensor([1])
        mask.data[mask < mask.float().mean()] = torch.Tensor([0])
    

Also, it’s possible that you might get better training results if you permit mask
to run from 0.0 to 1.0 and then add a penalty or Lagrange-multiplier constraint
to your loss so that the optimization itself pushes mask to be either 0.0 or
1.0. For example, mask**2 + (mask - 1.0)**2 takes on its minimum of
0.0 exactly when mask is equal to either 0.0 or 1.0.

Best.

K. Frank