| | --- |
| | license: mit |
| | tags: |
| | - pytorch |
| | - safetensors |
| | - threshold-logic |
| | - neuromorphic |
| | --- |
| | |
| | # threshold-parity3 |
| |
|
| | 3-bit parity function. Outputs 1 if odd number of inputs are high. |
| |
|
| | ## Function |
| |
|
| | parity3(a, b, c) = a XOR b XOR c |
| |
|
| | ## Truth Table |
| |
|
| | | a | b | c | out | |
| | |---|---|---|-----| |
| | | 0 | 0 | 0 | 0 | |
| | | 0 | 0 | 1 | 1 | |
| | | 0 | 1 | 0 | 1 | |
| | | 0 | 1 | 1 | 0 | |
| | | 1 | 0 | 0 | 1 | |
| | | 1 | 0 | 1 | 0 | |
| | | 1 | 1 | 0 | 0 | |
| | | 1 | 1 | 1 | 1 | |
| |
|
| | ## Architecture |
| |
|
| | Cascade of two XOR2 gates: parity(a,b,c) = XOR(XOR(a,b), c) |
| |
|
| | Each XOR2 uses OR-NAND-AND structure: |
| | - OR: fires if either input is 1 |
| | - NAND: fires if not both inputs are 1 |
| | - AND: fires if both OR and NAND fire (XOR condition) |
| |
|
| | **Layers:** |
| | 1. xor1.or, xor1.nand (on inputs a, b) |
| | 2. xor1.and (combines layer 1) |
| | 3. xor2.or, xor2.nand (on xor1 output and c) |
| | 4. xor2.and (final output) |
| |
|
| | ## Parameters |
| |
|
| | | | | |
| | |---|---| |
| | | Inputs | 3 | |
| | | Outputs | 1 | |
| | | Neurons | 6 | |
| | | Layers | 4 | |
| | | Parameters | 18 | |
| | | Magnitude | 20 | |
| |
|
| | ## 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 parity3(a, b, c): |
| | return xor2(xor2(a, b, 'xor1'), c, 'xor2') |
| | |
| | print(parity3(1, 0, 1)) # 0 (even parity) |
| | print(parity3(1, 1, 1)) # 1 (odd parity) |
| | ``` |
| |
|
| | ## License |
| |
|
| | MIT |
| |
|