Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
β’
248 items
β’
Updated
β’
1
2-to-4 one-hot encoder. Converts a 2-bit binary value to a 4-bit one-hot representation.
onehot_encode(a1, a0) -> (y3, y2, y1, y0)
Exactly one output bit is set, corresponding to the input value.
| 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 |
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:
| Inputs | 2 |
| Outputs | 4 |
| Neurons | 4 |
| Layers | 1 |
| Parameters | 12 |
| Magnitude | 12 |
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
MIT