CharlesCNorton
Add 2-to-4 one-hot encoder threshold circuit
3f960d8
---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- encoding
---
# threshold-onehot-encoder
2-to-4 one-hot encoder. Converts a 2-bit binary value to a 4-bit one-hot representation.
## Function
onehot_encode(a1, a0) -> (y3, y2, y1, y0)
Exactly one output bit is set, corresponding to the input value.
## Truth Table
| a1 | a0 | y3 | y2 | y1 | y0 | Value |
|----|----|----|----|----|-----|-------|
| 0 | 0 | 0 | 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 | 0 | 2 |
| 1 | 1 | 1 | 0 | 0 | 0 | 3 |
## Architecture
Single-layer implementation using threshold logic:
```
a1 a0
β”‚ β”‚
β”Œβ”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”
β”‚ β”‚
β–Ό β–Ό β–Ό β–Ό
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚y3 β”‚y2 β”‚y1 β”‚y0 β”‚ Layer 1
β”‚ANDβ”‚AΒ·Bβ”‚AΒ·Bβ”‚NORβ”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
β”‚ β”‚ β”‚ β”‚
β–Ό β–Ό β–Ό β–Ό
```
Each output is a single threshold neuron:
- y0 = NOR(a1, a0): w=[-1,-1], b=0
- y1 = NOT(a1) AND a0: w=[-1,1], b=-1
- y2 = a1 AND NOT(a0): w=[1,-1], b=-1
- y3 = a1 AND a0: w=[1,1], b=-2
## Parameters
| | |
|---|---|
| Inputs | 2 |
| Outputs | 4 |
| Neurons | 4 |
| Layers | 1 |
| Parameters | 12 |
| Magnitude | 12 |
## Usage
```python
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def 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 y3, y2, y1, y0
# onehot(1, 0) = (0, 1, 0, 0) # value 2
```
## Applications
- Address decoding
- Memory select lines
- State machine encoding
- Neural network input preprocessing
## License
MIT