phanerozoic commited on
Commit
07d60e8
·
verified ·
1 Parent(s): 255afde

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. README.md +67 -0
  2. config.json +21 -0
  3. create_safetensors.py +21 -0
  4. model.py +50 -0
  5. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-xor4
11
+
12
+ 4-input XOR gate. Cascade of three standard XOR circuits (OR + NAND + AND).
13
+
14
+ ## Architecture
15
+
16
+ ```
17
+ a b c d
18
+ │ │ │ │
19
+ └──┴──┐ │ │
20
+ ▼ │ │
21
+ ┌─────┐ │ │
22
+ │ XOR │ │ │
23
+ └──┬──┘ │ │
24
+ └──┬─┘ │
25
+ ▼ │
26
+ ┌─────┐ │
27
+ │ XOR │ │
28
+ └──┬──┘ │
29
+ └────┬────┘
30
+
31
+ ┌─────┐
32
+ │ XOR │
33
+ └──┬──┘
34
+
35
+ XOR4(a,b,c,d)
36
+ ```
37
+
38
+ Each XOR uses the standard OR + NAND + AND structure:
39
+ - OR: w=[1,1], b=-1 (magnitude 3)
40
+ - NAND: w=[-1,-1], b=1 (magnitude 3)
41
+ - AND: w=[1,1], b=-2 (magnitude 4)
42
+
43
+ ## Parameters
44
+
45
+ | | |
46
+ |---|---|
47
+ | Neurons | 9 |
48
+ | Layers | 6 |
49
+ | Parameters | 27 |
50
+ | Magnitude | 30 |
51
+
52
+ ## Optimized Version
53
+
54
+ See `threshold-xor4-mag21` for a 30% magnitude reduction using optimized XOR blocks.
55
+
56
+ ## Usage
57
+
58
+ ```python
59
+ from safetensors.torch import load_file
60
+
61
+ w = load_file('model.safetensors')
62
+ # See model.py for implementation
63
+ ```
64
+
65
+ ## License
66
+
67
+ MIT
config.json ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "threshold_network",
3
+ "task": "xor4_gate",
4
+ "architecture": "4 -> 2 -> 1 -> 2 -> 1 -> 2 -> 1",
5
+ "input_size": 4,
6
+ "output_size": 1,
7
+ "num_neurons": 9,
8
+ "num_layers": 6,
9
+ "num_parameters": 27,
10
+ "magnitude": 30,
11
+ "activation": "heaviside",
12
+ "weight_constraints": "integer",
13
+ "verification": {
14
+ "method": "exhaustive",
15
+ "inputs_tested": 16
16
+ },
17
+ "accuracy": {
18
+ "all_inputs": "16/16",
19
+ "percentage": 100.0
20
+ }
21
+ }
create_safetensors.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ def xor_block(prefix):
5
+ return {
6
+ f'{prefix}.layer1.or.weight': torch.tensor([1.0, 1.0]),
7
+ f'{prefix}.layer1.or.bias': torch.tensor([-1.0]),
8
+ f'{prefix}.layer1.nand.weight': torch.tensor([-1.0, -1.0]),
9
+ f'{prefix}.layer1.nand.bias': torch.tensor([1.0]),
10
+ f'{prefix}.layer2.and.weight': torch.tensor([1.0, 1.0]),
11
+ f'{prefix}.layer2.and.bias': torch.tensor([-2.0]),
12
+ }
13
+
14
+ weights = {}
15
+ weights.update(xor_block('xor1'))
16
+ weights.update(xor_block('xor2'))
17
+ weights.update(xor_block('xor3'))
18
+
19
+ save_file(weights, 'model.safetensors')
20
+ mag = sum(t.abs().sum().item() for t in weights.values())
21
+ print(f'Created model.safetensors (magnitude {mag:.0f})')
model.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Threshold Network for 4-input XOR Gate
3
+
4
+ Cascade of three standard XORs (OR + NAND + AND structure).
5
+ """
6
+
7
+ import torch
8
+ from safetensors.torch import load_file
9
+
10
+
11
+ def xor2(x1, x2, w, prefix):
12
+ inp = torch.tensor([float(x1), float(x2)])
13
+ or_out = int((inp * w[f'{prefix}.layer1.or.weight']).sum() + w[f'{prefix}.layer1.or.bias'] >= 0)
14
+ nand_out = int((inp * w[f'{prefix}.layer1.nand.weight']).sum() + w[f'{prefix}.layer1.nand.bias'] >= 0)
15
+ h = torch.tensor([float(or_out), float(nand_out)])
16
+ return int((h * w[f'{prefix}.layer2.and.weight']).sum() + w[f'{prefix}.layer2.and.bias'] >= 0)
17
+
18
+
19
+ class ThresholdXOR4:
20
+ def __init__(self, weights_dict):
21
+ self.w = weights_dict
22
+
23
+ def __call__(self, a, b, c, d):
24
+ xor_ab = xor2(a, b, self.w, 'xor1')
25
+ xor_abc = xor2(xor_ab, c, self.w, 'xor2')
26
+ xor_abcd = xor2(xor_abc, d, self.w, 'xor3')
27
+ return float(xor_abcd)
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 = ThresholdXOR4(weights)
37
+
38
+ print("4-input XOR Gate:")
39
+ correct = 0
40
+ for a in [0, 1]:
41
+ for b in [0, 1]:
42
+ for c in [0, 1]:
43
+ for d in [0, 1]:
44
+ out = int(model(a, b, c, d))
45
+ expected = a ^ b ^ c ^ d
46
+ if out == expected:
47
+ correct += 1
48
+ status = "OK" if out == expected else "FAIL"
49
+ print(f" XOR4({a},{b},{c},{d}) = {out} [{status}]")
50
+ print(f"Total: {correct}/16")
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e20bfd700009c0cd02ff8550bee0c1262fa773f59a2845d99fce96ff52485c1c
3
+ size 1468