Back to topic
Optional
Numerical critic Lipschitz check
max |f'| for tanh(θx), weight clip, and GP penalty. Self-check in the notebook.
- Version
- v1
- Minutes
- ~15
- Evidence
- Self-check in the notebook
Until platform auto-check exists, compare cell output with the expected numbers in the notebook. This is not a debt and does not affect mastery.
Download the notebook and run it locally. Its environment requirements are listed inside.
Notebook contents
Readable on your phone. The code below has not been executed in the browser; download the notebook to run it.
GM · Численная проверка Липшица критика / Critic Lipschitz check
Activity version: 1 · Seed: 0 · Данные: скалярная критика .
Тема WGAN: 1-Lipschitz (дифференцируемый случай). Weight clipping / gradient penalty тянут к этому.
Ожидаемые числа
| Setup | max on grid | Tolerance |
|---|---|---|
1.0 at |
abs ≤ 1e-12 | |
2.0 |
abs ≤ 1e-12 | |
| clip from 2 | max slope 1.0 |
exact |
| GP target: mean $( | f' | -1)^2\theta=2$ over noise |
Самопроверка в ноутбуке
Не влияет на сертификат.
import numpy as np
def f_prime(theta, x):
# d/dx tanh(theta x) = theta * (1 - tanh^2(theta x))
t = np.tanh(theta * x)
return theta * (1 - t * t)
xs = np.linspace(-3, 3, 1001)
for theta, expect in [(1.0, 1.0), (2.0, 2.0)]:
m = float(np.max(np.abs(f_prime(theta, xs))))
print("theta", theta, "max|f'|", m)
assert abs(m - expect) < 1e-12
theta_clipped = float(np.clip(2.0, -1.0, 1.0))
assert theta_clipped == 1.0
assert abs(float(np.max(np.abs(f_prime(theta_clipped, xs)))) - 1.0) < 1e-12
rng = np.random.default_rng(0)
x = rng.normal(size=2048)
gp = float(np.mean((np.abs(f_prime(2.0, x)) - 1.0) ** 2))
print("GP mean at theta=2", gp)
assert gp > 0.1
print("OK: WGAN Lipschitz check")
Самопроверка в ноутбуке
- даёт max slope 1; — 2
- clipping возвращает Lip ≤ 1
- GP-штраф положителен при