phanerozoic commited on
Commit
3ded640
·
verified ·
1 Parent(s): 32545fd

Rename from tiny-OR-verified

Browse files
Files changed (4) hide show
  1. README.md +82 -0
  2. config.json +21 -0
  3. model.py +61 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-or
11
+
12
+ A 1-of-2 threshold gate. One active input is enough to fire.
13
+
14
+ ## Circuit
15
+
16
+ ```
17
+ x y
18
+ │ │
19
+ └─┬─┘
20
+
21
+ ┌───────┐
22
+ │ w: 1,1│
23
+ │ b: -1 │
24
+ └───────┘
25
+
26
+
27
+ OR(x,y)
28
+ ```
29
+
30
+ ## Mechanism
31
+
32
+ Same weights as AND, but bias -1 instead of -2. The lower bar means a single vote suffices:
33
+
34
+ | x | y | sum | output |
35
+ |---|---|-----|--------|
36
+ | 0 | 0 | -1 | 0 |
37
+ | 0 | 1 | 0 | 1 |
38
+ | 1 | 0 | 0 | 1 |
39
+ | 1 | 1 | 1 | 1 |
40
+
41
+ AND and OR are the same circuit with different thresholds. This is the essence of threshold logic: the bias determines how many inputs must agree.
42
+
43
+ ## Parameters
44
+
45
+ | | |
46
+ |---|---|
47
+ | Weights | [1, 1] |
48
+ | Bias | -1 |
49
+ | Total | 3 parameters |
50
+
51
+ ## Properties
52
+
53
+ - Linearly separable
54
+ - De Morgan dual: OR(x,y) = NOT(AND(NOT(x), NOT(y)))
55
+ - Generalizes to n-input OR with weights all 1, bias -1
56
+
57
+ ## Usage
58
+
59
+ ```python
60
+ from safetensors.torch import load_file
61
+ import torch
62
+
63
+ w = load_file('model.safetensors')
64
+
65
+ def or_gate(x, y):
66
+ inputs = torch.tensor([float(x), float(y)])
67
+ return int((inputs * w['weight']).sum() + w['bias'] >= 0)
68
+ ```
69
+
70
+ ## Files
71
+
72
+ ```
73
+ threshold-or/
74
+ ├── model.safetensors
75
+ ├── model.py
76
+ ├── config.json
77
+ └── README.md
78
+ ```
79
+
80
+ ## License
81
+
82
+ MIT
config.json ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "threshold_network",
3
+ "task": "or_gate",
4
+ "architecture": "2 -> 1",
5
+ "input_size": 2,
6
+ "output_size": 1,
7
+ "num_neurons": 1,
8
+ "num_parameters": 3,
9
+ "activation": "heaviside",
10
+ "weight_constraints": "integer",
11
+ "verification": {
12
+ "method": "coq_proof",
13
+ "exhaustive": true,
14
+ "inputs_tested": 4
15
+ },
16
+ "accuracy": {
17
+ "all_inputs": "4/4",
18
+ "percentage": 100.0
19
+ },
20
+ "github": "https://github.com/CharlesCNorton/coq-circuits"
21
+ }
model.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Threshold Network for OR Gate
3
+
4
+ A formally verified single-neuron threshold network computing logical disjunction.
5
+ Weights are integer-constrained and activation uses the Heaviside step function.
6
+ """
7
+
8
+ import torch
9
+ from safetensors.torch import load_file
10
+
11
+
12
+ class ThresholdOR:
13
+ """
14
+ OR gate implemented as a threshold neuron.
15
+
16
+ Circuit: output = (w1*x1 + w2*x2 + bias >= 0)
17
+ With weights=[1,1], bias=-1: any input reaches threshold.
18
+ """
19
+
20
+ def __init__(self, weights_dict):
21
+ self.weight = weights_dict['weight']
22
+ self.bias = weights_dict['bias']
23
+
24
+ def __call__(self, x1, x2):
25
+ inputs = torch.tensor([float(x1), float(x2)])
26
+ weighted_sum = (inputs * self.weight).sum() + self.bias
27
+ return (weighted_sum >= 0).float()
28
+
29
+ @classmethod
30
+ def from_safetensors(cls, path="model.safetensors"):
31
+ return cls(load_file(path))
32
+
33
+
34
+ def forward(x, weights):
35
+ """
36
+ Forward pass with Heaviside activation.
37
+
38
+ Args:
39
+ x: Input tensor of shape [..., 2]
40
+ weights: Dict with 'weight' and 'bias' tensors
41
+
42
+ Returns:
43
+ OR(x[0], x[1])
44
+ """
45
+ x = torch.as_tensor(x, dtype=torch.float32)
46
+ weighted_sum = (x * weights['weight']).sum(dim=-1) + weights['bias']
47
+ return (weighted_sum >= 0).float()
48
+
49
+
50
+ if __name__ == "__main__":
51
+ weights = load_file("model.safetensors")
52
+ model = ThresholdOR(weights)
53
+
54
+ print("OR Gate Truth Table:")
55
+ print("-" * 25)
56
+ for x1 in [0, 1]:
57
+ for x2 in [0, 1]:
58
+ out = int(model(x1, x2).item())
59
+ expected = x1 | x2
60
+ status = "OK" if out == expected else "FAIL"
61
+ print(f"OR({x1}, {x2}) = {out} [{status}]")
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:257890f45a8a039afb9d6d7f5627d9f8a78684ca98d5467eb2d76eb976d90d52
3
+ size 140