| | --- |
| | license: mit |
| | tags: |
| | - pytorch |
| | - safetensors |
| | - threshold-logic |
| | - neuromorphic |
| | --- |
| | |
| | # 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: |
| | - xor_ab: XOR(a, b) - parallel with xor_cd |
| | - xor_cd: XOR(c, d) - parallel with xor_ab |
| | - xor_final: XOR(xor_ab, xor_cd) |
| | |
| | **Layer structure:** |
| | 1. or1, nand1 (a,b) and or2, nand2 (c,d) in parallel |
| | 2. and1 (xor_ab) and and2 (xor_cd) |
| | 3. or3, nand3 (on xor outputs) |
| | 4. and3 (final output) |
| | |
| | ## Parameters |
| | |
| | | | | |
| | |---|---| |
| | | Inputs | 4 | |
| | | Outputs | 1 | |
| | | Neurons | 9 | |
| | | Layers | 4 | |
| | | Parameters | 27 | |
| | | Magnitude | 30 | |
| | |
| | ## 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 |
| | |