Upload folder using huggingface_hub
Browse files- README.md +125 -0
- config.json +9 -0
- model.py +25 -0
- model.safetensors +3 -0
README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
- encoder
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# threshold-binarytothermometer
|
| 12 |
+
|
| 13 |
+
Converts 3-bit binary to 7-bit thermometer code. A single-layer threshold circuit.
|
| 14 |
+
|
| 15 |
+
## Circuit
|
| 16 |
+
|
| 17 |
+
```
|
| 18 |
+
bβ bβ bβ
|
| 19 |
+
β β β
|
| 20 |
+
β β β
|
| 21 |
+
βββββ΄ββββ¬ββββ΄ββββ¬ββββ΄ββββ
|
| 22 |
+
β β β β
|
| 23 |
+
βΌ βΌ βΌ βΌ
|
| 24 |
+
ββββββββββββββββββββββββββββββββ
|
| 25 |
+
β yβ ββ yβ ββ yβ ββ ... β
|
| 26 |
+
βw:4,2,1βw:4,2,1βw:4,2,1β β
|
| 27 |
+
βb: -1 ββb: -2 ββb: -3 ββ β
|
| 28 |
+
ββββββββββββββββββββββββββββββββ
|
| 29 |
+
β β β β
|
| 30 |
+
βΌ βΌ βΌ βΌ
|
| 31 |
+
yβ yβ yβ ... yβ
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
+
## Thermometer Code
|
| 35 |
+
|
| 36 |
+
Thermometer encoding represents value n as n consecutive ones:
|
| 37 |
+
|
| 38 |
+
| Value | Binary | Thermometer |
|
| 39 |
+
|-------|--------|-------------|
|
| 40 |
+
| 0 | 000 | 0000000 |
|
| 41 |
+
| 1 | 001 | 1000000 |
|
| 42 |
+
| 2 | 010 | 1100000 |
|
| 43 |
+
| 3 | 011 | 1110000 |
|
| 44 |
+
| 4 | 100 | 1111000 |
|
| 45 |
+
| 5 | 101 | 1111100 |
|
| 46 |
+
| 6 | 110 | 1111110 |
|
| 47 |
+
| 7 | 111 | 1111111 |
|
| 48 |
+
|
| 49 |
+
Like mercury rising in a thermometer - higher values fill more positions.
|
| 50 |
+
|
| 51 |
+
## Mechanism
|
| 52 |
+
|
| 53 |
+
Each output yα΅’ fires when value > i:
|
| 54 |
+
|
| 55 |
+
```
|
| 56 |
+
yα΅’: (4Β·bβ + 2Β·bβ + 1Β·bβ) - (i+1) β₯ 0
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
The weights [4, 2, 1] compute the binary value. The bias sets the threshold.
|
| 60 |
+
|
| 61 |
+
| Output | Bias | Fires when |
|
| 62 |
+
|--------|------|------------|
|
| 63 |
+
| yβ | -1 | value β₯ 1 |
|
| 64 |
+
| yβ | -2 | value β₯ 2 |
|
| 65 |
+
| yβ | -3 | value β₯ 3 |
|
| 66 |
+
| yβ | -4 | value β₯ 4 |
|
| 67 |
+
| yβ | -5 | value β₯ 5 |
|
| 68 |
+
| yβ
| -6 | value β₯ 6 |
|
| 69 |
+
| yβ | -7 | value β₯ 7 |
|
| 70 |
+
|
| 71 |
+
## Why Thermometer?
|
| 72 |
+
|
| 73 |
+
Thermometer codes are used in:
|
| 74 |
+
|
| 75 |
+
- **DACs/ADCs**: Monotonic, glitch-free conversion
|
| 76 |
+
- **Flash ADCs**: Each comparator outputs one thermometer bit
|
| 77 |
+
- **Priority queues**: Natural ordering representation
|
| 78 |
+
- **Neural networks**: Unary encoding preserves magnitude relationships
|
| 79 |
+
|
| 80 |
+
## Single-Layer Elegance
|
| 81 |
+
|
| 82 |
+
This is one of the rare multi-output functions computable in a single layer. Each output is a simple threshold on the input value - no inter-neuron dependencies.
|
| 83 |
+
|
| 84 |
+
## Parameters
|
| 85 |
+
|
| 86 |
+
All neurons share the same weights, only biases differ:
|
| 87 |
+
|
| 88 |
+
| Component | Value |
|
| 89 |
+
|-----------|-------|
|
| 90 |
+
| Weights (all) | [4, 2, 1] |
|
| 91 |
+
| Biases | [-1, -2, -3, -4, -5, -6, -7] |
|
| 92 |
+
|
| 93 |
+
**Total: 7 neurons, 28 parameters, 1 layer**
|
| 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 binary_to_therm(b2, b1, b0):
|
| 104 |
+
inp = torch.tensor([float(b2), float(b1), float(b0)])
|
| 105 |
+
return [int((inp * w[f'y{i}.weight']).sum() + w[f'y{i}.bias'] >= 0)
|
| 106 |
+
for i in range(7)]
|
| 107 |
+
|
| 108 |
+
# Value 5 -> thermometer with 5 ones
|
| 109 |
+
therm = binary_to_therm(1, 0, 1)
|
| 110 |
+
print(therm) # [1, 1, 1, 1, 1, 0, 0]
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
## Files
|
| 114 |
+
|
| 115 |
+
```
|
| 116 |
+
threshold-binarytothermometer/
|
| 117 |
+
βββ model.safetensors
|
| 118 |
+
βββ model.py
|
| 119 |
+
βββ config.json
|
| 120 |
+
βββ README.md
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
## License
|
| 124 |
+
|
| 125 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-binarytothermometer",
|
| 3 |
+
"description": "3-bit binary to 7-bit thermometer converter",
|
| 4 |
+
"inputs": 3,
|
| 5 |
+
"outputs": 7,
|
| 6 |
+
"neurons": 7,
|
| 7 |
+
"layers": 1,
|
| 8 |
+
"parameters": 28
|
| 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 binary_to_thermometer(b2, b1, b0, weights):
|
| 8 |
+
"""Convert 3-bit binary to 7-bit thermometer code.
|
| 9 |
+
Returns list [y0, y1, ..., y6] where yi=1 iff value > i.
|
| 10 |
+
"""
|
| 11 |
+
inp = torch.tensor([float(b2), float(b1), float(b0)])
|
| 12 |
+
outputs = []
|
| 13 |
+
for i in range(7):
|
| 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('Binary to Thermometer Converter')
|
| 21 |
+
print('Value -> Thermometer')
|
| 22 |
+
for val in range(8):
|
| 23 |
+
b2, b1, b0 = (val >> 2) & 1, (val >> 1) & 1, val & 1
|
| 24 |
+
therm = binary_to_thermometer(b2, b1, b0, w)
|
| 25 |
+
print(f" {val} -> {''.join(map(str, therm))}")
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:760d99c9cd64cad36ac300778c64d371067b6ce4596ade3055fcdd3c2a240b23
|
| 3 |
+
size 992
|