phanerozoic's picture
Rename from tiny-HalfAdder-verified
0fc2474 verified
"""
Threshold Network for Half Adder
Adds two 1-bit inputs, producing sum (XOR) and carry (AND) outputs.
Sum uses 2-layer XOR, Carry uses single AND neuron.
"""
import torch
from safetensors.torch import load_file
def heaviside(x):
return (x >= 0).float()
class ThresholdHalfAdder:
"""
Half adder: sum = a XOR b, carry = a AND b
"""
def __init__(self, weights_dict):
self.weights = weights_dict
def __call__(self, a, b):
inputs = torch.tensor([float(a), float(b)])
# Sum = XOR (2-layer)
or_out = heaviside((inputs * self.weights['sum.layer1.or.weight']).sum() +
self.weights['sum.layer1.or.bias'])
nand_out = heaviside((inputs * self.weights['sum.layer1.nand.weight']).sum() +
self.weights['sum.layer1.nand.bias'])
layer1 = torch.tensor([or_out, nand_out])
sum_out = heaviside((layer1 * self.weights['sum.layer2.weight']).sum() +
self.weights['sum.layer2.bias'])
# Carry = AND (single neuron)
carry_out = heaviside((inputs * self.weights['carry.weight']).sum() +
self.weights['carry.bias'])
return int(sum_out.item()), int(carry_out.item())
@classmethod
def from_safetensors(cls, path="model.safetensors"):
return cls(load_file(path))
if __name__ == "__main__":
model = ThresholdHalfAdder.from_safetensors("model.safetensors")
print("Half Adder Truth Table:")
print("-" * 30)
print("a | b | sum | carry")
print("-" * 30)
for a in [0, 1]:
for b in [0, 1]:
s, c = model(a, b)
expected_s = a ^ b
expected_c = a & b
status = "OK" if (s == expected_s and c == expected_c) else "FAIL"
print(f"{a} | {b} | {s} | {c} [{status}]")