phanerozoic commited on
Commit
67df09a
·
verified ·
1 Parent(s): 4055650

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +68 -68
  2. config.json +9 -9
  3. create_safetensors.py +36 -36
  4. model.py +17 -17
README.md CHANGED
@@ -1,68 +1,68 @@
1
- ---
2
- license: mit
3
- tags:
4
- - pytorch
5
- - safetensors
6
- - threshold-logic
7
- - neuromorphic
8
- ---
9
-
10
- # threshold-2to4decoder
11
-
12
- 2-to-4 binary decoder. Converts 2-bit input to one-hot 4-bit output.
13
-
14
- ## Function
15
-
16
- decode(A1, A0) -> [Y0, Y1, Y2, Y3]
17
-
18
- Yi = 1 iff input = i
19
-
20
- ## Truth Table
21
-
22
- | A1 | A0 | Y0 | Y1 | Y2 | Y3 |
23
- |----|----|----|----|----|-----|
24
- | 0 | 0 | 1 | 0 | 0 | 0 |
25
- | 0 | 1 | 0 | 1 | 0 | 0 |
26
- | 1 | 0 | 0 | 0 | 1 | 0 |
27
- | 1 | 1 | 0 | 0 | 0 | 1 |
28
-
29
- ## Architecture
30
-
31
- Single layer with 4 neurons. Each Yi matches pattern i.
32
-
33
- | Output | Weights [A1, A0] | Bias |
34
- |--------|------------------|------|
35
- | Y0 | [-1, -1] | 0 |
36
- | Y1 | [-1, +1] | -1 |
37
- | Y2 | [+1, -1] | -1 |
38
- | Y3 | [+1, +1] | -2 |
39
-
40
- ## Parameters
41
-
42
- | | |
43
- |---|---|
44
- | Inputs | 2 |
45
- | Outputs | 4 |
46
- | Neurons | 4 |
47
- | Layers | 1 |
48
- | Parameters | 12 |
49
- | Magnitude | 12 |
50
-
51
- ## Usage
52
-
53
- ```python
54
- from safetensors.torch import load_file
55
- import torch
56
-
57
- w = load_file('model.safetensors')
58
-
59
- def decode2to4(a1, a0):
60
- inp = torch.tensor([float(a1), float(a0)])
61
- return [int((inp * w[f'y{i}.weight']).sum() + w[f'y{i}.bias'] >= 0) for i in range(4)]
62
-
63
- print(decode2to4(1, 0)) # [0, 0, 1, 0] - input 2
64
- ```
65
-
66
- ## License
67
-
68
- MIT
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - pytorch
5
+ - safetensors
6
+ - threshold-logic
7
+ - neuromorphic
8
+ ---
9
+
10
+ # threshold-2to4decoder
11
+
12
+ 2-to-4 binary decoder. Converts 2-bit input to one-hot 4-bit output.
13
+
14
+ ## Function
15
+
16
+ decode(A1, A0) -> [Y0, Y1, Y2, Y3]
17
+
18
+ Yi = 1 iff input = i
19
+
20
+ ## Truth Table
21
+
22
+ | A1 | A0 | Y0 | Y1 | Y2 | Y3 |
23
+ |----|----|----|----|----|-----|
24
+ | 0 | 0 | 1 | 0 | 0 | 0 |
25
+ | 0 | 1 | 0 | 1 | 0 | 0 |
26
+ | 1 | 0 | 0 | 0 | 1 | 0 |
27
+ | 1 | 1 | 0 | 0 | 0 | 1 |
28
+
29
+ ## Architecture
30
+
31
+ Single layer with 4 neurons. Each Yi matches pattern i.
32
+
33
+ | Output | Weights [A1, A0] | Bias |
34
+ |--------|------------------|------|
35
+ | Y0 | [-1, -1] | 0 |
36
+ | Y1 | [-1, +1] | -1 |
37
+ | Y2 | [+1, -1] | -1 |
38
+ | Y3 | [+1, +1] | -2 |
39
+
40
+ ## Parameters
41
+
42
+ | | |
43
+ |---|---|
44
+ | Inputs | 2 |
45
+ | Outputs | 4 |
46
+ | Neurons | 4 |
47
+ | Layers | 1 |
48
+ | Parameters | 12 |
49
+ | Magnitude | 12 |
50
+
51
+ ## Usage
52
+
53
+ ```python
54
+ from safetensors.torch import load_file
55
+ import torch
56
+
57
+ w = load_file('model.safetensors')
58
+
59
+ def decode2to4(a1, a0):
60
+ inp = torch.tensor([float(a1), float(a0)])
61
+ return [int((inp * w[f'y{i}.weight']).sum() + w[f'y{i}.bias'] >= 0) for i in range(4)]
62
+
63
+ print(decode2to4(1, 0)) # [0, 0, 1, 0] - input 2
64
+ ```
65
+
66
+ ## License
67
+
68
+ MIT
config.json CHANGED
@@ -1,9 +1,9 @@
1
- {
2
- "name": "threshold-2to4decoder",
3
- "description": "2-to-4 binary decoder",
4
- "inputs": 2,
5
- "outputs": 4,
6
- "neurons": 4,
7
- "layers": 1,
8
- "parameters": 12
9
- }
 
