Back to topic
Optional
Sigmoid and logistic NLL
σ(z), NLL and ∇w on a tiny dataset. Self-check in the notebook.
- Version
- v1
- Minutes
- ~20
- 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.
ML · Сигмоида и NLL / Sigmoid and logistic NLL
Activity version: 1 · Seed: n/a · Данные: 4 точки, , bias в нулевом столбце .
Ожидаемые числа
| Quantity | Value | Tolerance |
|---|---|---|
≈ 0.50000000, 0.73105858, 0.88079708, 0.95257413 |
abs ≤ 1e-5 | |
| NLL | ≈ 2.181924 |
abs ≤ 1e-5 |
| NLL | ≈ 1.06442978, 0.35037512 |
abs ≤ 1e-5 |
Самопроверка в ноутбуке
Не влияет на mastery.
import numpy as np
X = np.array([[1.0, 0.0], [1.0, 1.0], [1.0, 2.0], [1.0, 3.0]])
y = np.array([0.0, 0.0, 1.0, 1.0])
w = np.array([0.0, 1.0])
def sigmoid(z):
return 1.0 / (1.0 + np.exp(-z))
p = sigmoid(X @ w)
nll = -np.sum(y * np.log(p) + (1 - y) * np.log(1 - p))
g = X.T @ (p - y)
print("p", p)
print("nll", nll)
print("grad", g)
assert np.allclose(p, [0.50000000, 0.73105858, 0.88079708, 0.95257413], atol=1e-5)
assert abs(nll - 2.18192423) < 1e-5
assert np.allclose(g, [1.06442978, 0.35037512], atol=1e-5)
print("OK: logistic sigmoid NLL")
Самопроверка в ноутбуке
- , NLL и градиент совпали с таблицей
- градиент =