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

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

Downloads last month
9
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Collection including phanerozoic/threshold-binary-to-onehot