|
|
---
|
|
|
license: mit
|
|
|
tags:
|
|
|
- pytorch
|
|
|
- safetensors
|
|
|
- threshold-logic
|
|
|
- neuromorphic
|
|
|
- encoder
|
|
|
---
|
|
|
|
|
|
# threshold-binarytothermometer
|
|
|
|
|
|
Converts 3-bit binary to 7-bit thermometer code. A single-layer threshold circuit.
|
|
|
|
|
|
## Circuit
|
|
|
|
|
|
```
|
|
|
bβ bβ bβ
|
|
|
β β β
|
|
|
β β β
|
|
|
βββββ΄ββββ¬ββββ΄ββββ¬ββββ΄ββββ
|
|
|
β β β β
|
|
|
βΌ βΌ βΌ βΌ
|
|
|
ββββββββββββββββββββββββββββββββ
|
|
|
β yβ ββ yβ ββ yβ ββ ... β
|
|
|
βw:4,2,1βw:4,2,1βw:4,2,1β β
|
|
|
βb: -1 ββb: -2 ββb: -3 ββ β
|
|
|
ββββββββββββββββββββββββββββββββ
|
|
|
β β β β
|
|
|
βΌ βΌ βΌ βΌ
|
|
|
yβ yβ yβ ... yβ
|
|
|
```
|
|
|
|
|
|
## Thermometer Code
|
|
|
|
|
|
Thermometer encoding represents value n as n consecutive ones:
|
|
|
|
|
|
| Value | Binary | Thermometer |
|
|
|
|-------|--------|-------------|
|
|
|
| 0 | 000 | 0000000 |
|
|
|
| 1 | 001 | 1000000 |
|
|
|
| 2 | 010 | 1100000 |
|
|
|
| 3 | 011 | 1110000 |
|
|
|
| 4 | 100 | 1111000 |
|
|
|
| 5 | 101 | 1111100 |
|
|
|
| 6 | 110 | 1111110 |
|
|
|
| 7 | 111 | 1111111 |
|
|
|
|
|
|
Like mercury rising in a thermometer - higher values fill more positions.
|
|
|
|
|
|
## Mechanism
|
|
|
|
|
|
Each output yα΅’ fires when value > i:
|
|
|
|
|
|
```
|
|
|
yα΅’: (4Β·bβ + 2Β·bβ + 1Β·bβ) - (i+1) β₯ 0
|
|
|
```
|
|
|
|
|
|
The weights [4, 2, 1] compute the binary value. The bias sets the threshold.
|
|
|
|
|
|
| Output | Bias | Fires when |
|
|
|
|--------|------|------------|
|
|
|
| yβ | -1 | value β₯ 1 |
|
|
|
| yβ | -2 | value β₯ 2 |
|
|
|
| yβ | -3 | value β₯ 3 |
|
|
|
| yβ | -4 | value β₯ 4 |
|
|
|
| yβ | -5 | value β₯ 5 |
|
|
|
| yβ
| -6 | value β₯ 6 |
|
|
|
| yβ | -7 | value β₯ 7 |
|
|
|
|
|
|
## Why Thermometer?
|
|
|
|
|
|
Thermometer codes are used in:
|
|
|
|
|
|
- **DACs/ADCs**: Monotonic, glitch-free conversion
|
|
|
- **Flash ADCs**: Each comparator outputs one thermometer bit
|
|
|
- **Priority queues**: Natural ordering representation
|
|
|
- **Neural networks**: Unary encoding preserves magnitude relationships
|
|
|
|
|
|
## Single-Layer Elegance
|
|
|
|
|
|
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.
|
|
|
|
|
|
## Parameters
|
|
|
|
|
|
All neurons share the same weights, only biases differ:
|
|
|
|
|
|
| Component | Value |
|
|
|
|-----------|-------|
|
|
|
| Weights (all) | [4, 2, 1] |
|
|
|
| Biases | [-1, -2, -3, -4, -5, -6, -7] |
|
|
|
|
|
|
**Total: 7 neurons, 28 parameters, 1 layer**
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
```python
|
|
|
from safetensors.torch import load_file
|
|
|
import torch
|
|
|
|
|
|
w = load_file('model.safetensors')
|
|
|
|
|
|
def binary_to_therm(b2, b1, b0):
|
|
|
inp = torch.tensor([float(b2), float(b1), float(b0)])
|
|
|
return [int((inp * w[f'y{i}.weight']).sum() + w[f'y{i}.bias'] >= 0)
|
|
|
for i in range(7)]
|
|
|
|
|
|
# Value 5 -> thermometer with 5 ones
|
|
|
therm = binary_to_therm(1, 0, 1)
|
|
|
print(therm) # [1, 1, 1, 1, 1, 0, 0]
|
|
|
```
|
|
|
|
|
|
## Files
|
|
|
|
|
|
```
|
|
|
threshold-binarytothermometer/
|
|
|
βββ model.safetensors
|
|
|
βββ model.py
|
|
|
βββ config.json
|
|
|
βββ README.md
|
|
|
```
|
|
|
|
|
|
## License
|
|
|
|
|
|
MIT
|
|
|
|