File size: 4,279 Bytes
7feeb56 | 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | ---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- encoding
- decoder
---
# threshold-binary-to-onehot
2-to-4 binary-to-one-hot encoder. Converts a 2-bit binary value to a 4-bit one-hot representation. The inverse of the one-hot decoder.
## Circuit
```
a1 a0
β β
βββββββββββ€
β β
βββββ΄ββββ βββββ΄ββββ
β β β β
βΌ βΌ βΌ βΌ
βββββ βββββ βββββ βββββ
βy0 β βy1 β βy2 β βy3 β
βNORβ βANDβ βANDβ βANDβ
β β βΒ¬a1β βa1 β βa1 β
β β β a0β βΒ¬a0β βa0 β
βββββ βββββ βββββ βββββ
β β β β
βΌ βΌ βΌ βΌ
y0 y1 y2 y3
(00) (01) (10) (11)
```
## Function
```
binary_to_onehot(a1, a0) -> (y0, y1, y2, y3)
```
Exactly one output is high, corresponding to the binary input value.
## Truth Table
| a1 | a0 | Value | y0 | y1 | y2 | y3 |
|:--:|:--:|:-----:|:--:|:--:|:--:|:--:|
| 0 | 0 | 0 | 1 | 0 | 0 | 0 |
| 0 | 1 | 1 | 0 | 1 | 0 | 0 |
| 1 | 0 | 2 | 0 | 0 | 1 | 0 |
| 1 | 1 | 3 | 0 | 0 | 0 | 1 |
Output encoding: `y[i] = 1` iff input value equals `i`.
## Mechanism
Each output neuron detects a specific binary pattern:
| Output | Pattern | Weights [a1, a0] | Bias | Logic |
|--------|---------|------------------|------|-------|
| y0 | 00 | [-1, -1] | 0 | NOR(a1, a0) |
| y1 | 01 | [-1, +1] | -1 | Β¬a1 AND a0 |
| y2 | 10 | [+1, -1] | -1 | a1 AND Β¬a0 |
| y3 | 11 | [+1, +1] | -2 | a1 AND a0 |
**Analysis:**
- **y0 (value 0):** Fires when both inputs are 0. Negative weights ensure any 1 prevents firing.
- **y1 (value 1):** Fires when a1=0 and a0=1. The -1 on a1 inhibits, +1 on a0 excites.
- **y2 (value 2):** Fires when a1=1 and a0=0. The +1 on a1 excites, -1 on a0 inhibits.
- **y3 (value 3):** Fires when both are 1. Both positive weights must overcome bias -2.
## Architecture
Single-layer implementation with 4 parallel neurons:
| Neuron | Weights | Bias | Detects |
|--------|---------|------|---------|
| y0 | [-1, -1] | 0 | Binary 00 |
| y1 | [-1, +1] | -1 | Binary 01 |
| y2 | [+1, -1] | -1 | Binary 10 |
| y3 | [+1, +1] | -2 | Binary 11 |
## Parameters
| | |
|---|---|
| Inputs | 2 (a1, a0) |
| Outputs | 4 (y0, y1, y2, y3) |
| Neurons | 4 |
| Layers | 1 |
| Parameters | 12 |
| Magnitude | 12 |
## Relationship to One-Hot Decoder
This circuit is the **inverse** of `threshold-onehot-decoder`:
```
Binary [a1, a0] βββΊ binary-to-onehot βββΊ One-hot [y0, y1, y2, y3]
One-hot [y0, y1, y2, y3] βββΊ onehot-decoder βββΊ Binary [a1, a0]
```
Together they form a codec pair for binary β one-hot conversion.
## Usage
```python
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def binary_to_onehot(a1, a0):
inp = torch.tensor([float(a1), float(a0)])
y0 = int((inp @ w['y0.weight'].T + w['y0.bias'] >= 0).item())
y1 = int((inp @ w['y1.weight'].T + w['y1.bias'] >= 0).item())
y2 = int((inp @ w['y2.weight'].T + w['y2.bias'] >= 0).item())
y3 = int((inp @ w['y3.weight'].T + w['y3.bias'] >= 0).item())
return y0, y1, y2, y3
# Examples
print(binary_to_onehot(0, 0)) # (1, 0, 0, 0) - value 0
print(binary_to_onehot(0, 1)) # (0, 1, 0, 0) - value 1
print(binary_to_onehot(1, 0)) # (0, 0, 1, 0) - value 2
print(binary_to_onehot(1, 1)) # (0, 0, 0, 1) - value 3
```
## Applications
- Address decoding in memory systems
- Demultiplexer control signals
- State machine encoding
- Neural network softmax approximation
- Priority encoder inputs
## Scaling
For n-bit binary to 2^n one-hot:
- 3-bit β 8 outputs (8 neurons)
- 4-bit β 16 outputs (16 neurons)
Each additional input bit doubles the outputs.
## Files
```
threshold-binary-to-onehot/
βββ model.safetensors
βββ model.py
βββ create_safetensors.py
βββ config.json
βββ README.md
```
## License
MIT
|