CharlesCNorton
Exactly 2 of 4 threshold circuit, magnitude 16
eb0c29e
import torch
from safetensors.torch import load_file
def load_model(path='model.safetensors'):
return load_file(path)
def exactly2of4(a, b, c, d, weights):
inp = torch.tensor([float(a), float(b), float(c), float(d)])
l1 = (inp @ weights['layer1.weight'].T + weights['layer1.bias'] >= 0).float()
out = (l1 @ weights['layer2.weight'].T + weights['layer2.bias'] >= 0).float()
return int(out.item())
if __name__ == '__main__':
w = load_model()
print('exactly2outof4 truth table:')
for i in range(16):
a, b, c, d = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
s = a + b + c + d
print(f' {a}{b}{c}{d} (sum={s}) -> {exactly2of4(a, b, c, d, w)}')