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

4-bit zero detector, magnitude 4

Browse files
Files changed (5) hide show
  1. README.md +55 -0
  2. config.json +9 -0
  3. create_safetensors.py +25 -0
  4. model.py +16 -0
  5. model.safetensors +0 -0
README.md ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-iszero4
11
+
12
+ 4-bit zero detector. Outputs 1 if all 4 input bits are zero.
13
+
14
+ ## Function
15
+
16
+ iszero4(a, b, c, d) = 1 if (a + b + c + d) == 0, else 0
17
+
18
+ Equivalent to 4-input NOR gate.
19
+
20
+ ## Architecture
21
+
22
+ Single neuron: weights [-1, -1, -1, -1], bias 0
23
+
24
+ Fires when: -a - b - c - d + 0 >= 0, i.e., all inputs are 0.
25
+
26
+ ## Parameters
27
+
28
+ | | |
29
+ |---|---|
30
+ | Inputs | 4 |
31
+ | Outputs | 1 |
32
+ | Neurons | 1 |
33
+ | Layers | 1 |
34
+ | Parameters | 5 |
35
+ | Magnitude | 4 |
36
+
37
+ ## Usage
38
+
39
+ ```python
40
+ from safetensors.torch import load_file
41
+ import torch
42
+
43
+ w = load_file('model.safetensors')
44
+
45
+ def iszero4(a, b, c, d):
46
+ inp = torch.tensor([float(a), float(b), float(c), float(d)])
47
+ return int((inp @ w['neuron.weight'].T + w['neuron.bias'] >= 0).item())
48
+
49
+ print(iszero4(0, 0, 0, 0)) # 1 (is zero)
50
+ print(iszero4(0, 0, 0, 1)) # 0 (not zero)
51
+ ```
52
+
53
+ ## License
54
+
55
+ MIT
config.json ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "threshold-iszero4",
3
+ "description": "4-bit zero detector",
4
+ "inputs": 4,
5
+ "outputs": 1,
6
+ "neurons": 1,
7
+ "layers": 1,
8
+ "parameters": 5
9
+ }
create_safetensors.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ # iszero4: outputs 1 if all 4 bits are 0 (4-input NOR)
5
+ weights = {
6
+ 'neuron.weight': torch.tensor([[-1.0, -1.0, -1.0, -1.0]], dtype=torch.float32),
7
+ 'neuron.bias': torch.tensor([0.0], dtype=torch.float32)
8
+ }
9
+ save_file(weights, 'model.safetensors')
10
+
11
+ def iszero4(a, b, c, d):
12
+ inp = torch.tensor([float(a), float(b), float(c), float(d)])
13
+ return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
14
+
15
+ print("Verifying iszero4...")
16
+ errors = 0
17
+ for i in range(16):
18
+ bits = [(i >> j) & 1 for j in range(4)]
19
+ result = iszero4(*bits)
20
+ expected = 1 if i == 0 else 0
21
+ if result != expected:
22
+ errors += 1
23
+ if errors == 0:
24
+ print("All 16 test cases passed!")
25
+ print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")
model.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from safetensors.torch import load_file
3
+
4
+ def load_model(path='model.safetensors'):
5
+ return load_file(path)
6
+
7
+ def iszero4(a, b, c, d, weights):
8
+ inp = torch.tensor([float(a), float(b), float(c), float(d)])
9
+ return int((inp @ weights['neuron.weight'].T + weights['neuron.bias'] >= 0).item())
10
+
11
+ if __name__ == '__main__':
12
+ w = load_model()
13
+ print('iszero4: outputs 1 only for input 0000')
14
+ for i in [0, 1, 8, 15]:
15
+ bits = [(i >> j) & 1 for j in range(4)]
16
+ print(f' {i:04b} -> {iszero4(*bits, w)}')
model.safetensors ADDED
Binary file (164 Bytes). View file