Download generator.py from KtiyaKK/SpTn-Model-only-1P: direct link, hf CLI and curl.
- Browser
- Download file 652 Bytes
-
https://huggingface.co/KtiyaKK/SpTn-Model-only-1P/resolve/main/generator.py
- Command line
-
hf download hf://KtiyaKK/SpTn-Model-only-1P/generator.py
-
curl -L -o generator.py https://huggingface.co/KtiyaKK/SpTn-Model-only-1P/resolve/main/generator.py
652 Bytes
| 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)) |