phanerozoic's picture
Upload folder using huggingface_hub
85582c4 verified
---
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