CharlesCNorton
Exactly 2 of 3 threshold circuit, magnitude 14
e21df7e
import torch
from safetensors.torch import load_file
def load_model(path='model.safetensors'):
return load_file(path)
def exactly2of3(a, b, c, weights):
"""Returns 1 if exactly 2 of 3 inputs are high"""
inp = torch.tensor([float(a), float(b), float(c)])
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('exactly2outof3 truth table:')
for i in range(8):
a, b, c = (i >> 2) & 1, (i >> 1) & 1, i & 1
print(f' {a}{b}{c} -> {exactly2of3(a, b, c, w)}')