Threshold Logic Circuits
Collection
Boolean gates, voting functions, modular arithmetic, and adders as threshold networks.
•
248 items
•
Updated
•
1
4-to-2 priority encoder. Outputs binary encoding of highest-priority active input.
priority_encode(i3, i2, i1, i0) -> (y1, y0, valid)
| i3 | i2 | i1 | i0 | y1 | y0 | v | highest |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | none |
| 0 | 0 | 0 | 1 | 0 | 0 | 1 | i0 |
| 0 | 0 | 1 | X | 0 | 1 | 1 | i1 |
| 0 | 1 | X | X | 1 | 0 | 1 | i2 |
| 1 | X | X | X | 1 | 1 | 1 | i3 |
Single layer with 3 neurons:
| Inputs | 4 |
| Outputs | 3 |
| Neurons | 3 |
| Layers | 1 |
| Parameters | 15 |
| Magnitude | 13 |
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def priority_encode(i3, i2, i1, i0):
inp = torch.tensor([float(i3), float(i2), float(i1), float(i0)])
y1 = int((inp @ w['y1.weight'].T + w['y1.bias'] >= 0).item())
y0 = int((inp @ w['y0.weight'].T + w['y0.bias'] >= 0).item())
v = int((inp @ w['v.weight'].T + w['v.bias'] >= 0).item())
return y1, y0, v
print(priority_encode(0, 1, 1, 0)) # (1, 0, 1) -> i2 is highest
print(priority_encode(1, 1, 1, 1)) # (1, 1, 1) -> i3 is highest
MIT