phanerozoic commited on
Commit
c47804c
Β·
verified Β·
1 Parent(s): 3c19fa3

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +72 -12
README.md CHANGED
@@ -9,7 +9,7 @@ tags:
9
 
10
  # threshold-exactly2outof8
11
 
12
- Exactly-2-out-of-8 detector. Fires when exactly two inputs are active.
13
 
14
  ## Circuit
15
 
@@ -22,8 +22,7 @@ Exactly-2-out-of-8 detector. Fires when exactly two inputs are active.
22
  β–Ό β–Ό
23
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
24
  β”‚ AtLeast2β”‚ β”‚ AtMost2 β”‚
25
- β”‚ w: +1Γ—8 β”‚ β”‚ w: -1Γ—8 β”‚
26
- β”‚ b: -2 β”‚ β”‚ b: +2 β”‚
27
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
28
  β”‚ β”‚
29
  β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
@@ -36,25 +35,86 @@ Exactly-2-out-of-8 detector. Fires when exactly two inputs are active.
36
  (HW = 2?)
37
  ```
38
 
 
 
 
 
 
 
 
 
 
 
39
  ## Mechanism
40
 
41
- - **AtLeast2**: Fires when HW β‰₯ 2 (bias -2)
42
- - **AtMost2**: Fires when HW ≀ 2 (weights -1, bias +2)
43
- - **AND**: Exactly2 = AtLeast2 AND AtMost2
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  ## Truth Table
46
 
47
- | HW | AtLeast2 | AtMost2 | Exactly2 |
48
- |----|----------|---------|----------|
49
  | 0 | 0 | 1 | 0 |
50
  | 1 | 0 | 1 | 0 |
51
- | 2 | 1 | 1 | **1** |
52
  | 3 | 1 | 0 | 0 |
53
- | ... | 1 | 0 | 0 |
54
 
55
- ## Architecture
56
 
57
- **3 neurons, 21 parameters, 2 layers**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  ## Files
60
 
 
9
 
10
  # threshold-exactly2outof8
11
 
12
+ Exactly-2-out-of-8 detector. Fires when precisely two inputs are active. The pair detector.
13
 
14
  ## Circuit
15
 
 
22
  β–Ό β–Ό
23
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
24
  β”‚ AtLeast2β”‚ β”‚ AtMost2 β”‚
25
+ β”‚ HW β‰₯ 2 β”‚ β”‚ HW ≀ 2 β”‚
 
26
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
27
  β”‚ β”‚
28
  β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
 
35
  (HW = 2?)
36
  ```
37
 
38
+ ## The Pair Detection Problem
39
+
40
+ Detecting exactly two active inputs is useful in:
41
+
42
+ - **Error detection**: Exactly 2 bit flips in a codeword
43
+ - **Voting systems**: Detecting ties in 4-person subcommittees
44
+ - **Combinatorics**: Enumerating pairs from a set
45
+
46
+ There are C(8,2) = 28 input patterns that fire this circuit.
47
+
48
  ## Mechanism
49
 
50
+ Two threshold neurons work in concert:
51
+
52
+ **AtLeast2** (lower bound):
53
+ ```
54
+ sum = xβ‚€ + x₁ + ... + x₇ - 2
55
+ fires when sum β‰₯ 0, i.e., HW β‰₯ 2
56
+ ```
57
+
58
+ **AtMost2** (upper bound):
59
+ ```
60
+ sum = -xβ‚€ - x₁ - ... - x₇ + 2
61
+ fires when sum β‰₯ 0, i.e., -HW + 2 β‰₯ 0, i.e., HW ≀ 2
62
+ ```
63
+
64
+ The AND gate computes the intersection: HW β‰₯ 2 AND HW ≀ 2 = HW = 2.
65
 
66
  ## Truth Table
67
 
68
+ | HW | AtLeast2 | AtMost2 | Output |
69
+ |----|----------|---------|--------|
70
  | 0 | 0 | 1 | 0 |
71
  | 1 | 0 | 1 | 0 |
72
+ | **2** | **1** | **1** | **1** |
73
  | 3 | 1 | 0 | 0 |
74
+ | 4+ | 1 | 0 | 0 |
75
 
76
+ ## Parameters
77
 
78
+ | Component | Weights | Bias |
79
+ |-----------|---------|------|
80
+ | AtLeast2 | [+1, +1, +1, +1, +1, +1, +1, +1] | -2 |
81
+ | AtMost2 | [-1, -1, -1, -1, -1, -1, -1, -1] | +2 |
82
+ | AND | [+1, +1] | -2 |
83
+
84
+ **Total: 3 neurons, 21 parameters, 2 layers**
85
+
86
+ ## Contrast with At-Least-2
87
+
88
+ | Circuit | Condition | Fires on HW |
89
+ |---------|-----------|-------------|
90
+ | 2-out-of-8 | HW β‰₯ 2 | 2, 3, 4, 5, 6, 7, 8 |
91
+ | **Exactly-2** | HW = 2 | 2 only |
92
+
93
+ The existing `threshold-2outof8` fires on 247 of 256 inputs. This circuit fires on only 28.
94
+
95
+ ## Usage
96
+
97
+ ```python
98
+ from safetensors.torch import load_file
99
+ import torch
100
+
101
+ w = load_file('model.safetensors')
102
+
103
+ def exactly2(bits):
104
+ inp = torch.tensor([float(b) for b in bits])
105
+ atleast = int((inp * w['atleast.weight']).sum() + w['atleast.bias'] >= 0)
106
+ atmost = int((inp * w['atmost.weight']).sum() + w['atmost.bias'] >= 0)
107
+ comb = torch.tensor([float(atleast), float(atmost)])
108
+ return int((comb * w['and.weight']).sum() + w['and.bias'] >= 0)
109
+
110
+ # Pair: positions 2 and 5 active
111
+ bits = [0, 0, 1, 0, 0, 1, 0, 0]
112
+ print(exactly2(bits)) # 1
113
+
114
+ # Three active - not a pair
115
+ bits = [1, 0, 1, 0, 0, 1, 0, 0]
116
+ print(exactly2(bits)) # 0
117
+ ```
118
 
119
  ## Files
120