File size: 1,926 Bytes
5937f23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
75
76
77
78
79
80
81
---

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


# threshold-8to3encoder

8-to-3 priority encoder. Outputs binary index of highest-priority set input.

## Function

encode(I7..I0) -> (Y2, Y1, Y0)

Priority: I7 > I6 > I5 > I4 > I3 > I2 > I1 > I0

## Example Encodings

| Input | Highest | Output |
|-------|---------|--------|
| 10000000 | I7 | 111 (7) |
| 01000000 | I6 | 110 (6) |
| 00100000 | I5 | 101 (5) |
| 00010000 | I4 | 100 (4) |
| 00001000 | I3 | 011 (3) |
| 00000100 | I2 | 010 (2) |
| 00000010 | I1 | 001 (1) |
| 00000001 | I0 | 000 (0) |
| 11111111 | I7 | 111 (7) |

## Architecture

Single layer with 3 neurons using weighted priority:

| Output | Function | Weights [I7..I0] | Bias |
|--------|----------|------------------|------|
| Y2 | I7∨I6∨I5∨I4 | [1,1,1,1,0,0,0,0] | -1 |
| Y1 | Priority bit 1 | [16,16,-4,-4,1,1,0,0] | -1 |
| Y0 | Priority bit 0 | [128,-64,32,-16,8,-4,2,0] | -1 |

Y1 and Y0 use weighted dominance: higher-priority inputs have larger weights
that override lower-priority inputs through the threshold mechanism.

## Parameters

| | |
|---|---|
| Inputs | 8 |
| Outputs | 3 |
| Neurons | 3 |
| Layers | 1 |
| Parameters | 27 |
| Magnitude | 303 |

## Usage

```python

from safetensors.torch import load_file

import torch



w = load_file('model.safetensors')



def encode8to3(i7, i6, i5, i4, i3, i2, i1, i0):

    inp = torch.tensor([float(i7), float(i6), float(i5), float(i4),

                        float(i3), float(i2), float(i1), float(i0)])

    y2 = int((inp @ w['y2.weight'].T + w['y2.bias'] >= 0).item())

    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 y2, y1, y0



# I5 is highest set bit

print(encode8to3(0, 0, 1, 0, 1, 0, 0, 0))  # (1, 0, 1) = 5

```

## License

MIT