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 index of highest-priority set input.
encode(I3, I2, I1, I0) -> (Y1, Y0)
Priority: I3 > I2 > I1 > I0
| I3 | I2 | I1 | I0 | Y1 | Y0 | Index |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 1 | 0 | 0 | 0 |
| 0 | 0 | 1 | x | 0 | 1 | 1 |
| 0 | 1 | x | x | 1 | 0 | 2 |
| 1 | x | x | x | 1 | 1 | 3 |
Single layer with 2 neurons:
| Output | Weights [I3, I2, I1, I0] | Bias | Function |
|---|---|---|---|
| Y1 | [1, 1, 0, 0] | -1 | I3 OR I2 |
| Y0 | [3, -2, 1, 0] | -1 | I3 OR (NOT I2 AND I1) |
| Inputs | 4 |
| Outputs | 2 |
| Neurons | 2 |
| Layers | 1 |
| Parameters | 10 |
| Magnitude | 10 |
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def encode4to2(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())
return y1, y0
print(encode4to2(0, 1, 1, 0)) # (1, 0) -> index 2 (I2 is highest)
print(encode4to2(1, 0, 0, 1)) # (1, 1) -> index 3 (I3 is highest)
MIT