| | --- |
| | license: mit |
| | tags: |
| | - pytorch |
| | - safetensors |
| | - threshold-logic |
| | - neuromorphic |
| | --- |
| | |
| | # threshold-and4 |
| |
|
| | A 4-of-4 threshold gate. All four inputs must be active to reach the firing threshold. |
| |
|
| | ## Circuit |
| |
|
| | ``` |
| | x1 x2 x3 x4 |
| | β β β β |
| | βββββ΄ββββ΄ββββ |
| | β |
| | βΌ |
| | βββββββββββ |
| | βw: 1,1,1,1β |
| | β b: -4 β |
| | βββββββββββ |
| | β |
| | βΌ |
| | AND4(x1,x2,x3,x4) |
| | ``` |
| |
|
| | ## Mechanism |
| |
|
| | Each input contributes +1 to the sum. The bias of -4 means exactly four contributions are required to reach zero: |
| |
|
| | | x1 | x2 | x3 | x4 | sum | output | |
| | |----|----|----|-----|-----|--------| |
| | | 0 | 0 | 0 | 0 | -4 | 0 | |
| | | 0 | 0 | 0 | 1 | -3 | 0 | |
| | | ... | ... | ... | ... | ... | 0 | |
| | | 1 | 1 | 1 | 0 | -1 | 0 | |
| | | 1 | 1 | 1 | 1 | 0 | 1 | |
| |
|
| | Only the all-ones input reaches the threshold. |
| |
|
| | ## Parameters |
| |
|
| | | | | |
| | |---|---| |
| | | Weights | [1, 1, 1, 1] | |
| | | Bias | -4 | |
| | | Magnitude | 8 | |
| | | Total | 5 parameters | |
| |
|
| | ## Optimality |
| |
|
| | This circuit is at minimum magnitude (8). The pattern generalizes: n-input AND requires weights all 1 and bias -n, giving magnitude 2n. |
| |
|
| | | Gate | Weights | Bias | Magnitude | |
| | |------|---------|------|-----------| |
| | | AND2 | [1, 1] | -2 | 4 | |
| | | AND3 | [1, 1, 1] | -3 | 6 | |
| | | AND4 | [1, 1, 1, 1] | -4 | 8 | |
| | | ANDn | [1, ..., 1] | -n | 2n | |
| |
|
| | ## Properties |
| |
|
| | - Linearly separable (single neuron suffices) |
| | - Commutative, associative, idempotent |
| | - De Morgan dual: AND4(x1,x2,x3,x4) = NOT(OR4(NOT(x1), NOT(x2), NOT(x3), NOT(x4))) |
| |
|
| | ## Usage |
| |
|
| | ```python |
| | from safetensors.torch import load_file |
| | import torch |
| | |
| | w = load_file('model.safetensors') |
| | |
| | def and4_gate(x1, x2, x3, x4): |
| | inputs = torch.tensor([float(x1), float(x2), float(x3), float(x4)]) |
| | return int((inputs * w['weight']).sum() + w['bias'] >= 0) |
| | |
| | # Test |
| | assert and4_gate(1, 1, 1, 1) == 1 |
| | assert and4_gate(1, 1, 1, 0) == 0 |
| | assert and4_gate(0, 0, 0, 0) == 0 |
| | ``` |
| |
|
| | ## Files |
| |
|
| | ``` |
| | threshold-and4/ |
| | βββ model.safetensors |
| | βββ model.py |
| | βββ config.json |
| | βββ create_safetensors.py |
| | βββ README.md |
| | ``` |
| |
|
| | ## License |
| |
|
| | MIT |
| |
|