File size: 1,632 Bytes
81fa364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
---
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