1
+ {
2
+ "name": "threshold-2to4decoder",
3
+ "description": "2-to-4 binary decoder",
4
+ "inputs": 2,
5
+ "outputs": 4,
6
+ "neurons": 4,
7
+ "layers": 1,
8
+ "parameters": 12
9
+ }
create_safetensors.py CHANGED
@@ -1,36 +1,36 @@
1
- import torch
2
- from safetensors.torch import save_file
3
-
4
- weights = {}
5
-
6
- # Input: A1, A0 (2 bits)
7
- # Output: Y0-Y3 (one-hot)
8
- # Yi fires when input = i
9
-
10
- for i in range(4):
11
- a1_bit = (i >> 1) & 1
12
- a0_bit = i & 1
13
- w = [1.0 if a1_bit else -1.0, 1.0 if a0_bit else -1.0]
14
- bias = -bin(i).count('1')
15
- weights[f'y{i}.weight'] = torch.tensor([w], dtype=torch.float32)
16
- weights[f'y{i}.bias'] = torch.tensor([float(bias)], dtype=torch.float32)
17
-
18
- save_file(weights, 'model.safetensors')
19
-
20
- def decode2to4(a1, a0):
21
- inp = torch.tensor([float(a1), float(a0)])
22
- return [int((inp * weights[f'y{i}.weight']).sum() + weights[f'y{i}.bias'] >= 0) for i in range(4)]
23
-
24
- print("Verifying 2to4decoder...")
25
- errors = 0
26
- for val in range(4):
27
- a1, a0 = (val >> 1) & 1, val & 1
28
- result = decode2to4(a1, a0)
29
- expected = [1 if i == val else 0 for i in range(4)]
30
- if result != expected:
31
- errors += 1
32
- print(f"ERROR: {val} -> {result}, expected {expected}")
33
-
34
- if errors == 0:
35
- print("All 4 test cases passed!")
36
- print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")
 
1
+ import torch
2
+ from safetensors.torch import save_file
3
+
4
+ weights = {}
5
+
6
+ # Input: A1, A0 (2 bits)
7
+ # Output: Y0-Y3 (one-hot)
8
+ # Yi fires when input = i
9
+
10
+ for i in range(4):
11
+ a1_bit = (i >> 1) & 1
12
+ a0_bit = i & 1
13
+ w = [1.0 if a1_bit else -1.0, 1.0 if a0_bit else -1.0]
14
+ bias = -bin(i).count('1')
15
+ weights[f'y{i}.weight'] = torch.tensor([w], dtype=torch.float32)
16
+ weights[f'y{i}.bias'] = torch.tensor([float(bias)], dtype=torch.float32)
17
+
18
+ save_file(weights, 'model.safetensors')
19
+
20
+ def decode2to4(a1, a0):
21
+ inp = torch.tensor([float(a1), float(a0)])
22
+ return [int((inp * weights[f'y{i}.weight']).sum() + weights[f'y{i}.bias'] >= 0) for i in range(4)]
23
+
24
+ print("Verifying 2to4decoder...")
25
+ errors = 0
26
+ for val in range(4):
27
+ a1, a0 = (val >> 1) & 1, val & 1
28
+ result = decode2to4(a1, a0)
29
+ expected = [1 if i == val else 0 for i in range(4)]
30
+ if result != expected:
31
+ errors += 1
32
+ print(f"ERROR: {val} -> {result}, expected {expected}")
33
+
34
+ if errors == 0:
35
+ print("All 4 test cases passed!")
36
+ print(f"Magnitude: {sum(t.abs().sum().item() for t in weights.values()):.0f}")
model.py CHANGED
@@ -1,17 +1,17 @@
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 decode2to4(a1, a0, weights):
8
- inp = torch.tensor([float(a1), float(a0)])
9
- return [int((inp * weights[f'y{i}.weight']).sum() + weights[f'y{i}.bias'] >= 0) for i in range(4)]
10
-
11
- if __name__ == '__main__':
12
- w = load_model()
13
- print('2-to-4 Decoder:')
14
- for val in range(4):
15
- a1, a0 = (val >> 1) & 1, val & 1
16
- result = decode2to4(a1, a0, w)
17
- print(f' {val} ({a1}{a0}) -> {"".join(map(str, result))}')
 
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 decode2to4(a1, a0, weights):
8
+ inp = torch.tensor([float(a1), float(a0)])
9
+ return [int((inp * weights[f'y{i}.weight']).sum() + weights[f'y{i}.bias'] >= 0) for i in range(4)]
10
+
11
+ if __name__ == '__main__':
12
+ w = load_model()
13
+ print('2-to-4 Decoder:')
14
+ for val in range(4):
15
+ a1, a0 = (val >> 1) & 1, val & 1
16
+ result = decode2to4(a1, a0, w)
17
+ print(f' {val} ({a1}{a0}) -> {"".join(map(str, result))}')