--- 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