phanerozoic commited on
Commit
10710cf
Β·
verified Β·
1 Parent(s): 73b8226

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +129 -0
  2. config.json +9 -0
  3. model.py +25 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ - decoder
9
+ ---
10
+
11
+ # threshold-3to8decoder
12
+
13
+ 3-to-8 binary decoder. Converts 3-bit binary input to one-hot 8-bit output. A single-layer threshold circuit.
14
+
15
+ ## Circuit
16
+
17
+ ```
18
+ aβ‚‚ a₁ aβ‚€
19
+ β”‚ β”‚ β”‚
20
+ β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€
21
+ β”‚ β”‚ β”‚
22
+ β”Œβ”€β”€β”€β”΄β”€β”€β”€β”¬β”€β”€β”€β”΄β”€β”€β”€β”¬β”€β”€β”€β”΄β”€β”€β”€β”
23
+ β”‚ β”‚ β”‚ β”‚
24
+ β–Ό β–Ό β–Ό β–Ό
25
+ β”Œβ”€β”€β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”€β”€β”
26
+ β”‚ yβ‚€ β”‚β”‚ y₁ β”‚β”‚ yβ‚‚ β”‚β”‚ ... β”‚
27
+ β”‚-1,-1,-1β”‚β”‚-1,-1,+1β”‚β”‚-1,+1,-1β”‚β”‚ β”‚
28
+ β”‚b: 0 β”‚β”‚b: -1 β”‚β”‚b: -1 β”‚β”‚ β”‚
29
+ β””β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”˜
30
+ β”‚ β”‚ β”‚ β”‚
31
+ β–Ό β–Ό β–Ό β–Ό
32
+ yβ‚€ y₁ yβ‚‚ ... y₇
33
+ ```
34
+
35
+ ## One-Hot Encoding
36
+
37
+ Each input value activates exactly one output:
38
+
39
+ | Input | aβ‚‚a₁aβ‚€ | Output yβ‚€y₁yβ‚‚y₃yβ‚„yβ‚…y₆y₇ |
40
+ |-------|--------|--------------------------|
41
+ | 0 | 000 | 10000000 |
42
+ | 1 | 001 | 01000000 |
43
+ | 2 | 010 | 00100000 |
44
+ | 3 | 011 | 00010000 |
45
+ | 4 | 100 | 00001000 |
46
+ | 5 | 101 | 00000100 |
47
+ | 6 | 110 | 00000010 |
48
+ | 7 | 111 | 00000001 |
49
+
50
+ ## Mechanism
51
+
52
+ Each output yα΅’ acts as a "pattern matcher" for input = i:
53
+
54
+ - **Weight +1** for bit positions that should be 1
55
+ - **Weight -1** for bit positions that should be 0
56
+ - **Bias** = -(number of 1 bits in i)
57
+
58
+ Example for yβ‚… (binary 101):
59
+ ```
60
+ weights: [+1, -1, +1] (match 1, reject 0, match 1)
61
+ bias: -2 (need 2 matches to fire)
62
+ ```
63
+
64
+ When input = 101: sum = 1Β·1 + (-1)Β·0 + 1Β·1 = 2, fires
65
+ When input = 111: sum = 1Β·1 + (-1)Β·1 + 1Β·1 = 1, doesn't fire
66
+
67
+ ## The Matching Principle
68
+
69
+ The circuit computes "how well does input match pattern i?"
70
+
71
+ - Perfect match: score = (number of 1s in i)
72
+ - One bit wrong: score = (number of 1s in i) - 1
73
+
74
+ The bias ensures only perfect matches pass.
75
+
76
+ ## Weight Patterns
77
+
78
+ | Output | Binary | Weights | Bias |
79
+ |--------|--------|---------|------|
80
+ | yβ‚€ | 000 | [-1, -1, -1] | 0 |
81
+ | y₁ | 001 | [-1, -1, +1] | -1 |
82
+ | yβ‚‚ | 010 | [-1, +1, -1] | -1 |
83
+ | y₃ | 011 | [-1, +1, +1] | -2 |
84
+ | yβ‚„ | 100 | [+1, -1, -1] | -1 |
85
+ | yβ‚… | 101 | [+1, -1, +1] | -2 |
86
+ | y₆ | 110 | [+1, +1, -1] | -2 |
87
+ | y₇ | 111 | [+1, +1, +1] | -3 |
88
+
89
+ ## Single-Layer Elegance
90
+
91
+ 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?"
92
+
93
+ ## Architecture
94
+
95
+ **8 neurons, 32 parameters, 1 layer**
96
+
97
+ All neurons run in parallel - no dependencies.
98
+
99
+ ## Usage
100
+
101
+ ```python
102
+ from safetensors.torch import load_file
103
+ import torch
104
+
105
+ w = load_file('model.safetensors')
106
+
107
+ def decode(a2, a1, a0):
108
+ inp = torch.tensor([float(a2), float(a1), float(a0)])
109
+ return [int((inp * w[f'y{i}.weight']).sum() + w[f'y{i}.bias'] >= 0)
110
+ for i in range(8)]
111
+
112
+ # Input 5 -> output 5 is hot
113
+ outputs = decode(1, 0, 1)
114
+ print(outputs) # [0, 0, 0, 0, 0, 1, 0, 0]
115
+ ```
116
+
117
+ ## Files
118
+
119
+ ```
120
+ threshold-3to8decoder/
121
+ β”œβ”€β”€ model.safetensors
122
+ β”œβ”€β”€ model.py
123
+ β”œβ”€β”€ config.json
124
+ └── README.md
125
+ ```
126
+
127
+ ## License
128
+
129
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-3to8decoder",
3
+ "description": "3-to-8 binary decoder as threshold circuit",
4
+ "inputs": 3,
5
+ "outputs": 8,
6
+ "neurons": 8,
7
+ "layers": 1,
8
+ "parameters": 32
9
+ }
model.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import load_file
3
+
4
+ def load_model(path='model.safetensors'):
5
+ return load_file(path)
6
+
7
+ def decode_3to8(a2, a1, a0, weights):
8
+ """3-to-8 decoder: converts 3-bit binary to one-hot 8-bit output.
9
+ Returns list [y0, y1, ..., y7] where yi=1 iff input = i.
10
+ """
11
+ inp = torch.tensor([float(a2), float(a1), float(a0)])
12
+ outputs = []
13
+ for i in range(8):
14
+ y = int((inp * weights[f'y{i}.weight']).sum() + weights[f'y{i}.bias'] >= 0)
15
+ outputs.append(y)
16
+ return outputs
17
+
18
+ if __name__ == '__main__':
19
+ w = load_model()
20
+ print('3-to-8 Decoder')
21
+ print('Input -> One-hot output')
22
+ for val in range(8):
23
+ a2, a1, a0 = (val >> 2) & 1, (val >> 1) & 1, val & 1
24
+ outputs = decode_3to8(a2, a1, a0, w)
25
+ print(f" {val} ({a2},{a1},{a0}) -> {''.join(map(str, outputs))}")
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:849544a81f765170bdc0541aae6988f31857af26ba696f97e0d167aace70e7cd
3
+ size 1136