File size: 1,930 Bytes
0fc2474 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
"""
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}]")
|