Add threshold-mux: 2:1 multiplexer
Browse files- README.md +100 -0
- config.json +9 -0
- model.py +21 -0
- model.safetensors +3 -0
README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# threshold-mux
|
| 11 |
+
|
| 12 |
+
2:1 multiplexer. Selects between two inputs based on a select signal.
|
| 13 |
+
|
| 14 |
+
## Circuit
|
| 15 |
+
|
| 16 |
+
```
|
| 17 |
+
a b s
|
| 18 |
+
β β β
|
| 19 |
+
β βββββΌββββ
|
| 20 |
+
βββββ¬ββββ β
|
| 21 |
+
β βββββ
|
| 22 |
+
βΌ βΌ
|
| 23 |
+
βββββββββββββββ
|
| 24 |
+
β a AND Β¬s β N1: w=[1,0,-1] b=-1
|
| 25 |
+
βββββββββββββββ
|
| 26 |
+
β
|
| 27 |
+
β βββββββββββββββ
|
| 28 |
+
β β b AND s β N2: w=[0,1,1] b=-2
|
| 29 |
+
β βββββββββββββββ
|
| 30 |
+
β β
|
| 31 |
+
ββββββ¬βββββ
|
| 32 |
+
βΌ
|
| 33 |
+
βββββββββββ
|
| 34 |
+
β OR β w=[1,1] b=-1
|
| 35 |
+
βββββββββββ
|
| 36 |
+
β
|
| 37 |
+
βΌ
|
| 38 |
+
output
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
## Function
|
| 42 |
+
|
| 43 |
+
MUX(a, b, s) = a if s=0, b if s=1
|
| 44 |
+
|
| 45 |
+
Equivalent to: OR(AND(a, NOT(s)), AND(b, s))
|
| 46 |
+
|
| 47 |
+
## Truth Table
|
| 48 |
+
|
| 49 |
+
| a | b | s | out |
|
| 50 |
+
|---|---|---|-----|
|
| 51 |
+
| 0 | 0 | 0 | 0 |
|
| 52 |
+
| 0 | 0 | 1 | 0 |
|
| 53 |
+
| 0 | 1 | 0 | 0 |
|
| 54 |
+
| 0 | 1 | 1 | 1 |
|
| 55 |
+
| 1 | 0 | 0 | 1 |
|
| 56 |
+
| 1 | 0 | 1 | 0 |
|
| 57 |
+
| 1 | 1 | 0 | 1 |
|
| 58 |
+
| 1 | 1 | 1 | 1 |
|
| 59 |
+
|
| 60 |
+
## Architecture
|
| 61 |
+
|
| 62 |
+
| Layer | Neurons | Weights | Bias |
|
| 63 |
+
|-------|---------|---------|------|
|
| 64 |
+
| 1 | N1 (a AND Β¬s) | [1, 0, -1] | -1 |
|
| 65 |
+
| 1 | N2 (b AND s) | [0, 1, 1] | -2 |
|
| 66 |
+
| 2 | OR | [1, 1] | -1 |
|
| 67 |
+
|
| 68 |
+
**Total: 3 neurons, 11 parameters, 2 layers**
|
| 69 |
+
|
| 70 |
+
## Usage
|
| 71 |
+
|
| 72 |
+
```python
|
| 73 |
+
from safetensors.torch import load_file
|
| 74 |
+
import torch
|
| 75 |
+
|
| 76 |
+
w = load_file('model.safetensors')
|
| 77 |
+
|
| 78 |
+
def mux(a, b, s):
|
| 79 |
+
inp = torch.tensor([float(a), float(b), float(s)])
|
| 80 |
+
l1 = (inp @ w['layer1.weight'].T + w['layer1.bias'] >= 0).float()
|
| 81 |
+
out = (l1 @ w['layer2.weight'].T + w['layer2.bias'] >= 0).float()
|
| 82 |
+
return int(out.item())
|
| 83 |
+
|
| 84 |
+
print(mux(1, 0, 0)) # 1 (selects a)
|
| 85 |
+
print(mux(1, 0, 1)) # 0 (selects b)
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
## Files
|
| 89 |
+
|
| 90 |
+
```
|
| 91 |
+
threshold-mux/
|
| 92 |
+
βββ model.safetensors
|
| 93 |
+
βββ model.py
|
| 94 |
+
βββ config.json
|
| 95 |
+
βββ README.md
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
## License
|
| 99 |
+
|
| 100 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-mux",
|
| 3 |
+
"description": "2:1 multiplexer as threshold circuit",
|
| 4 |
+
"inputs": 3,
|
| 5 |
+
"outputs": 1,
|
| 6 |
+
"neurons": 3,
|
| 7 |
+
"layers": 2,
|
| 8 |
+
"parameters": 11
|
| 9 |
+
}
|
model.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import load_file
|
| 3 |
+
|
| 4 |
+
def load_model(path='model.safetensors'):
|
| 5 |
+
return load_file(path)
|
| 6 |
+
|
| 7 |
+
def mux(a, b, s, weights):
|
| 8 |
+
"""2:1 Multiplexer: returns a if s=0, b if s=1"""
|
| 9 |
+
inp = torch.tensor([float(a), float(b), float(s)])
|
| 10 |
+
l1 = (inp @ weights['layer1.weight'].T + weights['layer1.bias'] >= 0).float()
|
| 11 |
+
out = (l1 @ weights['layer2.weight'].T + weights['layer2.bias'] >= 0).float()
|
| 12 |
+
return int(out.item())
|
| 13 |
+
|
| 14 |
+
if __name__ == '__main__':
|
| 15 |
+
w = load_model()
|
| 16 |
+
print('MUX truth table:')
|
| 17 |
+
for a in [0, 1]:
|
| 18 |
+
for b in [0, 1]:
|
| 19 |
+
for s in [0, 1]:
|
| 20 |
+
result = mux(a, b, s, w)
|
| 21 |
+
print(f'MUX({a}, {b}, s={s}) = {result}')
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4b0a8e636796b10271048c4580602c5b3225013122155b8ca5bad13e7fd3e665
|
| 3 |
+
size 324
|