threshold-weighted / model.py
CharlesCNorton
Add weighted threshold circuit
0910cb4
import torch
from safetensors.torch import load_file
def load_model(path='model.safetensors'):
return load_file(path)
def weighted_threshold(x3, x2, x1, x0, weights):
"""Weighted threshold: 4*x3 + 3*x2 + 2*x1 + 1*x0 >= 6."""
inp = torch.tensor([float(x3), float(x2), float(x1), float(x0)])
y = int((inp @ weights['y.weight'].T + weights['y.bias'] >= 0).item())
return y
if __name__ == '__main__':
w = load_model()
print('Weighted Threshold (weights: 4,3,2,1, threshold: 6):')
for i in range(16):
x3, x2, x1, x0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
y = weighted_threshold(x3, x2, x1, x0, w)
ws = 4*x3 + 3*x2 + 2*x1 + 1*x0
print(f' {x3}{x2}{x1}{x0} (sum={ws:2d}) -> {y}')