--- 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