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