File size: 1,978 Bytes
4ee35bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
---

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


# threshold-onehot-decoder

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

## Function

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



Input must have exactly one bit set.



## Truth Table



| y3 | y2 | y1 | y0 | a1 | a0 | Value |

|----|----|----|----|----|-----|-------|

| 0 | 0 | 0 | 1 | 0 | 0 | 0 |

| 0 | 0 | 1 | 0 | 0 | 1 | 1 |

| 0 | 1 | 0 | 0 | 1 | 0 | 2 |

| 1 | 0 | 0 | 0 | 1 | 1 | 3 |



## Architecture



Single-layer implementation:



```

  y3   y2   y1   y0

   β”‚    β”‚    β”‚    β”‚

   β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”˜

         β”‚

    β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”

    β”‚         β”‚

    β–Ό         β–Ό

  β”Œβ”€β”€β”€β”     β”Œβ”€β”€β”€β”

  β”‚a1 β”‚     β”‚a0 β”‚  Layer 1

  β”‚OR β”‚     β”‚OR β”‚

  β””β”€β”€β”€β”˜     β””β”€β”€β”€β”˜

    β”‚         β”‚

    β–Ό         β–Ό

```



Each output is a single OR gate:

- a0 = OR(y1, y3): w=[1,0,1,0], b=-1

- a1 = OR(y2, y3): w=[1,1,0,0], b=-1



The decoder recognizes that:

- Bit 0 of position = 1 for positions 1 and 3

- Bit 1 of position = 1 for positions 2 and 3



## Parameters



| | |

|---|---|

| Inputs | 4 |

| Outputs | 2 |

| Neurons | 2 |

| Layers | 1 |

| Parameters | 10 |

| Magnitude | 6 |



## Usage



```python

from safetensors.torch import load_file
import torch

w = load_file('model.safetensors')



def decode(y3, y2, y1, y0):

    inp = torch.tensor([float(y3), float(y2), float(y1), float(y0)])

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

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

    return a1, a0



# decode(0, 1, 0, 0) = (1, 0)  # value 2

```



## Applications



- Priority encoder output processing

- State machine decoding

- Memory address reconstruction

- Neural network output interpretation



## License



MIT