Felix-Petersen/difflogic

Convert float input to bool in C

Closed this issue · 4 comments

I exported the C code for a model I trained on a float dataset. The generated function signature is

void apply_logic_gate_net (bool const *inp, int *out, size_t len)

How do I convert my float input to a boolean one to call this function?

If you want to call it from Python like in the example, you can use, e.g., a threshold and use a > 0.5 for some tensor a.

If you want to call it from C, you could equivalently also populate a bool array with boolean values derived from comparison to some threshold.

Is the a > 0.5 the default logic in the library? I mean, when I trained on a float dataset, did the library transformed it this way?

During inference, the inputs are always rounded (which is equivalent to a>0.5 in this case). See

(model(x.to('cuda').round()).argmax(-1) == y.to('cuda')).to(torch.float32).mean().item()

During training with MNIST, we found that it is slightly better to forward the real values through the network.

For other datasets, such as CIFAR, the transform of the data loader uses a range of 3 or 31 thresholds to better represent the respective data. See

transform = {

Depending on whether you use 1 or more thresholds (e.g., 1 threshold for MNIST, or 3/31 thresholds for CIFAR depending on model size), you can either use a>0.5 or apply the respective transformation such as

transform = {
    'cifar-10-3-thresholds': lambda x: torch.cat([(x > (i + 1) / 4).float() for i in range(3)], dim=0),
    'cifar-10-31-thresholds': lambda x: torch.cat([(x > (i + 1) / 32).float() for i in range(31)], dim=0),
}[args.dataset]

either in Python or similarly in C-code.

Don't hesitate to ask if you have more questions.