По желанию
Линейные оценки и потери
Проследить точную подгонку меток и шаги перцептрона; сравнить условные риски и проверить границы hinge/logistic. Ноутбук на стандартной библиотеке с сохранёнными результатами.
- Версия
- v2
- Минут
- ~20
- Проверка
- Самопроверка в ноутбуке
Данные: Explicit finite classifier fixtures from reviewed FP82b6cb0; new self-checks with Codex assistance. / Явные конечные примеры из проверенной FP82b6cb0; новые самопроверки с помощью Codex.
Пока нет автопроверки на платформе: сравните вывод ячеек с ожидаемыми числами в ноутбуке. Это не долг и не влияет на процент освоения.
Скачайте ноутбук и запустите его локально. Требования к окружению указаны внутри.
Содержимое ноутбука
Можно прочитать с телефона. Код ниже не выполнялся в браузере; для запуска скачайте ноутбук.
Linear scores and losses / Линейные оценки и потери
Follow the boundary shift, compare pointwise and restricted risks, and trace perceptron updates. Printed risks are rounded to six decimals. / Проследите сдвиг границы, сравните поточечный и ограниченный риски, проверьте шаги перцептрона. Напечатанные риски округлены до шести знаков.
Source / Источник: published revision 82b6cb0.
Prepared for the Fedor Pakhurov project with Codex assistance. No datasets, models or packages are downloaded. Only Python standard-library modules are used. Main calculations follow the reviewed source; added checks are identified below. No benchmark/generalization claim.
Подготовлено для проекта Фёдора Пахурова с помощью Codex. Данные, модели и пакеты не скачиваются. Используется стандартная библиотека Python. Основные расчёты следуют проверенному источнику; дополнительные проверки отмечены ниже. Это не бенчмарк и не проверка обобщения.
Run cells in order. Saved stdout is from a local execution; hosted/Colab execution has not been claimed. / Выполняйте ячейки по порядку. Сохранённый stdout получен локально; запуск в Colab не заявляется.
Reviewed calculations / Проверенные расчёты
The explicit points, probability counts and helper functions below come from the pinned original renewal. A fixed cyclic order is used for the perceptron, not random SGD. / Явные точки, количества вероятностей и функции ниже взяты из закреплённого авторского обновления. Перцептрон идёт в фиксированном циклическом порядке, не случайном SGD.
from fractions import Fraction
from itertools import product
import json
from math import comb, exp, log, log1p, sqrt
GEOMETRY = [((-1, 1), -1), ((0, 2), -1), ((1, 0), 1), ((2, 1), 1),
((3, 1), 1), ((2, 4), 1), ((1, -2), -1)]
def logistic(margin):
return (max(0, -margin) + log1p(exp(-abs(margin)))) / log(2)
def losses(margin):
return {'logistic': logistic(margin), 'hinge': max(0, 1-margin),
'exponential': exp(-margin), 'squared_hinge': max(0, 1-margin)**2,
'square': (1-margin)**2}
def conditional_risk(probability, score, loss):
return probability*loss(score)+(1-probability)*loss(-score)
def least_squares(xs, ys):
n = len(xs)
slope = Fraction(n*sum(x*y for x, y in zip(xs, ys))-sum(xs)*sum(ys),
n*sum(x*x for x in xs)-sum(xs)**2)
intercept = Fraction(sum(ys), n)-slope*Fraction(sum(xs), n)
return {'slope': str(slope), 'intercept': str(intercept),
'boundary': str(-intercept/slope),
'errors': sum(y*(slope*x+intercept) <= 0 for x, y in zip(xs, ys))}
def perceptron():
xs = [(2, -1, 1), (-1, 2, 1), (3, 1, 1), (0, -3, 1)]
ys = [1, -1, 1, -1]
weights = [0, 0, 0]
updates = []
for epoch in range(1, 101):
changed = False
for index, (x, y) in enumerate(zip(xs, ys), 1):
margin = y*sum(a*b for a, b in zip(x, weights))
if margin <= 0:
weights = [a+y*b for a, b in zip(weights, x)]
updates.append({'epoch': epoch, 'point': index, 'margin': margin,
'weights_after': weights[:]})
changed = True
if not changed:
break
else:
raise AssertionError('The explicitly separable fixture failed to terminate')
final_margins = [y*sum(a*b for a, b in zip(x, weights)) for x, y in zip(xs, ys)]
# A unit separating vector is (2, 0, -1)/sqrt(5) in the augmented space.
radius_squared = max(sum(a*a for a in x) for x in xs)
raw_min_margin = min(y*(2*x[0]-x[2]) for x, y in zip(xs, ys))
bound = Fraction(5*radius_squared, raw_min_margin**2)
assert final_margins == [5, 4, 8, 1] and len(updates) <= bound
return {'inputs': xs, 'labels': ys, 'updates': updates, 'weights': weights,
'final_margins': final_margins, 'radius_squared': radius_squared,
'gamma_squared': str(Fraction(raw_min_margin**2, 5)), 'bound': str(bound)}
def calculate():
rows = []
for name, (x, y) in zip('ABCDEFG', GEOMETRY):
score = 2*x[0]-x[1]-1
rows.append({'id': name, 'x': x, 'label': y, 'score': score,
'margin': y*score, 'losses': losses(y*score)})
upper_bounds = {key: sum(row['losses'][key] for row in rows)/len(rows)
for key in losses(0)}
error = sum(row['margin'] <= 0 for row in rows)/len(rows)
assert all(value >= error for value in upper_bounds.values())
# This checks the defined formulas on a grid; the text supplies the inequalities.
for i in range(-120, 121):
margin = i/20
assert all(value+1e-12 >= int(margin <= 0) for value in losses(margin).values())
# Three equally likely feature values. Counts specify a fictional distribution.
counts = [1, 7, 1]
probability = [count/8 for count in counts]
scores = [log(p/(1-p)) for p in probability]
pooled = sum(counts)/24
pooled_score = log(pooled/(1-pooled))
independent_loss = sum(conditional_risk(p, f, logistic) for p, f in zip(probability, scores))/3
linear_loss = sum(conditional_risk(p, pooled_score, logistic) for p in probability)/3
gradients = [1/(1+exp(-pooled_score))-p for p in probability]
assert abs(sum(gradients)) < 1e-12
assert abs(sum(x*g for x, g in zip([-1, 0, 1], gradients))) < 1e-12
# Strict convexity for this full-rank fixture makes this stationary linear fit unique.
conditional = []
for p in [1/8, 7/8, 3/8]:
optima = {'logistic': log(p/(1-p)), 'hinge': 1 if p > .5 else -1,
'exponential': .5*log(p/(1-p)), 'squared_hinge': 2*p-1, 'square': 2*p-1}
for name, best in optima.items():
best_risk = conditional_risk(p, best, lambda m: losses(m)[name])
for delta in [-2, -.1, .1, 2]:
assert best_risk <= conditional_risk(p, best+delta, lambda m: losses(m)[name])+1e-12
conditional.append({'probability': p, 'optima': optima})
separability = []
# Points (t,t^2), t=0..n-1: a linear score is a quadratic in t.
# Its strict signs switch at most twice. Conversely place roots between switches.
for n in [3, 6, 7, 9]:
count = sum(sum(a != b for a, b in zip(labels, labels[1:])) <= 2
for labels in product([-1, 1], repeat=n))
expected = 2*sum(comb(n-1, k) for k in range(min(2, n-1)+1))
assert count == expected
separability.append({'n': n, 'dimension': 2, 'separable_labelings': count,
'total_labelings': 2**n, 'probability': count/2**n})
result = {
'origin': 'Independently specified finite teaching fixtures; composed with Codex assistance; no empirical benchmark claim.',
'least_squares': {'base': least_squares([-4, -2, 1, 3], [-1, -1, 1, 1]),
'with_far_positive': least_squares([-4, -2, 1, 3, 40], [-1, -1, 1, 1, 1])},
'geometry': {'rows': rows, 'normal': [2, -1], 'bias': -1, 'example': [3, 1],
'projection': [1.4, 1.8], 'distance': 4/sqrt(5), 'error_fraction': error,
'average_losses': upper_bounds},
'conditional': {'positive_counts': counts, 'group_size': 8, 'feature_values': [-1, 0, 1],
'flexible_scores': scores, 'linear_weights': [0, pooled_score],
'flexible_loss': independent_loss, 'linear_loss': linear_loss,
'flexible_error': 3/24, 'linear_error': 9/24, 'pointwise_optima': conditional},
'perceptron': perceptron(), 'separability': separability,
}
assert result['least_squares']['base']['errors'] == 0
assert result['least_squares']['with_far_positive']['errors'] == 1
return result
result = calculate()
summary = {
'least_squares': result['least_squares'],
'conditional_losses': [round(result['conditional'][k], 6)
for k in ['flexible_loss', 'linear_loss']],
'conditional_errors': [result['conditional'][k]
for k in ['flexible_error', 'linear_error']],
'perceptron_updates': result['perceptron']['updates'],
'perceptron_final_margins': result['perceptron']['final_margins'],
'perceptron_update_bound': result['perceptron']['bound'],
}
print(json.dumps(summary, indent=2))New self-checks / Новые самопроверки
Before running, predict where hinge becomes zero and why logistic remains positive. Then change the displayed margin grid. The loop checks the selected points only; the lesson supplies the general inequality. / До запуска предскажите, где hinge станет нулём и почему логистическая потеря остаётся положительной. Затем измените сетку отступов. Цикл проверяет только выбранные точки; общее неравенство объяснено в теме.
# New self-checks from explicitly chosen small inputs.
for margin in [-2, -0.5, 0, 0.75, 1, 3]:
value = losses(margin)
assert value['hinge'] >= int(margin <= 0)
assert value['logistic'] >= int(margin <= 0)
print(f"margin={margin:5.2f} hinge={value['hinge']:.6f} logistic={value['logistic']:.6f}")
# Ties are assigned to +1: distinguish actual error from the conservative count.
assert (int(0 < 0), int(0 <= 0)) == (0, 1)
# New feature variant: an affine score cannot produce these three strict signs.
features = [-1, 0, 1]
assert [1 - 2*x*x > 0 for x in features] == [False, True, False]
print('Self-checks passed. Grid checks illustrate inequalities, not a proof for all margins.')