CharlesCNorton
At most 2 of 3 threshold circuit (NAND3), magnitude 5
69d9906
import torch
from safetensors.torch import load_file
def load_model(path='model.safetensors'):
return load_file(path)
def atmost2of3(a, b, c, weights):
"""Returns 1 if at most 2 of 3 inputs are high (NAND)"""
inp = torch.tensor([float(a), float(b), float(c)])
return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
if __name__ == '__main__':
w = load_model()
print('atmost2outof3 (NAND3) truth table:')
for i in range(8):
a, b, c = (i >> 2) & 1, (i >> 1) & 1, i & 1
print(f' {a}{b}{c} -> {atmost2of3(a, b, c, w)}')