Skip to content
BIASED
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 .ipynb

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 · Данные: скалярная критика fθ(x)=tanh⁡(θx)f_\theta(x)=\tanh(\theta x).

Тема WGAN: 1-Lipschitz ⇔\Leftrightarrow ∥∇f∥≤1\|\nabla f\|\le 1 (дифференцируемый случай). Weight clipping / gradient penalty тянут к этому.

Ожидаемые числа
Setup max ∣f′(x)∣\lvert f'(x)\rvert on grid Tolerance
θ=1\theta=1 1.0 at x=0x=0 abs ≤ 1e-12
θ=2\theta=2 2.0 abs ≤ 1e-12
clip θ→clip(θ,−1,1)\theta\to\mathrm{clip}(\theta,-1,1) from 2 max slope 1.0 exact
GP target: mean $( f' -1)^2atat\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")
 
Самопроверка в ноутбуке
  • θ=1\theta=1 даёт max slope 1; θ=2\theta=2 — 2
  • clipping возвращает Lip ≤ 1
  • GP-штраф положителен при θ=2\theta=2