phanerozoic commited on
Commit
b133db9
·
verified ·
1 Parent(s): 0e92d7e

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +63 -63
  2. config.json +9 -9
  3. create_safetensors.py +26 -26
  4. model.py +16 -16
README.md CHANGED
@@ -1,63 +1,63 @@
1
- ---
2
- license: mit
3
- tags:
4
- - pytorch
5
- - safetensors
6
- - threshold-logic
7
- - neuromorphic
8
- ---
9
-
10
- # threshold-3outof4
11
-
12
- At least 3 of 4 inputs high.
13
-
14
- ## Function
15
-
16
- 3outof4(a, b, c, d) = 1 if (a + b + c + d) >= 3, else 0
17
-
18
- ## Truth Table
19
-
20
- | a | b | c | d | sum | out |
21
- |---|---|---|---|-----|-----|
22
- | 0 | 0 | 0 | 0 | 0 | 0 |
23
- | 0 | 0 | 0 | 1 | 1 | 0 |
24
- | 0 | 0 | 1 | 1 | 2 | 0 |
25
- | 0 | 1 | 1 | 1 | 3 | 1 |
26
- | 1 | 1 | 1 | 1 | 4 | 1 |
27
-
28
- ## Architecture
29
-
30
- Single neuron: weights [1, 1, 1, 1], bias -3
31
-
32
- Fires when: a + b + c + d - 3 >= 0, i.e., sum >= 3
33
-
34
- ## Parameters
35
-
36
- | | |
37
- |---|---|
38
- | Inputs | 4 |
39
- | Outputs | 1 |
40
- | Neurons | 1 |
41
- | Layers | 1 |
42
- | Parameters | 5 |
43
- | Magnitude | 7 |
44
-
45
- ## Usage
46
-
47
- ```python
48
- from safetensors.torch import load_file
49
- import torch
50
-
51
- w = load_file('model.safetensors')
52
-
53
- def atleast3of4(a, b, c, d):
54
- inp = torch.tensor([float(a), float(b), float(c), float(d)])
55
- return int((inp @ w['neuron.weight'].T + w['neuron.bias'] >= 0).item())
56
-
57
- print(atleast3of4(0, 0, 1, 1)) # 0 (sum=2)
58
- print(atleast3of4(0, 1, 1, 1)) # 1 (sum=3)
59
- ```
60
-
61
- ## License
62
-
63
- MIT
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-3outof4
11
+
12
+ At least 3 of 4 inputs high.
13
+
14
+ ## Function
15
+
16
+ 3outof4(a, b, c, d) = 1 if (a + b + c + d) >= 3, else 0
17
+
18
+ ## Truth Table
19
+
20
+ | a | b | c | d | sum | out |
21
+ |---|---|---|---|-----|-----|
22
+ | 0 | 0 | 0 | 0 | 0 | 0 |
23
+ | 0 | 0 | 0 | 1 | 1 | 0 |
24
+ | 0 | 0 | 1 | 1 | 2 | 0 |
25
+ | 0 | 1 | 1 | 1 | 3 | 1 |
26
+ | 1 | 1 | 1 | 1 | 4 | 1 |
27
+
28
+ ## Architecture
29
+
30
+ Single neuron: weights [1, 1, 1, 1], bias -3
31
+
32
+ Fires when: a + b + c + d - 3 >= 0, i.e., sum >= 3
33
+
34
+ ## Parameters
35
+
36
+ | | |
37
+ |---|---|
38
+ | Inputs | 4 |
39
+ | Outputs | 1 |
40
+ | Neurons | 1 |
41
+ | Layers | 1 |
42
+ | Parameters | 5 |
43
+ | Magnitude | 7 |
44
+
45
+ ## Usage
46
+
47
+ ```python
48
+ from safetensors.torch import load_file
49
+ import torch
50
+
51
+ w = load_file('model.safetensors')
52
+
53
+ def atleast3of4(a, b, c, d):
54
+ inp = torch.tensor([float(a), float(b), float(c), float(d)])
55
+ return int((inp @ w['neuron.weight'].T + w['neuron.bias'] >= 0).item())
56
+
57
+ print(atleast3of4(0, 0, 1, 1)) # 0 (sum=2)
58
+ print(atleast3of4(0, 1, 1, 1)) # 1 (sum=3)
59
+ ```
60
+
61
+ ## License
62
+
63
+ MIT
config.json CHANGED
@@ -1,9 +1,9 @@
1
- {
2
- "name": "threshold-3outof4",
3
- "description": "At least 3 of 4 inputs high",
4
- "inputs": 4,
5
- "outputs": 1,
6
- "neurons": 1,
7
- "layers": 1,
8
- "parameters": 5
9
- }
 
1
+ {
2
+ "name": "threshold-3outof4",
3
+ "description": "At least 3 of 4 inputs high",
4
+ "inputs": 4,
5
+ "outputs": 1,
6
+ "neurons": 1,
7
+ "layers": 1,
8
+ "parameters": 5
9
+ }
create_safetensors.py CHANGED
@@ -1,26 +1,26 @@
1
- import torch
2
- from safetensors.torch import save_file
3
-
4
- weights = {
5
- 'neuron.weight': torch.tensor([[1.0, 1.0, 1.0, 1.0]], dtype=torch.float32),
6
- 'neuron.bias': torch.tensor([-3.0], dtype=torch.float32)
7
- }
8
-
9
- save_file(weights, 'model.safetensors')
10
-
11
- def atleast3of4(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 3outof4...")
16
- errors = 0
17
- for i in range(16):
18
- a, b, c, d = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
19
- result = atleast3of4(a, b, c, d)
20
- expected = 1 if (a + b + c + d) >= 3 else 0
21
- if result != expected:
22
- errors += 1
23
- if errors == 0:
24
- print("All 16 test cases passed!")
25
- mag = sum(t.abs().sum().item() for t in weights.values())
26
- print(f"Magnitude: {mag:.0f}")
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ weights = {
5
+ 'neuron.weight': torch.tensor([[1.0, 1.0, 1.0, 1.0]], dtype=torch.float32),
6
+ 'neuron.bias': torch.tensor([-3.0], dtype=torch.float32)
7
+ }
8
+
9
+ save_file(weights, 'model.safetensors')
10
+
11
+ def atleast3of4(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 3outof4...")
16
+ errors = 0
17
+ for i in range(16):
18
+ a, b, c, d = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
19
+ result = atleast3of4(a, b, c, d)
20
+ expected = 1 if (a + b + c + d) >= 3 else 0
21
+ if result != expected:
22
+ errors += 1
23
+ if errors == 0:
24
+ print("All 16 test cases passed!")
25
+ mag = sum(t.abs().sum().item() for t in weights.values())
26
+ print(f"Magnitude: {mag:.0f}")
model.py CHANGED
@@ -1,16 +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 atleast3of4(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('3outof4 truth table:')
14
- for i in range(16):
15
- a, b, c, d = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
16
- print(f' {a}{b}{c}{d} -> {atleast3of4(a, b, c, d, w)}')
 
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 atleast3of4(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('3outof4 truth table:')
14
+ for i in range(16):
15
+ a, b, c, d = (i >> 3) & 1, (i >> 2) & 1, (i >> 1) & 1, i & 1
16
+ print(f' {a}{b}{c}{d} -> {atleast3of4(a, b, c, d, w)}')