Upload folder using huggingface_hub
Browse files- README.md +81 -0
- config.json +9 -0
- create_safetensors.py +26 -0
- model.py +20 -0
- model.safetensors +3 -0
README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- pytorch
|
| 5 |
+
- safetensors
|
| 6 |
+
- threshold-logic
|
| 7 |
+
- neuromorphic
|
| 8 |
+
- voting
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# threshold-5outof6
|
| 12 |
+
|
| 13 |
+
At-least-5-of-6 threshold gate. Fires when 5 or more of the 6 inputs are high.
|
| 14 |
+
|
| 15 |
+
## Function
|
| 16 |
+
|
| 17 |
+
```
|
| 18 |
+
5outof6(x0, x1, x2, x3, x4, x5) = 1 if (x0 + x1 + x2 + x3 + x4 + x5) >= 5
|
| 19 |
+
```
|
| 20 |
+
|
| 21 |
+
## Truth Table (selected)
|
| 22 |
+
|
| 23 |
+
| Hamming Weight | Output |
|
| 24 |
+
|:--------------:|:------:|
|
| 25 |
+
| 0 | 0 |
|
| 26 |
+
| 1 | 0 |
|
| 27 |
+
| 2 | 0 |
|
| 28 |
+
| 3 | 0 |
|
| 29 |
+
| 4 | 0 |
|
| 30 |
+
| 5 | 1 |
|
| 31 |
+
| 6 | 1 |
|
| 32 |
+
|
| 33 |
+
## Mechanism
|
| 34 |
+
|
| 35 |
+
Single threshold neuron with uniform weights. The bias of -5 sets the firing threshold:
|
| 36 |
+
|
| 37 |
+
- Sum = (number of 1s) + (-5)
|
| 38 |
+
- Fires when sum >= 0, i.e., when Hamming weight >= 5
|
| 39 |
+
|
| 40 |
+
## k-out-of-6 Family
|
| 41 |
+
|
| 42 |
+
| Circuit | Bias | Fires when |
|
| 43 |
+
|---------|------|------------|
|
| 44 |
+
| 1-out-of-6 | -1 | HW >= 1 |
|
| 45 |
+
| 2-out-of-6 | -2 | HW >= 2 |
|
| 46 |
+
| 3-out-of-6 | -3 | HW >= 3 (majority) |
|
| 47 |
+
| 4-out-of-6 | -4 | HW >= 4 |
|
| 48 |
+
| 5-out-of-6 | -5 | HW >= 5 |
|
| 49 |
+
| 6-out-of-6 | -6 | HW = 6 (AND) |
|
| 50 |
+
|
| 51 |
+
## Parameters
|
| 52 |
+
|
| 53 |
+
| | |
|
| 54 |
+
|---|---|
|
| 55 |
+
| Weights | [1, 1, 1, 1, 1, 1] |
|
| 56 |
+
| Bias | -5 |
|
| 57 |
+
| Inputs | 6 |
|
| 58 |
+
| Outputs | 1 |
|
| 59 |
+
| Neurons | 1 |
|
| 60 |
+
| Layers | 1 |
|
| 61 |
+
| Parameters | 7 |
|
| 62 |
+
| Magnitude | 11 |
|
| 63 |
+
|
| 64 |
+
## Usage
|
| 65 |
+
|
| 66 |
+
```python
|
| 67 |
+
from safetensors.torch import load_file
|
| 68 |
+
import torch
|
| 69 |
+
|
| 70 |
+
w = load_file('model.safetensors')
|
| 71 |
+
|
| 72 |
+
def at_least_5_of_6(bits):
|
| 73 |
+
inputs = torch.tensor([float(b) for b in bits])
|
| 74 |
+
return int((inputs @ w['neuron.weight'].T + w['neuron.bias'] >= 0).item())
|
| 75 |
+
|
| 76 |
+
print(at_least_5_of_6([1, 1, 1, 1, 1, 0, 0, 0])) # 0 or 1 depending on k
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
## License
|
| 80 |
+
|
| 81 |
+
MIT
|
config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "threshold-5outof6",
|
| 3 |
+
"description": "At-least-5-of-6 threshold gate",
|
| 4 |
+
"inputs": 6,
|
| 5 |
+
"outputs": 1,
|
| 6 |
+
"neurons": 1,
|
| 7 |
+
"layers": 1,
|
| 8 |
+
"parameters": 7
|
| 9 |
+
}
|
create_safetensors.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file
|
| 3 |
+
|
| 4 |
+
weights = {
|
| 5 |
+
'neuron.weight': torch.tensor([[1.0, 1.0, 1.0, 1.0, 1.0, 1.0]], dtype=torch.float32),
|
| 6 |
+
'neuron.bias': torch.tensor([-5.0], dtype=torch.float32)
|
| 7 |
+
}
|
| 8 |
+
save_file(weights, 'model.safetensors')
|
| 9 |
+
|
| 10 |
+
def test(bits):
|
| 11 |
+
inputs = torch.tensor([float(b) for b in bits])
|
| 12 |
+
return int((inputs @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
|
| 13 |
+
|
| 14 |
+
print("Verifying 5-out-of-6...")
|
| 15 |
+
errors = 0
|
| 16 |
+
for i in range(64):
|
| 17 |
+
bits = [(i >> j) & 1 for j in range(6)]
|
| 18 |
+
result = test(bits)
|
| 19 |
+
expected = 1 if sum(bits) >= 5 else 0
|
| 20 |
+
if result != expected:
|
| 21 |
+
errors += 1
|
| 22 |
+
if errors == 0:
|
| 23 |
+
print("All 64 test cases passed!")
|
| 24 |
+
else:
|
| 25 |
+
print(f"FAILED: {errors} errors")
|
| 26 |
+
print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")
|
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 at_least_5_of_6(bits, weights):
|
| 8 |
+
"""Returns 1 if at least 5 of 6 inputs are high."""
|
| 9 |
+
inputs = torch.tensor([float(b) for b in bits])
|
| 10 |
+
return int((inputs @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
|
| 11 |
+
|
| 12 |
+
if __name__ == '__main__':
|
| 13 |
+
w = load_model()
|
| 14 |
+
print('5-out-of-6 truth table by Hamming weight:')
|
| 15 |
+
for hw in range(7):
|
| 16 |
+
bits = [1 if i < hw else 0 for i in range(6)]
|
| 17 |
+
result = at_least_5_of_6(bits, w)
|
| 18 |
+
expected = 1 if hw >= 5 else 0
|
| 19 |
+
status = 'OK' if result == expected else 'FAIL'
|
| 20 |
+
print(f' HW={hw}: {result} (expected {expected}) {status}')
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:66a7df3072cfc96fa32d074e7a8602f72d81199f19cc61dd72b473b9441b6f81
|
| 3 |
+
size 172
|