File size: 652 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
import numpy as np

# SpTn-Model-only-1P
# Exactly ONE trainable parameter

np.random.seed(42)

# One trainable parameter
p = np.array(0.5)

# Training data
data = np.array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1])

lr = 0.1

for step in range(1000):
    prediction = p
    error = prediction - np.mean(data)

    loss = error ** 2
    gradient = 2 * error

    p -= lr * gradient

# Keep probability valid
p = np.clip(p, 0.0, 1.0)

print("SpTn-Model-only-1P")
print("Trainable parameters: 1")
print("Learned probability:", p)
print("Loss:", loss)

# Generate new samples
samples = np.random.random(20) < p

print("Generated samples:")
print(samples.astype(int))