File size: 1,023 Bytes
b7602a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np

# SpTn-Model-only-1P
# Text -> 1 parameter -> generated text

np.random.seed(42)

# Exactly ONE trainable parameter
w = np.float32(0.5)

text = input("Enter text: ").lower()

if not text:
    text = "hello world"

chars = np.array(list("abcdefghijklmnopqrstuvwxyz "))

# Target = average character code, normalized
codes = np.array([
    (ord(c) - 32) / 95.0
    for c in text
    if 32 <= ord(c) <= 126
])

target = np.mean(codes)

# Train ONE parameter
lr = 0.05

for step in range(1000):
    prediction = w
    error = prediction - target

    loss = error ** 2
    gradient = 2 * error

    w -= lr * gradient

print("\nSpTn-Model-only-1P")
print("Trainable parameters:", 1)
print("Parameter:", w)
print("Loss:", loss)

# Generate text
output = ""

for _ in range(200):
    value = np.random.random()

    index = int(
        np.clip(
            w * len(chars) + value * 3,
            0,
            len(chars) - 1
        )
    )

    output += chars[index]

print("\nGenerated:")
print(output)