Upload folder using huggingface_hub
Browse files- README.md +114 -0
- config.json +9 -0
- model.py +18 -0
- model.safetensors +3 -0
README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
- functionally-complete
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# threshold-nand3
|
| 12 |
+
|
| 13 |
+
3-input NAND gate. Fires unless all three inputs are active. The universal gate extended.
|
| 14 |
+
|
| 15 |
+
## Circuit
|
| 16 |
+
|
| 17 |
+
```
|
| 18 |
+
a b c
|
| 19 |
+
β β β
|
| 20 |
+
βββββΌββββ
|
| 21 |
+
β
|
| 22 |
+
βΌ
|
| 23 |
+
ββββββββββββ
|
| 24 |
+
βw: -1,-1,-1β
|
| 25 |
+
β b: +2 β
|
| 26 |
+
ββββββββββββ
|
| 27 |
+
β
|
| 28 |
+
βΌ
|
| 29 |
+
NAND(a,b,c)
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
## The Veto Detector
|
| 33 |
+
|
| 34 |
+
3-input NAND fires when at least one input is 0:
|
| 35 |
+
|
| 36 |
+
| Inputs | Sum | Output |
|
| 37 |
+
|--------|-----|--------|
|
| 38 |
+
| 000 | +2 | 1 |
|
| 39 |
+
| 001 | +1 | 1 |
|
| 40 |
+
| 010 | +1 | 1 |
|
| 41 |
+
| 011 | 0 | 1 |
|
| 42 |
+
| 100 | +1 | 1 |
|
| 43 |
+
| 101 | 0 | 1 |
|
| 44 |
+
| 110 | 0 | 1 |
|
| 45 |
+
| **111** | **-1** | **0** |
|
| 46 |
+
|
| 47 |
+
Only unanimous activation silences the gate.
|
| 48 |
+
|
| 49 |
+
## Negative Weights
|
| 50 |
+
|
| 51 |
+
Each input *subtracts* from the sum. The positive bias provides headroom:
|
| 52 |
+
|
| 53 |
+
```
|
| 54 |
+
sum = -a - b - c + 2 = 2 - HW
|
| 55 |
+
fires when 2 - HW >= 0
|
| 56 |
+
fires when HW <= 2
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
This is AtMost2 for 3 inputs.
|
| 60 |
+
|
| 61 |
+
## Functional Completeness
|
| 62 |
+
|
| 63 |
+
NAND is universal - any Boolean function can be built from NAND:
|
| 64 |
+
|
| 65 |
+
- NOT(x) = NAND(x, x, x)
|
| 66 |
+
- AND(x,y,z) = NAND(NAND(x,y,z), NAND(x,y,z), NAND(x,y,z))
|
| 67 |
+
- OR(x,y,z) = NAND(NAND(x,x,x), NAND(y,y,y), NAND(z,z,z))
|
| 68 |
+
|
| 69 |
+
## Dual of 3-input NOR
|
| 70 |
+
|
| 71 |
+
| Gate | Weights | Bias | Fires when |
|
| 72 |
+
|------|---------|------|------------|
|
| 73 |
+
| **NAND3** | all -1 | +2 | HW β€ 2 |
|
| 74 |
+
| NOR3 | all -1 | 0 | HW = 0 |
|
| 75 |
+
|
| 76 |
+
NAND is permissive (7 of 8 pass). NOR is restrictive (1 of 8 passes).
|
| 77 |
+
|
| 78 |
+
## Parameters
|
| 79 |
+
|
| 80 |
+
| Component | Value |
|
| 81 |
+
|-----------|-------|
|
| 82 |
+
| Weights | [-1, -1, -1] |
|
| 83 |
+
| Bias | +2 |
|
| 84 |
+
| **Total** | **4 parameters** |
|
| 85 |
+
|
| 86 |
+
## Usage
|
| 87 |
+
|
| 88 |
+
```python
|
| 89 |
+
from safetensors.torch import load_file
|
| 90 |
+
import torch
|
| 91 |
+
|
| 92 |
+
w = load_file('model.safetensors')
|
| 93 |
+
|
| 94 |
+
def nand3(a, b, c):
|
| 95 |
+
inp = torch.tensor([float(a), float(b), float(c)])
|
| 96 |
+
return int((inp * w['weight']).sum() + w['bias'] >= 0)
|
| 97 |
+
|
| 98 |
+
print(nand3(1, 1, 0)) # 1
|
| 99 |
+
print(nand3(1, 1, 1)) # 0
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
## Files
|
| 103 |
+
|
| 104 |
+
```
|
| 105 |
+
threshold-nand3/
|
| 106 |
+
βββ model.safetensors
|
| 107 |
+
βββ model.py
|
| 108 |
+
βββ config.json
|
| 109 |
+
βββ README.md
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
## License
|
| 113 |
+
|
| 114 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-nand3",
|
| 3 |
+
"description": "3-input NAND gate as threshold circuit",
|
| 4 |
+
"inputs": 3,
|
| 5 |
+
"outputs": 1,
|
| 6 |
+
"neurons": 1,
|
| 7 |
+
"layers": 1,
|
| 8 |
+
"parameters": 4
|
| 9 |
+
}
|
model.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 nand3(a, b, c, weights):
|
| 8 |
+
"""3-input NAND gate."""
|
| 9 |
+
inp = torch.tensor([float(a), float(b), float(c)])
|
| 10 |
+
return int((inp * weights['weight']).sum() + weights['bias'] >= 0)
|
| 11 |
+
|
| 12 |
+
if __name__ == '__main__':
|
| 13 |
+
w = load_model()
|
| 14 |
+
print('3-input NAND truth table:')
|
| 15 |
+
for a in [0, 1]:
|
| 16 |
+
for b in [0, 1]:
|
| 17 |
+
for c in [0, 1]:
|
| 18 |
+
print(f'NAND({a},{b},{c}) = {nand3(a, b, c, w)}')
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9f4b18d4677acf48bb022a38bcf0fc5db72c5019c72ccba08f7d02d803e86f43
|
| 3 |
+
size 144
|