phanerozoic commited on
Commit
4fcebd3
·
verified ·
1 Parent(s): e3d057e

Upload folder using huggingface_hub

Browse files
README.md ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-nand4
11
+
12
+ 4-input NAND gate. Outputs 0 only when all inputs are 1.
13
+
14
+ ## Circuit
15
+
16
+ ```
17
+ x1 x2 x3 x4
18
+ │ │ │ │
19
+ └───┴───┴───┘
20
+
21
+
22
+ ┌──────────┐
23
+ │w:-1,-1,-1,-1│
24
+ │ b: 3 │
25
+ └──────────┘
26
+
27
+
28
+ NAND4(x1,x2,x3,x4)
29
+ ```
30
+
31
+ ## Parameters
32
+
33
+ | | |
34
+ |---|---|
35
+ | Weights | [-1, -1, -1, -1] |
36
+ | Bias | 3 |
37
+ | Magnitude | 7 |
38
+
39
+ ## Optimality
40
+
41
+ Exhaustive enumeration of 7,183 configurations confirms magnitude 7 is optimal. 1 valid configuration exists.
42
+
43
+ | Magnitude | Valid Configs |
44
+ |-----------|---------------|
45
+ | 0-6 | 0 |
46
+ | 7 | 1 |
47
+
48
+ ## Usage
49
+
50
+ ```python
51
+ from safetensors.torch import load_file
52
+ import torch
53
+
54
+ w = load_file('model.safetensors')
55
+
56
+ def nand4(x1, x2, x3, x4):
57
+ inputs = torch.tensor([float(x1), float(x2), float(x3), float(x4)])
58
+ return int((inputs * w['weight']).sum() + w['bias'] >= 0)
59
+ ```
60
+
61
+ ## License
62
+
63
+ MIT
__pycache__/model.cpython-313.pyc ADDED
Binary file (2.4 kB). View file
 
config.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "threshold_network",
3
+ "task": "nand4_gate",
4
+ "architecture": "4 -> 1",
5
+ "input_size": 4,
6
+ "output_size": 1,
7
+ "num_neurons": 1,
8
+ "num_parameters": 5,
9
+ "activation": "heaviside",
10
+ "weight_constraints": "integer",
11
+ "verification": {
12
+ "method": "exhaustive",
13
+ "inputs_tested": 16
14
+ },
15
+ "accuracy": {
16
+ "all_inputs": "16/16",
17
+ "percentage": 100.0
18
+ }
19
+ }
create_safetensors.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ weights = {
5
+ 'weight': torch.tensor([-1.0, -1.0, -1.0, -1.0]),
6
+ 'bias': torch.tensor([3.0]),
7
+ }
8
+
9
+ save_file(weights, 'model.safetensors')
10
+ print('Created model.safetensors')
11
+ print(f' weight: {weights["weight"].tolist()}')
12
+ print(f' bias: {weights["bias"].item()}')
13
+ print(f' magnitude: {sum(abs(w) for w in weights["weight"].tolist()) + abs(weights["bias"].item()):.0f}')
model.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Threshold Network for 4-input NAND Gate
3
+ """
4
+
5
+ import torch
6
+ from safetensors.torch import load_file
7
+
8
+
9
+ class ThresholdNAND4:
10
+ def __init__(self, weights_dict):
11
+ self.weight = weights_dict['weight']
12
+ self.bias = weights_dict['bias']
13
+
14
+ def __call__(self, x1, x2, x3, x4):
15
+ inputs = torch.tensor([float(x1), float(x2), float(x3), float(x4)])
16
+ weighted_sum = (inputs * self.weight).sum() + self.bias
17
+ return (weighted_sum >= 0).float()
18
+
19
+ @classmethod
20
+ def from_safetensors(cls, path="model.safetensors"):
21
+ return cls(load_file(path))
22
+
23
+
24
+ if __name__ == "__main__":
25
+ weights = load_file("model.safetensors")
26
+ model = ThresholdNAND4(weights)
27
+
28
+ print("4-input NAND Gate Truth Table:")
29
+ print("-" * 35)
30
+ correct = 0
31
+ for x1 in [0, 1]:
32
+ for x2 in [0, 1]:
33
+ for x3 in [0, 1]:
34
+ for x4 in [0, 1]:
35
+ out = int(model(x1, x2, x3, x4).item())
36
+ expected = 1 - (x1 & x2 & x3 & x4)
37
+ status = "OK" if out == expected else "FAIL"
38
+ if out == expected:
39
+ correct += 1
40
+ print(f"NAND4({x1}, {x2}, {x3}, {x4}) = {out} [{status}]")
41
+ print(f"\nTotal: {correct}/16 correct")
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:55e8950d44e8e560b3aaa57e992904867a38321cd2a198a3d19934cedc796dd0
3
+ size 148