phanerozoic commited on
Commit
d8d93ae
Β·
verified Β·
1 Parent(s): fa0dda9

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +121 -0
  2. config.json +9 -0
  3. model.py +23 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-exactly1outof8
11
+
12
+ Exactly-1-out-of-8 detector. Fires when exactly one input is active.
13
+
14
+ ## Circuit
15
+
16
+ ```
17
+ xβ‚€ x₁ xβ‚‚ x₃ xβ‚„ xβ‚… x₆ x₇
18
+ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
19
+ β””β”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”Όβ”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”˜
20
+ β”‚
21
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”
22
+ β–Ό β–Ό
23
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
24
+ β”‚ AtLeast1β”‚ β”‚ AtMost1 β”‚
25
+ β”‚ w: +1Γ—8 β”‚ β”‚ w: -1Γ—8 β”‚
26
+ β”‚ b: -1 β”‚ β”‚ b: +1 β”‚
27
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
28
+ β”‚ β”‚
29
+ β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
30
+ β–Ό
31
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
32
+ β”‚ AND β”‚
33
+ β”‚ w: 1, 1 β”‚
34
+ β”‚ b: -2 β”‚
35
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
36
+ β”‚
37
+ β–Ό
38
+ (HW = 1?)
39
+ ```
40
+
41
+ ## Mechanism
42
+
43
+ The circuit uses two threshold neurons in parallel:
44
+
45
+ 1. **AtLeast1**: Fires when HW β‰₯ 1 (at least one input active)
46
+ - Weights: all +1
47
+ - Bias: -1
48
+
49
+ 2. **AtMost1**: Fires when HW ≀ 1 (at most one input active)
50
+ - Weights: all -1
51
+ - Bias: +1
52
+ - Logic: fires when -HW + 1 β‰₯ 0, i.e., HW ≀ 1
53
+
54
+ 3. **AND**: Combines the two conditions
55
+ - Exactly1 = AtLeast1 AND AtMost1
56
+
57
+ ## Truth Table
58
+
59
+ | HW | AtLeast1 | AtMost1 | Exactly1 |
60
+ |----|----------|---------|----------|
61
+ | 0 | 0 | 1 | 0 |
62
+ | 1 | 1 | 1 | **1** |
63
+ | 2 | 1 | 0 | 0 |
64
+ | 3 | 1 | 0 | 0 |
65
+ | ... | 1 | 0 | 0 |
66
+ | 8 | 1 | 0 | 0 |
67
+
68
+ ## Exactly-k Family
69
+
70
+ | Circuit | AtLeast bias | AtMost bias |
71
+ |---------|--------------|-------------|
72
+ | **Exactly1** | -1 | +1 |
73
+ | Exactly2 | -2 | +2 |
74
+ | Exactly3 | -3 | +3 |
75
+ | ... | -k | +k |
76
+ | Exactly7 | -7 | +7 |
77
+
78
+ All use the same structure: two threshold detectors + AND.
79
+
80
+ ## Architecture
81
+
82
+ | Component | Neurons | Parameters |
83
+ |-----------|---------|------------|
84
+ | AtLeast1 | 1 | 9 |
85
+ | AtMost1 | 1 | 9 |
86
+ | AND | 1 | 3 |
87
+ | **Total** | **3** | **21** |
88
+
89
+ **Layers: 2**
90
+
91
+ ## Usage
92
+
93
+ ```python
94
+ from safetensors.torch import load_file
95
+ import torch
96
+
97
+ w = load_file('model.safetensors')
98
+
99
+ def exactly1(bits):
100
+ inp = torch.tensor([float(b) for b in bits])
101
+ atleast = int((inp * w['atleast.weight']).sum() + w['atleast.bias'] >= 0)
102
+ atmost = int((inp * w['atmost.weight']).sum() + w['atmost.bias'] >= 0)
103
+ return int((torch.tensor([float(atleast), float(atmost)]) * w['and.weight']).sum() + w['and.bias'] >= 0)
104
+
105
+ bits = [0, 0, 0, 1, 0, 0, 0, 0] # HW=1
106
+ print(exactly1(bits)) # 1
107
+ ```
108
+
109
+ ## Files
110
+
111
+ ```
112
+ threshold-exactly1outof8/
113
+ β”œβ”€β”€ model.safetensors
114
+ β”œβ”€β”€ model.py
115
+ β”œβ”€β”€ config.json
116
+ └── README.md
117
+ ```
118
+
119
+ ## License
120
+
121
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-exactly1outof8",
3
+ "description": "Exactly-1-out-of-8 detector as threshold circuit",
4
+ "inputs": 8,
5
+ "outputs": 1,
6
+ "neurons": 3,
7
+ "layers": 2,
8
+ "parameters": 20
9
+ }
model.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 exactly1(bits, weights):
8
+ """Exactly-1-out-of-8 detector.
9
+ bits: list of 8 binary values
10
+ Returns: 1 if exactly one bit is set, 0 otherwise
11
+ """
12
+ inp = torch.tensor([float(b) for b in bits])
13
+ atleast = int((inp * weights['atleast.weight']).sum() + weights['atleast.bias'] >= 0)
14
+ atmost = int((inp * weights['atmost.weight']).sum() + weights['atmost.bias'] >= 0)
15
+ return int((torch.tensor([float(atleast), float(atmost)]) * weights['and.weight']).sum() + weights['and.bias'] >= 0)
16
+
17
+ if __name__ == '__main__':
18
+ w = load_model()
19
+ print('Exactly1OutOf8 Detector')
20
+ for hw in range(9):
21
+ bits = [1] * hw + [0] * (8 - hw)
22
+ result = exactly1(bits, w)
23
+ print(f'HW={hw}: {result}')
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3de11d8b2cd4d44e20f6a71ae67e97603b25165fa55131f9918f7c08e15f7a09
3
+ size 484