CharlesCNorton commited on
Commit ·
0910cb4
0
Parent(s):
Add weighted threshold circuit
Browse files1 neuron, 1 layer, 5 parameters, magnitude 16.
- .gitattributes +1 -0
- README.md +93 -0
- config.json +9 -0
- create_safetensors.py +57 -0
- model.py +20 -0
- model.safetensors +3 -0
.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
- weighted-voting
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# threshold-weighted
|
| 12 |
+
|
| 13 |
+
Weighted threshold function demonstrating non-uniform input weights.
|
| 14 |
+
|
| 15 |
+
## Function
|
| 16 |
+
|
| 17 |
+
y = 1 iff 4·x3 + 3·x2 + 2·x1 + 1·x0 >= 6
|
| 18 |
+
|
| 19 |
+
Each input has a different "voting power":
|
| 20 |
+
- x3: weight 4 (most influential)
|
| 21 |
+
- x2: weight 3
|
| 22 |
+
- x1: weight 2
|
| 23 |
+
- x0: weight 1 (least influential)
|
| 24 |
+
|
| 25 |
+
Maximum weighted sum = 10, threshold = 6 (weighted majority).
|
| 26 |
+
|
| 27 |
+
## Truth Table (selected rows)
|
| 28 |
+
|
| 29 |
+
| x3 | x2 | x1 | x0 | w_sum | y |
|
| 30 |
+
|----|----|----|-----|-------|---|
|
| 31 |
+
| 0 | 0 | 0 | 0 | 0 | 0 |
|
| 32 |
+
| 0 | 1 | 1 | 1 | 6 | **1** |
|
| 33 |
+
| 1 | 0 | 0 | 0 | 4 | 0 |
|
| 34 |
+
| 1 | 0 | 1 | 0 | 6 | **1** |
|
| 35 |
+
| 1 | 1 | 0 | 0 | 7 | **1** |
|
| 36 |
+
| 1 | 1 | 1 | 1 | 10 | **1** |
|
| 37 |
+
|
| 38 |
+
Note: x3 alone (weight 4) isn't enough, but x3 + x1 (weight 6) passes.
|
| 39 |
+
|
| 40 |
+
## Architecture
|
| 41 |
+
|
| 42 |
+
Single threshold neuron:
|
| 43 |
+
|
| 44 |
+
```
|
| 45 |
+
x3 ──(×4)──┐
|
| 46 |
+
x2 ──(×3)──┼──► Σ ──► (≥6?) ──► y
|
| 47 |
+
x1 ──(×2)──┤
|
| 48 |
+
x0 ──(×1)──┘
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
## Parameters
|
| 52 |
+
|
| 53 |
+
| | |
|
| 54 |
+
|---|---|
|
| 55 |
+
| Inputs | 4 |
|
| 56 |
+
| Outputs | 1 |
|
| 57 |
+
| Neurons | 1 |
|
| 58 |
+
| Layers | 1 |
|
| 59 |
+
| Parameters | 5 |
|
| 60 |
+
| Magnitude | 16 |
|
| 61 |
+
|
| 62 |
+
## Theory
|
| 63 |
+
|
| 64 |
+
This is the fundamental building block of threshold logic. Any linearly separable Boolean function can be computed by a single weighted threshold neuron. Non-linearly-separable functions (like XOR) require multiple layers.
|
| 65 |
+
|
| 66 |
+
The general form: y = 1 iff Σ(wi·xi) >= θ
|
| 67 |
+
|
| 68 |
+
## Applications
|
| 69 |
+
|
| 70 |
+
- Weighted voting systems
|
| 71 |
+
- Credit scoring
|
| 72 |
+
- Risk assessment
|
| 73 |
+
- Neural network layers
|
| 74 |
+
|
| 75 |
+
## Usage
|
| 76 |
+
|
| 77 |
+
```python
|
| 78 |
+
from safetensors.torch import load_file
|
| 79 |
+
import torch
|
| 80 |
+
|
| 81 |
+
w = load_file('model.safetensors')
|
| 82 |
+
|
| 83 |
+
def weighted(x3, x2, x1, x0):
|
| 84 |
+
inp = torch.tensor([float(x3), float(x2), float(x1), float(x0)])
|
| 85 |
+
return int((inp @ w['y.weight'].T + w['y.bias'] >= 0).item())
|
| 86 |
+
|
| 87 |
+
# weighted(1, 0, 1, 0) = 1 # 4+2=6 >= 6
|
| 88 |
+
# weighted(1, 0, 0, 1) = 0 # 4+1=5 < 6
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
## License
|
| 92 |
+
|
| 93 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-weighted",
|
| 3 |
+
"description": "Weighted threshold function (4,3,2,1 >= 6)",
|
| 4 |
+
"inputs": 4,
|
| 5 |
+
"outputs": 1,
|
| 6 |
+
"neurons": 1,
|
| 7 |
+
"layers": 1,
|
| 8 |
+
"parameters": 5
|
| 9 |
+
}
|
create_safetensors.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file
|
| 3 |
+
|
| 4 |
+
weights = {}
|
| 5 |
+
|
| 6 |
+
# Weighted Threshold Function
|
| 7 |
+
# Inputs: x3, x2, x1, x0 with weights 4, 3, 2, 1
|
| 8 |
+
# Output: y = 1 iff 4*x3 + 3*x2 + 2*x1 + 1*x0 >= 6
|
| 9 |
+
#
|
| 10 |
+
# This represents weighted voting where different inputs have different influence.
|
| 11 |
+
# Maximum sum = 4+3+2+1 = 10
|
| 12 |
+
# Threshold = 6 (need majority of weighted votes)
|
| 13 |
+
#
|
| 14 |
+
# Single neuron implementation!
|
| 15 |
+
|
| 16 |
+
weights['y.weight'] = torch.tensor([[4.0, 3.0, 2.0, 1.0]], dtype=torch.float32)
|
| 17 |
+
weights['y.bias'] = torch.tensor([-6.0], dtype=torch.float32)
|
| 18 |
+
|
| 19 |
+
save_file(weights, 'model.safetensors')
|
| 20 |
+
|
| 21 |
+
def weighted_threshold(x3, x2, x1, x0):
|
| 22 |
+
inp = torch.tensor([float(x3), float(x2), float(x1), float(x0)])
|
| 23 |
+
y = int((inp @ weights['y.weight'].T + weights['y.bias'] >= 0).item())
|
| 24 |
+
return y
|
| 25 |
+
|
| 26 |
+
def reference(x3, x2, x1, x0):
|
| 27 |
+
weighted_sum = 4*x3 + 3*x2 + 2*x1 + 1*x0
|
| 28 |
+
return 1 if weighted_sum >= 6 else 0
|
| 29 |
+
|
| 30 |
+
print("Verifying Weighted Threshold (4,3,2,1 >= 6)...")
|
| 31 |
+
errors = 0
|
| 32 |
+
for i in range(16):
|
| 33 |
+
x3, x2, x1, x0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
|
| 34 |
+
result = weighted_threshold(x3, x2, x1, x0)
|
| 35 |
+
expected = reference(x3, x2, x1, x0)
|
| 36 |
+
if result != expected:
|
| 37 |
+
errors += 1
|
| 38 |
+
print(f"ERROR: ({x3},{x2},{x1},{x0}) -> {result}, expected {expected}")
|
| 39 |
+
|
| 40 |
+
if errors == 0:
|
| 41 |
+
print("All 16 test cases passed!")
|
| 42 |
+
else:
|
| 43 |
+
print(f"FAILED: {errors} errors")
|
| 44 |
+
|
| 45 |
+
print("\nTruth Table:")
|
| 46 |
+
print("x3 x2 x1 x0 | w_sum | y")
|
| 47 |
+
print("-" * 26)
|
| 48 |
+
for i in range(16):
|
| 49 |
+
x3, x2, x1, x0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
|
| 50 |
+
y = weighted_threshold(x3, x2, x1, x0)
|
| 51 |
+
ws = 4*x3 + 3*x2 + 2*x1 + 1*x0
|
| 52 |
+
print(f" {x3} {x2} {x1} {x0} | {ws:2d} | {y}")
|
| 53 |
+
|
| 54 |
+
mag = sum(t.abs().sum().item() for t in weights.values())
|
| 55 |
+
print(f"\nMagnitude: {mag:.0f}")
|
| 56 |
+
print(f"Parameters: {sum(t.numel() for t in weights.values())}")
|
| 57 |
+
print(f"Neurons: {len([k for k in weights.keys() if 'weight' in k])}")
|
model.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 weighted_threshold(x3, x2, x1, x0, weights):
|
| 8 |
+
"""Weighted threshold: 4*x3 + 3*x2 + 2*x1 + 1*x0 >= 6."""
|
| 9 |
+
inp = torch.tensor([float(x3), float(x2), float(x1), float(x0)])
|
| 10 |
+
y = int((inp @ weights['y.weight'].T + weights['y.bias'] >= 0).item())
|
| 11 |
+
return y
|
| 12 |
+
|
| 13 |
+
if __name__ == '__main__':
|
| 14 |
+
w = load_model()
|
| 15 |
+
print('Weighted Threshold (weights: 4,3,2,1, threshold: 6):')
|
| 16 |
+
for i in range(16):
|
| 17 |
+
x3, x2, x1, x0 = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
|
| 18 |
+
y = weighted_threshold(x3, x2, x1, x0, w)
|
| 19 |
+
ws = 4*x3 + 3*x2 + 2*x1 + 1*x0
|
| 20 |
+
print(f' {x3}{x2}{x1}{x0} (sum={ws:2d}) -> {y}')
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:baf960db67b51595173a3ee0dbc62325a01c1cbc699b389ca8234cf40e26b192
|
| 3 |
+
size 156
|