File size: 3,209 Bytes
85582c4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
---
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
|