File size: 1,970 Bytes
3f960d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
- encoding
---

# threshold-onehot-encoder

2-to-4 one-hot encoder. Converts a 2-bit binary value to a 4-bit one-hot representation.

## Function

onehot_encode(a1, a0) -> (y3, y2, y1, y0)

Exactly one output bit is set, corresponding to the input value.

## Truth Table

| a1 | a0 | y3 | y2 | y1 | y0 | Value |
|----|----|----|----|----|-----|-------|
| 0 | 0 | 0 | 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 | 1 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 | 0 | 2 |
| 1 | 1 | 1 | 0 | 0 | 0 | 3 |

## Architecture

Single-layer implementation using threshold logic:

```
        a1    a0
        β”‚     β”‚
    β”Œβ”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”
    β”‚             β”‚
    β–Ό   β–Ό   β–Ό   β–Ό
  β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
  β”‚y3 β”‚y2 β”‚y1 β”‚y0 β”‚  Layer 1
  β”‚ANDβ”‚AΒ·Bβ”‚AΒ·Bβ”‚NORβ”‚
  β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
    β”‚   β”‚   β”‚   β”‚
    β–Ό   β–Ό   β–Ό   β–Ό
```

Each output is a single threshold neuron:
- y0 = NOR(a1, a0): w=[-1,-1], b=0
- y1 = NOT(a1) AND a0: w=[-1,1], b=-1
- y2 = a1 AND NOT(a0): w=[1,-1], b=-1
- y3 = a1 AND a0: w=[1,1], b=-2

## Parameters

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

## Usage

```python
from safetensors.torch import load_file
import torch

w = load_file('model.safetensors')

def onehot(a1, a0):
    inp = torch.tensor([float(a1), float(a0)])
    y0 = int((inp @ w['y0.weight'].T + w['y0.bias'] >= 0).item())
    y1 = int((inp @ w['y1.weight'].T + w['y1.bias'] >= 0).item())
    y2 = int((inp @ w['y2.weight'].T + w['y2.bias'] >= 0).item())
    y3 = int((inp @ w['y3.weight'].T + w['y3.bias'] >= 0).item())
    return y3, y2, y1, y0

# onehot(1, 0) = (0, 1, 0, 0)  # value 2
```

## Applications

- Address decoding
- Memory select lines
- State machine encoding
- Neural network input preprocessing

## License

MIT