phanerozoic commited on
Commit
57b22fc
·
verified ·
1 Parent(s): 3b604a3

Upload folder using huggingface_hub

Browse files
README.md ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-or4
11
+
12
+ A 1-of-4 threshold gate. Any single active input is enough to fire.
13
+
14
+ ## Circuit
15
+
16
+ ```
17
+ x1 x2 x3 x4
18
+ │ │ │ │
19
+ └───┴───┴───┘
20
+
21
+
22
+ ┌─────────┐
23
+ │w: 1,1,1,1│
24
+ │ b: -1 │
25
+ └─────────┘
26
+
27
+
28
+ OR4(x1,x2,x3,x4)
29
+ ```
30
+
31
+ ## Mechanism
32
+
33
+ Same weights as AND4, but bias -1 instead of -4. A single vote suffices:
34
+
35
+ | x1 | x2 | x3 | x4 | sum | output |
36
+ |----|----|----|-----|-----|--------|
37
+ | 0 | 0 | 0 | 0 | -1 | 0 |
38
+ | 0 | 0 | 0 | 1 | 0 | 1 |
39
+ | ... | ... | ... | ... | ... | 1 |
40
+ | 1 | 1 | 1 | 1 | 3 | 1 |
41
+
42
+ ## Parameters
43
+
44
+ | | |
45
+ |---|---|
46
+ | Weights | [1, 1, 1, 1] |
47
+ | Bias | -1 |
48
+ | Magnitude | 5 |
49
+ | Total | 5 parameters |
50
+
51
+ ## Optimality
52
+
53
+ Exhaustive enumeration of all 1,683 weight configurations at magnitudes 0-5 confirms this circuit is **at minimum magnitude (5)**. There is exactly 1 valid configuration at magnitude 5, and no valid configurations exist below it.
54
+
55
+ | Magnitude | Valid Configs |
56
+ |-----------|---------------|
57
+ | 0-4 | 0 |
58
+ | 5 | 1 |
59
+
60
+ ## Properties
61
+
62
+ - Linearly separable (single neuron suffices)
63
+ - De Morgan dual: OR4(x1,x2,x3,x4) = NOT(AND4(NOT(x1), NOT(x2), NOT(x3), NOT(x4)))
64
+
65
+ ## Usage
66
+
67
+ ```python
68
+ from safetensors.torch import load_file
69
+ import torch
70
+
71
+ w = load_file('model.safetensors')
72
+
73
+ def or4_gate(x1, x2, x3, x4):
74
+ inputs = torch.tensor([float(x1), float(x2), float(x3), float(x4)])
75
+ return int((inputs * w['weight']).sum() + w['bias'] >= 0)
76
+
77
+ assert or4_gate(0, 0, 0, 0) == 0
78
+ assert or4_gate(0, 0, 0, 1) == 1
79
+ assert or4_gate(1, 1, 1, 1) == 1
80
+ ```
81
+
82
+ ## Files
83
+
84
+ ```
85
+ threshold-or4/
86
+ ├── model.safetensors
87
+ ├── model.py
88
+ ├── config.json
89
+ ├── create_safetensors.py
90
+ └── README.md
91
+ ```
92
+
93
+ ## License
94
+
95
+ MIT
__pycache__/model.cpython-313.pyc ADDED
Binary file (3.17 kB). View file
 
config.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "threshold_network",
3
+ "task": "or4_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([-1.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,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Threshold Network for 4-input OR Gate
3
+ """
4
+
5
+ import torch
6
+ from safetensors.torch import load_file
7
+
8
+
9
+ class ThresholdOR4:
10
+ """
11
+ 4-input OR gate implemented as a threshold neuron.
12
+
13
+ Circuit: output = (w1*x1 + w2*x2 + w3*x3 + w4*x4 + bias >= 0)
14
+ With weights=[1,1,1,1], bias=-1: any single input reaches threshold.
15
+ """
16
+
17
+ def __init__(self, weights_dict):
18
+ self.weight = weights_dict['weight']
19
+ self.bias = weights_dict['bias']
20
+
21
+ def __call__(self, x1, x2, x3, x4):
22
+ inputs = torch.tensor([float(x1), float(x2), float(x3), float(x4)])
23
+ weighted_sum = (inputs * self.weight).sum() + self.bias
24
+ return (weighted_sum >= 0).float()
25
+
26
+ @classmethod
27
+ def from_safetensors(cls, path="model.safetensors"):
28
+ return cls(load_file(path))
29
+
30
+
31
+ def forward(x, weights):
32
+ """
33
+ Forward pass with Heaviside activation.
34
+
35
+ Args:
36
+ x: Input tensor of shape [..., 4]
37
+ weights: Dict with 'weight' and 'bias' tensors
38
+
39
+ Returns:
40
+ OR(x[0], x[1], x[2], x[3])
41
+ """
42
+ x = torch.as_tensor(x, dtype=torch.float32)
43
+ weighted_sum = (x * weights['weight']).sum(dim=-1) + weights['bias']
44
+ return (weighted_sum >= 0).float()
45
+
46
+
47
+ if __name__ == "__main__":
48
+ weights = load_file("model.safetensors")
49
+ model = ThresholdOR4(weights)
50
+
51
+ print("4-input OR Gate Truth Table:")
52
+ print("-" * 35)
53
+ correct = 0
54
+ for x1 in [0, 1]:
55
+ for x2 in [0, 1]:
56
+ for x3 in [0, 1]:
57
+ for x4 in [0, 1]:
58
+ out = int(model(x1, x2, x3, x4).item())
59
+ expected = x1 | x2 | x3 | x4
60
+ status = "OK" if out == expected else "FAIL"
61
+ if out == expected:
62
+ correct += 1
63
+ print(f"OR4({x1}, {x2}, {x3}, {x4}) = {out} [{status}]")
64
+ 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:d5e9dabafbb22f3cc942b6a2e9016a6dcb5bd6a7ca794aeebf12c47e4721b168
3
+ size 148