File size: 1,396 Bytes
c371536
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
---

# threshold-parity5

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

## Function

parity5(a, b, c, d, e) = a XOR b XOR c XOR d XOR e

## Architecture

Hybrid tree-cascade: parity5 = XOR(XOR(XOR(a,b), XOR(c,d)), e)

Four XOR2 gates:
- xor_ab: XOR(a, b) - parallel with xor_cd
- xor_cd: XOR(c, d) - parallel with xor_ab
- xor_abcd: XOR(xor_ab, xor_cd)
- xor_final: XOR(xor_abcd, e)

## Parameters

| | |
|---|---|
| Inputs | 5 |
| Outputs | 1 |
| Neurons | 12 |
| Layers | 6 |
| Parameters | 36 |
| Magnitude | 40 |

## 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 parity5(a, b, c, d, e):
    xor_ab = xor2(a, b, 'xor_ab')
    xor_cd = xor2(c, d, 'xor_cd')
    xor_abcd = xor2(xor_ab, xor_cd, 'xor_abcd')
    return xor2(xor_abcd, e, 'xor_final')

print(parity5(1, 0, 1, 0, 1))  # 1 (odd)
print(parity5(1, 1, 1, 1, 0))  # 0 (even)
```

## License

MIT