Repository navigation
Expand file tree
/
Copy pathregularization.rb
More file actions
29 lines (28 loc) · 1.46 KB
/
Copy pathregularization.rb
File metadata and controls
29 lines (28 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# frozen_string_literal: true
# L2 regularization — J_data + λ||w||²/(2m); gradient adds λw/m, never λb/m.
# Compare training fit with shrinkage. Smaller training error is not by itself
# better generalization; held-out model selection is a separate example.
# Run me: ruby supervised/regularization.rb
require_relative "linear_regression"
require_relative "logistic_regression"
if __FILE__ == $PROGRAM_NAME
require_relative "../lib/course_data"
include Style
banner("Regularization", "course housing and microchip data · handwritten L2 objectives")
rows = CourseData.table(:houses, "data/houses.txt", skip: 1)
x = rows.map { |r| r.first(4) }
y = rows.map(&:last)
x = NumericTools.transform(x, NumericTools.fit_scaler(x))
[0.0, 1.0, 10.0].each do |lambda|
w, b = LinearRegression.fit(x, y, rate: 0.1, iterations: 1000, l2: lambda)
step("housing lambda=#{lambda}: data cost=#{LinearRegression.loss(x, y, w, b)}; weights=#{vector(w)}")
end
chips, labels = CourseData.supervised(:logistic, "data/ex2data2.txt")
require_relative "regularized_logistic_regression"
expanded = RegularizedLogisticRegression.map_features(chips)
[0.0, 1.0, 10.0].each do |lambda|
w, b = LogisticRegression.fit(expanded, labels, iterations: 1000, rate: 0.01, l2: lambda)
step("microchip lambda=#{lambda}: objective=#{LogisticRegression.loss(expanded, labels, w, b, l2: lambda)}")
end
ok("linear and logistic penalties operate on the course feature matrices")
end