File size: 1,499 Bytes
96dfc17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---

license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
---


# threshold-4to2encoder

4-to-2 priority encoder. Outputs binary index of highest-priority set input.

## Function

encode(I3, I2, I1, I0) -> (Y1, Y0)

Priority: I3 > I2 > I1 > I0

## Truth Table (selected rows)

| 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 |

## Architecture

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) |

## Parameters

| | |
|---|---|
| Inputs | 4 |
| Outputs | 2 |
| Neurons | 2 |
| Layers | 1 |
| Parameters | 10 |
| Magnitude | 10 |

## Usage

```python

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)

```

## License

MIT