target = torch.zeros(8, 8) pos_indices = torch.tensor([ (0, 0), (0, 2), (0, 4), (1, 4), (1, 6), (1, 1), (2, 3), (3, 7), (4, 3), (7, 6), ]) # Add indexes of the principal diagonal as positive indexes. # This will be useful since we will use the BCELoss in PyTorch, # which will expect a value for the elements on the principal # diagonal as well. pos_indices = torch.cat([pos_indices, torch.arange(8).reshape(8, 1).expand(-1, 2)], dim=0) # Set the values in the target vector to 1. target[pos_indices[:,0], pos_indices[:,1]] = 1