CharlesCNorton commited on
Commit ·
2eff270
0
Parent(s):
1-bit less-than-or-equal comparator, magnitude 2
Browse files- README.md +64 -0
- config.json +9 -0
- create_safetensors.py +26 -0
- model.py +16 -0
- model.safetensors +0 -0
README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
# threshold-lessthanorequal
|
| 11 |
+
|
| 12 |
+
1-bit less-than-or-equal comparator. Outputs 1 when a <= b.
|
| 13 |
+
|
| 14 |
+
## Function
|
| 15 |
+
|
| 16 |
+
lte(a, b) = 1 if a <= b, else 0
|
| 17 |
+
|
| 18 |
+
Equivalent to: NOT(a) OR b (implication)
|
| 19 |
+
|
| 20 |
+
## Truth Table
|
| 21 |
+
|
| 22 |
+
| a | b | a <= b |
|
| 23 |
+
|---|---|--------|
|
| 24 |
+
| 0 | 0 | 1 |
|
| 25 |
+
| 0 | 1 | 1 |
|
| 26 |
+
| 1 | 0 | 0 |
|
| 27 |
+
| 1 | 1 | 1 |
|
| 28 |
+
|
| 29 |
+
## Architecture
|
| 30 |
+
|
| 31 |
+
Single neuron: weights [-1, 1], bias 0
|
| 32 |
+
|
| 33 |
+
Fires when: -a + b >= 0, i.e., b >= a
|
| 34 |
+
|
| 35 |
+
## Parameters
|
| 36 |
+
|
| 37 |
+
| | |
|
| 38 |
+
|---|---|
|
| 39 |
+
| Inputs | 2 |
|
| 40 |
+
| Outputs | 1 |
|
| 41 |
+
| Neurons | 1 |
|
| 42 |
+
| Layers | 1 |
|
| 43 |
+
| Parameters | 3 |
|
| 44 |
+
| Magnitude | 2 |
|
| 45 |
+
|
| 46 |
+
## Usage
|
| 47 |
+
|
| 48 |
+
```python
|
| 49 |
+
from safetensors.torch import load_file
|
| 50 |
+
import torch
|
| 51 |
+
|
| 52 |
+
w = load_file('model.safetensors')
|
| 53 |
+
|
| 54 |
+
def lte(a, b):
|
| 55 |
+
inp = torch.tensor([float(a), float(b)])
|
| 56 |
+
return int((inp @ w['neuron.weight'].T + w['neuron.bias'] >= 0).item())
|
| 57 |
+
|
| 58 |
+
print(lte(0, 1)) # 1
|
| 59 |
+
print(lte(1, 0)) # 0
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
## License
|
| 63 |
+
|
| 64 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-lessthanorequal",
|
| 3 |
+
"description": "1-bit less-than-or-equal comparator",
|
| 4 |
+
"inputs": 2,
|
| 5 |
+
"outputs": 1,
|
| 6 |
+
"neurons": 1,
|
| 7 |
+
"layers": 1,
|
| 8 |
+
"parameters": 3
|
| 9 |
+
}
|
create_safetensors.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file
|
| 3 |
+
|
| 4 |
+
# a <= b is equivalent to -a + b >= 0
|
| 5 |
+
weights = {
|
| 6 |
+
'neuron.weight': torch.tensor([[-1.0, 1.0]], dtype=torch.float32),
|
| 7 |
+
'neuron.bias': torch.tensor([0.0], dtype=torch.float32)
|
| 8 |
+
}
|
| 9 |
+
save_file(weights, 'model.safetensors')
|
| 10 |
+
|
| 11 |
+
def lte(a, b):
|
| 12 |
+
inp = torch.tensor([float(a), float(b)])
|
| 13 |
+
return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
|
| 14 |
+
|
| 15 |
+
print("Verifying lessthanorequal...")
|
| 16 |
+
errors = 0
|
| 17 |
+
for a in [0, 1]:
|
| 18 |
+
for b in [0, 1]:
|
| 19 |
+
result = lte(a, b)
|
| 20 |
+
expected = 1 if a <= b else 0
|
| 21 |
+
if result != expected:
|
| 22 |
+
errors += 1
|
| 23 |
+
print(f"ERROR: {a} <= {b} -> {result}, expected {expected}")
|
| 24 |
+
if errors == 0:
|
| 25 |
+
print("All 4 test cases passed!")
|
| 26 |
+
print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")
|
model.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 lte(a, b, weights):
|
| 8 |
+
inp = torch.tensor([float(a), float(b)])
|
| 9 |
+
return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
|
| 10 |
+
|
| 11 |
+
if __name__ == '__main__':
|
| 12 |
+
w = load_model()
|
| 13 |
+
print('lessthanorequal truth table:')
|
| 14 |
+
for a in [0, 1]:
|
| 15 |
+
for b in [0, 1]:
|
| 16 |
+
print(f' {a} <= {b} -> {lte(a, b, w)}')
|
model.safetensors
ADDED
|
Binary file (156 Bytes). View file
|
|
|