Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
248 items
β’
Updated
β’
1
4-to-2 one-hot decoder. Converts a 4-bit one-hot representation to a 2-bit binary value.
onehot_decode(y3, y2, y1, y0) -> (a1, a0)
Input must have exactly one bit set.
| y3 | y2 | y1 | y0 | a1 | a0 | Value |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 1 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 | 0 | 1 | 1 |
| 0 | 1 | 0 | 0 | 1 | 0 | 2 |
| 1 | 0 | 0 | 0 | 1 | 1 | 3 |
Single-layer implementation:
y3 y2 y1 y0
β β β β
ββββββ΄βββββ΄βββββ
β
ββββββ΄βββββ
β β
βΌ βΌ
βββββ βββββ
βa1 β βa0 β Layer 1
βOR β βOR β
βββββ βββββ
β β
βΌ βΌ
Each output is a single OR gate:
The decoder recognizes that:
| Inputs | 4 |
| Outputs | 2 |
| Neurons | 2 |
| Layers | 1 |
| Parameters | 10 |
| Magnitude | 6 |
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def decode(y3, y2, y1, y0):
inp = torch.tensor([float(y3), float(y2), float(y1), float(y0)])
a0 = int((inp @ w['a0.weight'].T + w['a0.bias'] >= 0).item())
a1 = int((inp @ w['a1.weight'].T + w['a1.bias'] >= 0).item())
return a1, a0
# decode(0, 1, 0, 0) = (1, 0) # value 2
MIT