phanerozoic commited on
Commit
2b5226a
·
verified ·
1 Parent(s): cc61bcb

Upload folder using huggingface_hub

Browse files
Files changed (5) hide show
  1. README.md +44 -0
  2. config.json +9 -0
  3. create_safetensors.py +28 -0
  4. model.py +55 -0
  5. model.safetensors +3 -0
README.md ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-xnor4
11
+
12
+ 4-input XNOR gate. Outputs 1 when an even number of inputs are 1 (0, 2, or 4).
13
+
14
+ ## Architecture
15
+
16
+ Tree structure: XNOR4(a,b,c,d) = XNOR(XNOR(a,b), XNOR(c,d))
17
+
18
+ ```
19
+ a,b ──► XNOR ──┐
20
+ ├──► XNOR ──► output
21
+ c,d ──► XNOR ──┘
22
+ ```
23
+
24
+ ## Parameters
25
+
26
+ | | |
27
+ |---|---|
28
+ | Neurons | 9 |
29
+ | Layers | 4 |
30
+ | Parameters | 27 |
31
+ | Magnitude | 27 |
32
+
33
+ ## Usage
34
+
35
+ ```python
36
+ from safetensors.torch import load_file
37
+
38
+ w = load_file('model.safetensors')
39
+ # See model.py for forward pass
40
+ ```
41
+
42
+ ## License
43
+
44
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-xnor4",
3
+ "description": "4-input XNOR gate as threshold circuit",
4
+ "inputs": 4,
5
+ "outputs": 1,
6
+ "neurons": 9,
7
+ "layers": 4,
8
+ "parameters": 27
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ # XNOR4 = XNOR(XNOR(a,b), XNOR(c,d))
5
+ # Tree structure using 3 XNOR gates
6
+ # Each XNOR: NOR + AND -> OR
7
+
8
+ def make_xnor_weights(prefix):
9
+ return {
10
+ f'{prefix}.layer1.n1.weight': torch.tensor([-1.0, -1.0]), # NOR
11
+ f'{prefix}.layer1.n1.bias': torch.tensor([0.0]),
12
+ f'{prefix}.layer1.n2.weight': torch.tensor([1.0, 1.0]), # AND
13
+ f'{prefix}.layer1.n2.bias': torch.tensor([-2.0]),
14
+ f'{prefix}.layer2.weight': torch.tensor([1.0, 1.0]), # OR
15
+ f'{prefix}.layer2.bias': torch.tensor([-1.0]),
16
+ }
17
+
18
+ weights = {}
19
+ weights.update(make_xnor_weights('xnor1')) # XNOR(a,b)
20
+ weights.update(make_xnor_weights('xnor2')) # XNOR(c,d)
21
+ weights.update(make_xnor_weights('xnor3')) # XNOR(xnor_ab, xnor_cd)
22
+
23
+ save_file(weights, 'model.safetensors')
24
+
25
+ mag = sum(t.abs().sum().item() for t in weights.values())
26
+ print(f'Created model.safetensors')
27
+ print(f' magnitude: {mag:.0f}')
28
+ print(f' parameters: {sum(t.numel() for t in weights.values())}')
model.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Threshold Network for 4-input XNOR Gate
3
+
4
+ XNOR4(a,b,c,d) = 1 when even number of inputs are 1 (0, 2, or 4)
5
+ Built as: XNOR(XNOR(a,b), XNOR(c,d))
6
+ """
7
+
8
+ import torch
9
+ from safetensors.torch import load_file
10
+
11
+
12
+ def xnor2(x, y, w, prefix):
13
+ """2-input XNOR using NOR + AND -> OR structure."""
14
+ inp = torch.tensor([float(x), float(y)])
15
+ n1 = int((inp * w[f'{prefix}.layer1.n1.weight']).sum() + w[f'{prefix}.layer1.n1.bias'] >= 0)
16
+ n2 = int((inp * w[f'{prefix}.layer1.n2.weight']).sum() + w[f'{prefix}.layer1.n2.bias'] >= 0)
17
+ h = torch.tensor([float(n1), float(n2)])
18
+ return int((h * w[f'{prefix}.layer2.weight']).sum() + w[f'{prefix}.layer2.bias'] >= 0)
19
+
20
+
21
+ class ThresholdXNOR4:
22
+ def __init__(self, weights_dict):
23
+ self.w = weights_dict
24
+
25
+ def __call__(self, a, b, c, d):
26
+ # Tree structure: XNOR(XNOR(a,b), XNOR(c,d))
27
+ xnor_ab = xnor2(a, b, self.w, 'xnor1')
28
+ xnor_cd = xnor2(c, d, self.w, 'xnor2')
29
+ result = xnor2(xnor_ab, xnor_cd, self.w, 'xnor3')
30
+ return float(result)
31
+
32
+ @classmethod
33
+ def from_safetensors(cls, path="model.safetensors"):
34
+ return cls(load_file(path))
35
+
36
+
37
+ if __name__ == "__main__":
38
+ weights = load_file("model.safetensors")
39
+ model = ThresholdXNOR4(weights)
40
+
41
+ print("4-input XNOR Gate Truth Table:")
42
+ print("-" * 35)
43
+ correct = 0
44
+ for a in [0, 1]:
45
+ for b in [0, 1]:
46
+ for c in [0, 1]:
47
+ for d in [0, 1]:
48
+ out = int(model(a, b, c, d))
49
+ # XNOR4 = even parity = NOT XOR4
50
+ expected = 1 - (a ^ b ^ c ^ d)
51
+ status = "OK" if out == expected else "FAIL"
52
+ if out == expected:
53
+ correct += 1
54
+ print(f"XNOR4({a},{b},{c},{d}) = {out} [{status}]")
55
+ print(f"\nTotal: {correct}/16 correct")
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8a85f9a18d2b2f26d401efc40f3f410be757d5a379fe7a50114b7d3992d34f94
3
+ size 1452