phanerozoic commited on
Commit
5a209f3
Β·
verified Β·
1 Parent(s): e08bf72

Rename from tiny-1OutOf8-verified

Browse files
Files changed (4) hide show
  1. README.md +83 -0
  2. config.json +22 -0
  3. model.py +51 -0
  4. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-1outof8
11
+
12
+ At-least-one detector for 8 inputs. Equivalent to an 8-input OR gate.
13
+
14
+ ## Circuit
15
+
16
+ ```
17
+ xβ‚€ x₁ xβ‚‚ x₃ xβ‚„ xβ‚… x₆ x₇
18
+ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚
19
+ β””β”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”Όβ”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”˜
20
+ β–Ό
21
+ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
22
+ β”‚ w: all 1β”‚
23
+ β”‚ b: -1 β”‚
24
+ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
25
+ β”‚
26
+ β–Ό
27
+ HW β‰₯ 1
28
+ ```
29
+
30
+ ## Mechanism
31
+
32
+ With uniform weights of 1 and bias -1, the neuron fires when at least one input is active:
33
+
34
+ - Sum = (number of 1s) - 1
35
+ - Fires when sum β‰₯ 0, i.e., when Hamming weight β‰₯ 1
36
+
37
+ This is the loosest threshold in the k-out-of-8 family.
38
+
39
+ ## k-out-of-8 Family
40
+
41
+ | Circuit | Bias | Fires when |
42
+ |---------|------|------------|
43
+ | **1-out-of-8** | **-1** | **HW β‰₯ 1 (this)** |
44
+ | 2-out-of-8 | -2 | HW β‰₯ 2 |
45
+ | ... | ... | ... |
46
+ | 8-out-of-8 | -8 | HW = 8 |
47
+
48
+ All share weights [1,1,1,1,1,1,1,1]. Only the bias differs.
49
+
50
+ ## Parameters
51
+
52
+ | | |
53
+ |---|---|
54
+ | Weights | [1, 1, 1, 1, 1, 1, 1, 1] |
55
+ | Bias | -1 |
56
+ | Total | 9 parameters |
57
+
58
+ ## Usage
59
+
60
+ ```python
61
+ from safetensors.torch import load_file
62
+ import torch
63
+
64
+ w = load_file('model.safetensors')
65
+
66
+ def at_least_1(bits):
67
+ inputs = torch.tensor([float(b) for b in bits])
68
+ return int((inputs * w['weight']).sum() + w['bias'] >= 0)
69
+ ```
70
+
71
+ ## Files
72
+
73
+ ```
74
+ threshold-1outof8/
75
+ β”œβ”€β”€ model.safetensors
76
+ β”œβ”€β”€ model.py
77
+ β”œβ”€β”€ config.json
78
+ └── README.md
79
+ ```
80
+
81
+ ## License
82
+
83
+ MIT
config.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "threshold_network",
3
+ "task": "1_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": 1,
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,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Threshold Network for 1-out-of-8 Gate
3
+
4
+ A formally verified single-neuron threshold network.
5
+ Outputs 1 when at least 1 of the 8 inputs is true (equivalent to 8-input OR).
6
+ """
7
+
8
+ import torch
9
+ from safetensors.torch import load_file
10
+
11
+
12
+ class Threshold1OutOf8:
13
+ """
14
+ 1-out-of-8 threshold gate.
15
+
16
+ Circuit: output = (sum of inputs - 1 >= 0)
17
+ Fires when hamming weight >= 1.
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
+ def forward(x, weights):
35
+ x = torch.as_tensor(x, dtype=torch.float32)
36
+ weighted_sum = (x * weights['weight']).sum(dim=-1) + weights['bias']
37
+ return (weighted_sum >= 0).float()
38
+
39
+
40
+ if __name__ == "__main__":
41
+ weights = load_file("model.safetensors")
42
+ model = Threshold1OutOf8(weights)
43
+
44
+ print("1-out-of-8 Gate Tests:")
45
+ print("-" * 35)
46
+ for hw in range(9):
47
+ bits = [1]*hw + [0]*(8-hw)
48
+ out = int(model(bits).item())
49
+ expected = 1 if hw >= 1 else 0
50
+ status = "OK" if out == expected else "FAIL"
51
+ 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:1a455027587d2caa06bbb964b1175a708df76fc849c753559303032cc9ba7851
3
+ size 164