File size: 3,449 Bytes
10710cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
---

license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- decoder
---


# threshold-3to8decoder

3-to-8 binary decoder. Converts 3-bit binary input to one-hot 8-bit output. A single-layer threshold circuit.

## Circuit

```

       aβ‚‚      a₁      aβ‚€

        β”‚       β”‚       β”‚

        β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€

        β”‚       β”‚       β”‚

    β”Œβ”€β”€β”€β”΄β”€β”€β”€β”¬β”€β”€β”€β”΄β”€β”€β”€β”¬β”€β”€β”€β”΄β”€β”€β”€β”

    β”‚       β”‚       β”‚       β”‚

    β–Ό       β–Ό       β–Ό       β–Ό

β”Œβ”€β”€β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”€β”€β”

β”‚  yβ‚€  β”‚β”‚  y₁  β”‚β”‚  yβ‚‚  β”‚β”‚ ...  β”‚

β”‚-1,-1,-1β”‚β”‚-1,-1,+1β”‚β”‚-1,+1,-1β”‚β”‚      β”‚

β”‚b:  0 β”‚β”‚b: -1 β”‚β”‚b: -1 β”‚β”‚      β”‚

β””β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”˜

    β”‚       β”‚       β”‚       β”‚

    β–Ό       β–Ό       β–Ό       β–Ό

   yβ‚€      y₁      yβ‚‚  ... y₇

```

## One-Hot Encoding

Each input value activates exactly one output:

| Input | aβ‚‚a₁aβ‚€ | Output yβ‚€y₁yβ‚‚y₃yβ‚„yβ‚…y₆y₇ |
|-------|--------|--------------------------|
| 0 | 000 | 10000000 |
| 1 | 001 | 01000000 |
| 2 | 010 | 00100000 |
| 3 | 011 | 00010000 |
| 4 | 100 | 00001000 |
| 5 | 101 | 00000100 |
| 6 | 110 | 00000010 |
| 7 | 111 | 00000001 |

## Mechanism

Each output yα΅’ acts as a "pattern matcher" for input = i:

- **Weight +1** for bit positions that should be 1
- **Weight -1** for bit positions that should be 0
- **Bias** = -(number of 1 bits in i)

Example for yβ‚… (binary 101):
```

weights: [+1, -1, +1]  (match 1, reject 0, match 1)

bias: -2              (need 2 matches to fire)

```

When input = 101: sum = 1Β·1 + (-1)Β·0 + 1Β·1 = 2, fires
When input = 111: sum = 1Β·1 + (-1)Β·1 + 1Β·1 = 1, doesn't fire

## The Matching Principle

The circuit computes "how well does input match pattern i?"

- Perfect match: score = (number of 1s in i)
- One bit wrong: score = (number of 1s in i) - 1

The bias ensures only perfect matches pass.

## Weight Patterns

| Output | Binary | Weights | Bias |
|--------|--------|---------|------|
| yβ‚€ | 000 | [-1, -1, -1] | 0 |
| y₁ | 001 | [-1, -1, +1] | -1 |
| yβ‚‚ | 010 | [-1, +1, -1] | -1 |
| y₃ | 011 | [-1, +1, +1] | -2 |
| yβ‚„ | 100 | [+1, -1, -1] | -1 |
| yβ‚… | 101 | [+1, -1, +1] | -2 |
| y₆ | 110 | [+1, +1, -1] | -2 |
| y₇ | 111 | [+1, +1, +1] | -3 |

## Single-Layer Elegance

Unlike traditional logic (which uses AND, OR, NOT combinations), threshold logic can decode in one layer. Each output neuron directly computes "does input match me?"

## Architecture

**8 neurons, 32 parameters, 1 layer**

All neurons run in parallel - no dependencies.

## Usage

```python

from safetensors.torch import load_file

import torch



w = load_file('model.safetensors')



def decode(a2, a1, a0):

    inp = torch.tensor([float(a2), float(a1), float(a0)])

    return [int((inp * w[f'y{i}.weight']).sum() + w[f'y{i}.bias'] >= 0)

            for i in range(8)]



# Input 5 -> output 5 is hot

outputs = decode(1, 0, 1)

print(outputs)  # [0, 0, 0, 0, 0, 1, 0, 0]

```

## Files

```

threshold-3to8decoder/

β”œβ”€β”€ model.safetensors

β”œβ”€β”€ model.py

β”œβ”€β”€ config.json

└── README.md

```

## License

MIT