phanerozoic commited on
Commit
5cc0d0b
Β·
verified Β·
1 Parent(s): c9976a5

Rename from tiny-7OutOf8-verified

Browse files
Files changed (4) hide show
  1. README.md +79 -0
  2. config.json +22 -0
  3. model.py +45 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-7outof8
11
+
12
+ Near-unanimity detector. Fires when 7 or more of 8 inputs are set.
13
+
14
+ ## Circuit
15
+
16
+ ```
17
+ xβ‚€ x₁ xβ‚‚ x₃ xβ‚„ xβ‚… x₆ x₇
18
+ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
19
+ β””β”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”Όβ”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”˜
20
+ β–Ό
21
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
22
+ β”‚ w: all 1β”‚
23
+ β”‚ b: -7 β”‚
24
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
25
+ β”‚
26
+ β–Ό
27
+ HW β‰₯ 7
28
+ ```
29
+
30
+ ## Mechanism
31
+
32
+ - Sum = (number of 1s) - 7
33
+ - Fires when Hamming weight β‰₯ 7
34
+
35
+ Tolerates exactly one missing input. Almost-AND.
36
+
37
+ ## k-out-of-8 Family
38
+
39
+ | Circuit | Bias | Fires when |
40
+ |---------|------|------------|
41
+ | ... | ... | ... |
42
+ | 6-out-of-8 | -6 | HW β‰₯ 6 |
43
+ | **7-out-of-8** | **-7** | **HW β‰₯ 7 (this)** |
44
+ | 8-out-of-8 | -8 | HW = 8 |
45
+
46
+ ## Parameters
47
+
48
+ | | |
49
+ |---|---|
50
+ | Weights | [1, 1, 1, 1, 1, 1, 1, 1] |
51
+ | Bias | -7 |
52
+ | Total | 9 parameters |
53
+
54
+ ## Usage
55
+
56
+ ```python
57
+ from safetensors.torch import load_file
58
+ import torch
59
+
60
+ w = load_file('model.safetensors')
61
+
62
+ def at_least_7(bits):
63
+ inputs = torch.tensor([float(b) for b in bits])
64
+ return int((inputs * w['weight']).sum() + w['bias'] >= 0)
65
+ ```
66
+
67
+ ## Files
68
+
69
+ ```
70
+ threshold-7outof8/
71
+ β”œβ”€β”€ model.safetensors
72
+ β”œβ”€β”€ model.py
73
+ β”œβ”€β”€ config.json
74
+ └── README.md
75
+ ```
76
+
77
+ ## License
78
+
79
+ MIT
config.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "threshold_network",
3
+ "task": "7_out_of_8_threshold",
4
+ "architecture": "8 -> 1",
5
+ "input_size": 8,
6
+ "output_size": 1,
7
+ "num_neurons": 1,
8
+ "num_parameters": 9,
9
+ "threshold": 7,
10
+ "activation": "heaviside",
11
+ "weight_constraints": "integer",
12
+ "verification": {
13
+ "method": "coq_proof",
14
+ "exhaustive": true,
15
+ "inputs_tested": 256
16
+ },
17
+ "accuracy": {
18
+ "all_inputs": "256/256",
19
+ "percentage": 100.0
20
+ },
21
+ "github": "https://github.com/CharlesCNorton/coq-circuits"
22
+ }
model.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Threshold Network for 7-out-of-8 Gate
3
+
4
+ A formally verified single-neuron threshold network.
5
+ Outputs 1 when at least 7 of the 8 inputs are true.
6
+ """
7
+
8
+ import torch
9
+ from safetensors.torch import load_file
10
+
11
+
12
+ class Threshold7OutOf8:
13
+ """
14
+ 7-out-of-8 threshold gate.
15
+
16
+ Circuit: output = (sum of inputs - 7 >= 0)
17
+ Fires when hamming weight >= 7.
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, bits):
25
+ inputs = torch.tensor([float(b) for b in bits])
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
+ if __name__ == "__main__":
35
+ weights = load_file("model.safetensors")
36
+ model = Threshold7OutOf8(weights)
37
+
38
+ print("7-out-of-8 Gate Tests:")
39
+ print("-" * 35)
40
+ for hw in range(9):
41
+ bits = [1]*hw + [0]*(8-hw)
42
+ out = int(model(bits).item())
43
+ expected = 1 if hw >= 7 else 0
44
+ status = "OK" if out == expected else "FAIL"
45
+ print(f"HW={hw}: {out} [{status}]")
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:33245de1a9f9a276352616ca5058a221f2553da656d9be52a90386fff05731ee
3
+ size 164