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