threshold-mux / README.md
phanerozoic's picture
Add threshold-mux: 2:1 multiplexer
ea99dfa verified
---
license: mit
tags:
- pytorch
- safetensors
- threshold-logic
- neuromorphic
---
# threshold-mux
2:1 multiplexer. Selects between two inputs based on a select signal.
## Circuit
```
a b s
β”‚ β”‚ β”‚
β”‚ └───┼───┐
β””β”€β”€β”€β”¬β”€β”€β”€β”˜ β”‚
β”‚ β”Œβ”€β”€β”€β”˜
β–Ό β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ a AND Β¬s β”‚ N1: w=[1,0,-1] b=-1
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”‚ b AND s β”‚ N2: w=[0,1,1] b=-2
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚
β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ OR β”‚ w=[1,1] b=-1
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
output
```
## Function
MUX(a, b, s) = a if s=0, b if s=1
Equivalent to: OR(AND(a, NOT(s)), AND(b, s))
## Truth Table
| a | b | s | out |
|---|---|---|-----|
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 0 |
| 0 | 1 | 1 | 1 |
| 1 | 0 | 0 | 1 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 |
## Architecture
| Layer | Neurons | Weights | Bias |
|-------|---------|---------|------|
| 1 | N1 (a AND Β¬s) | [1, 0, -1] | -1 |
| 1 | N2 (b AND s) | [0, 1, 1] | -2 |
| 2 | OR | [1, 1] | -1 |
**Total: 3 neurons, 11 parameters, 2 layers**
## Usage
```python
from safetensors.torch import load_file
import torch
w = load_file('model.safetensors')
def mux(a, b, s):
inp = torch.tensor([float(a), float(b), float(s)])
l1 = (inp @ w['layer1.weight'].T + w['layer1.bias'] >= 0).float()
out = (l1 @ w['layer2.weight'].T + w['layer2.bias'] >= 0).float()
return int(out.item())
print(mux(1, 0, 0)) # 1 (selects a)
print(mux(1, 0, 1)) # 0 (selects b)
```
## Files
```
threshold-mux/
β”œβ”€β”€ model.safetensors
β”œβ”€β”€ model.py
β”œβ”€β”€ config.json
└── README.md
```
## License
MIT