CoolFace
Modelpublic

phanerozoic/threshold-parity4

sourceHugging Facemitupdated 8mo agoView on Hugging Face
0likes17downloads
Model Card

threshold-parity4

4-bit parity function. Outputs 1 if odd number of inputs are high.

Function

parity4(a, b, c, d) = a XOR b XOR c XOR d

Architecture

Tree structure: parity(a,b,c,d) = XOR(XOR(a,b), XOR(c,d))

Three XOR2 gates, each using OR-NAND-AND structure:

  • xorab: XOR(a, b) - parallel with xorcd
  • xorcd: XOR(c, d) - parallel with xorab
  • xorfinal: XOR(xorab, xor_cd)

Layer structure:

  1. 1.or1, nand1 (a,b) and or2, nand2 (c,d) in parallel
  2. 2.and1 (xorab) and and2 (xorcd)
  3. 3.or3, nand3 (on xor outputs)
  4. 4.and3 (final output)

Parameters

Inputs4
Outputs1
Neurons9
Layers4
Parameters27
Magnitude30

Usage

python
from safetensors.torch import load_file

w = load_file('model.safetensors')

def xor2(a, b, prefix):
    or_out = int(a * w[f'{prefix}.or.weight'][0] + b * w[f'{prefix}.or.weight'][1] + w[f'{prefix}.or.bias'] >= 0)
    nand_out = int(a * w[f'{prefix}.nand.weight'][0] + b * w[f'{prefix}.nand.weight'][1] + w[f'{prefix}.nand.bias'] >= 0)
    return int(or_out * w[f'{prefix}.and.weight'][0] + nand_out * w[f'{prefix}.and.weight'][1] + w[f'{prefix}.and.bias'] >= 0)

def parity4(a, b, c, d):
    return xor2(xor2(a, b, 'xor_ab'), xor2(c, d, 'xor_cd'), 'xor_final')

print(parity4(1, 0, 1, 0))  # 0 (even)
print(parity4(1, 1, 1, 0))  # 1 (odd)

License

MIT