|
|
---
|
|
|
license: mit
|
|
|
tags:
|
|
|
- pytorch
|
|
|
- safetensors
|
|
|
- threshold-logic
|
|
|
- neuromorphic
|
|
|
- modular-arithmetic
|
|
|
---
|
|
|
|
|
|
# threshold-mod7
|
|
|
|
|
|
Computes Hamming weight mod 7 for 8-bit inputs. Multi-layer network with thermometer encoding.
|
|
|
|
|
|
## Circuit
|
|
|
|
|
|
```
|
|
|
xβ xβ xβ xβ xβ xβ
xβ xβ
|
|
|
β β β β β β β β
|
|
|
ββββ΄βββ΄βββ΄βββΌβββ΄βββ΄βββ΄βββ
|
|
|
βΌ
|
|
|
βββββββββββββββ
|
|
|
β Thermometer β Layer 1: 9 neurons
|
|
|
βββββββββββββββ
|
|
|
β
|
|
|
βΌ
|
|
|
βββββββββββββββ
|
|
|
β MOD-7 β Layer 2: 6 neurons
|
|
|
β Detection β Pattern (1,1,1,1,1,1,-6)
|
|
|
βββββββββββββββ
|
|
|
β
|
|
|
βΌ
|
|
|
βββββββββββββββ
|
|
|
β Classify β Output: 7 classes
|
|
|
βββββββββββββββ
|
|
|
β
|
|
|
βΌ
|
|
|
{0, 1, 2, 3, 4, 5, 6}
|
|
|
```
|
|
|
|
|
|
## Algebraic Insight
|
|
|
|
|
|
Pattern `(1, 1, 1, 1, 1, 1, -6)` cycles mod 7:
|
|
|
|
|
|
```
|
|
|
HW=0: sum=0 β 0 mod 7
|
|
|
...
|
|
|
HW=6: sum=6 β 6 mod 7
|
|
|
HW=7: sum=0 β 0 mod 7 (reset: 1+1+1+1+1+1-6=0)
|
|
|
HW=8: sum=1 β 1 mod 7
|
|
|
```
|
|
|
|
|
|
For 8-bit inputs, only one reset occurs (at HW=7).
|
|
|
|
|
|
## Architecture
|
|
|
|
|
|
| Layer | Neurons | Function |
|
|
|
|-------|---------|----------|
|
|
|
| Input | 8 | Binary bits |
|
|
|
| Hidden 1 | 9 | Thermometer encoding |
|
|
|
| Hidden 2 | 6 | MOD-7 detection |
|
|
|
| Output | 7 | One-hot classification |
|
|
|
|
|
|
**Total: 22 neurons, 190 parameters**
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
```python
|
|
|
from safetensors.torch import load_file
|
|
|
import torch
|
|
|
|
|
|
w = load_file('model.safetensors')
|
|
|
|
|
|
def forward(x):
|
|
|
x = x.float()
|
|
|
x = (x @ w['layer1.weight'].T + w['layer1.bias'] >= 0).float()
|
|
|
x = (x @ w['layer2.weight'].T + w['layer2.bias'] >= 0).float()
|
|
|
out = x @ w['output.weight'].T + w['output.bias']
|
|
|
return out.argmax(dim=-1)
|
|
|
```
|
|
|
|
|
|
## Files
|
|
|
|
|
|
```
|
|
|
threshold-mod7/
|
|
|
βββ model.safetensors
|
|
|
βββ model.py
|
|
|
βββ config.json
|
|
|
βββ README.md
|
|
|
```
|
|
|
|
|
|
## License
|
|
|
|
|
|
MIT
|
|
|
|