CharlesCNorton commited on
Commit
dbaf9f3
·
0 Parent(s):

Initial commit: 4-input AND threshold gate

Browse files

Single-neuron circuit with weights [1,1,1,1] and bias -4.
Magnitude 8, verified 16/16 inputs correct.

Files changed (5) hide show
  1. README.md +102 -0
  2. config.json +19 -0
  3. create_safetensors.py +13 -0
  4. model.py +67 -0
  5. model.safetensors +0 -0
README.md ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-and4
11
+
12
+ A 4-of-4 threshold gate. All four inputs must be active to reach the firing threshold.
13
+
14
+ ## Circuit
15
+
16
+ ```
17
+ x1 x2 x3 x4
18
+ │ │ │ │
19
+ └───┴───┴───┘
20
+
21
+
22
+ ┌─────────┐
23
+ │w: 1,1,1,1│
24
+ │ b: -4 │
25
+ └─────────┘
26
+
27
+
28
+ AND4(x1,x2,x3,x4)
29
+ ```
30
+
31
+ ## Mechanism
32
+
33
+ Each input contributes +1 to the sum. The bias of -4 means exactly four contributions are required to reach zero:
34
+
35
+ | x1 | x2 | x3 | x4 | sum | output |
36
+ |----|----|----|-----|-----|--------|
37
+ | 0 | 0 | 0 | 0 | -4 | 0 |
38
+ | 0 | 0 | 0 | 1 | -3 | 0 |
39
+ | ... | ... | ... | ... | ... | 0 |
40
+ | 1 | 1 | 1 | 0 | -1 | 0 |
41
+ | 1 | 1 | 1 | 1 | 0 | 1 |
42
+
43
+ Only the all-ones input reaches the threshold.
44
+
45
+ ## Parameters
46
+
47
+ | | |
48
+ |---|---|
49
+ | Weights | [1, 1, 1, 1] |
50
+ | Bias | -4 |
51
+ | Magnitude | 8 |
52
+ | Total | 5 parameters |
53
+
54
+ ## Optimality
55
+
56
+ This circuit is at minimum magnitude (8). The pattern generalizes: n-input AND requires weights all 1 and bias -n, giving magnitude 2n.
57
+
58
+ | Gate | Weights | Bias | Magnitude |
59
+ |------|---------|------|-----------|
60
+ | AND2 | [1, 1] | -2 | 4 |
61
+ | AND3 | [1, 1, 1] | -3 | 6 |
62
+ | AND4 | [1, 1, 1, 1] | -4 | 8 |
63
+ | ANDn | [1, ..., 1] | -n | 2n |
64
+
65
+ ## Properties
66
+
67
+ - Linearly separable (single neuron suffices)
68
+ - Commutative, associative, idempotent
69
+ - De Morgan dual: AND4(x1,x2,x3,x4) = NOT(OR4(NOT(x1), NOT(x2), NOT(x3), NOT(x4)))
70
+
71
+ ## Usage
72
+
73
+ ```python
74
+ from safetensors.torch import load_file
75
+ import torch
76
+
77
+ w = load_file('model.safetensors')
78
+
79
+ def and4_gate(x1, x2, x3, x4):
80
+ inputs = torch.tensor([float(x1), float(x2), float(x3), float(x4)])
81
+ return int((inputs * w['weight']).sum() + w['bias'] >= 0)
82
+
83
+ # Test
84
+ assert and4_gate(1, 1, 1, 1) == 1
85
+ assert and4_gate(1, 1, 1, 0) == 0
86
+ assert and4_gate(0, 0, 0, 0) == 0
87
+ ```
88
+
89
+ ## Files
90
+
91
+ ```
92
+ threshold-and4/
93
+ ├── model.safetensors
94
+ ├── model.py
95
+ ├── config.json
96
+ ├── create_safetensors.py
97
+ └── README.md
98
+ ```
99
+
100
+ ## License
101
+
102
+ MIT
config.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "threshold_network",
3
+ "task": "and4_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([-4.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,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Threshold Network for 4-input AND Gate
3
+
4
+ A formally verified single-neuron threshold network computing 4-way logical conjunction.
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 ThresholdAND4:
13
+ """
14
+ 4-input AND gate implemented as a threshold neuron.
15
+
16
+ Circuit: output = (w1*x1 + w2*x2 + w3*x3 + w4*x4 + bias >= 0)
17
+ With weights=[1,1,1,1], bias=-4: only (1,1,1,1) 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, x3, x4):
25
+ inputs = torch.tensor([float(x1), float(x2), float(x3), float(x4)])
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 [..., 4]
40
+ weights: Dict with 'weight' and 'bias' tensors
41
+
42
+ Returns:
43
+ AND(x[0], x[1], x[2], x[3])
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 = ThresholdAND4(weights)
53
+
54
+ print("4-input AND Gate Truth Table:")
55
+ print("-" * 35)
56
+ correct = 0
57
+ for x1 in [0, 1]:
58
+ for x2 in [0, 1]:
59
+ for x3 in [0, 1]:
60
+ for x4 in [0, 1]:
61
+ out = int(model(x1, x2, x3, x4).item())
62
+ expected = x1 & x2 & x3 & x4
63
+ status = "OK" if out == expected else "FAIL"
64
+ if out == expected:
65
+ correct += 1
66
+ print(f"AND4({x1}, {x2}, {x3}, {x4}) = {out} [{status}]")
67
+ print(f"\nTotal: {correct}/16 correct")
model.safetensors ADDED
Binary file (148 Bytes). View file