| --- |
| license: mit |
| tags: |
| - pytorch |
| - safetensors |
| - threshold-logic |
| - neuromorphic |
| - encoder |
| --- |
| |
| # threshold-priorityencoder4 |
|
|
| 4-to-2 priority encoder. Outputs binary encoding of highest-priority active input. |
|
|
| ## Function |
|
|
| priority_encode(i3, i2, i1, i0) -> (y1, y0, valid) |
| |
| - i3 = highest priority, i0 = lowest priority |
| - y1,y0 = 2-bit binary encoding of highest active input |
| - valid = 1 if any input is active |
| |
| ## Truth Table (selected) |
| |
| | 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 | |
| |
| ## Architecture |
| |
| Single layer with 3 neurons: |
| |
| - y1 = i3 OR i2: weights [1,1,0,0], bias -1 |
| - y0 = i3 OR (i1 AND NOT i2): weights [2,-1,1,0], bias -1 |
| - v = i3 OR i2 OR i1 OR i0: weights [1,1,1,1], bias -1 |
| |
| ## Parameters |
| |
| | | | |
| |---|---| |
| | Inputs | 4 | |
| | Outputs | 3 | |
| | Neurons | 3 | |
| | Layers | 1 | |
| | Parameters | 15 | |
| | Magnitude | 13 | |
| |
| ## Usage |
| |
| ```python |
| 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 |
| ``` |
| |
| ## License |
| |
| MIT |
| |