-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregularization.py
More file actions
executable file
·92 lines (73 loc) · 1.85 KB
/
regularization.py
File metadata and controls
executable file
·92 lines (73 loc) · 1.85 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import numpy as np
import matplotlib.pyplot as plt
# 真の関数
def g(x):
return 0.1 * (x ** 3 + x ** 2 + x)
# 真の関数にノイズを加えた学習データを適当な数だけ用意する
train_x = np.linspace(-2, 2, 8)
print(train_x)
train_y = g(train_x) + np.random.randn(train_x.size) * 0.05
# 標準化
mu = train_x.mean()
sigma = train_x.std()
def standardize(x):
return (x - mu) / sigma
train_z = standardize(train_x)
# 学習データの行列を作る
def to_matrix(x):
return np.vstack([
np.ones(x.size),
x,
x ** 2,
x ** 3,
x ** 4,
x ** 5,
x ** 6,
x ** 7,
x ** 8,
x ** 9,
x ** 10
]).T
X = to_matrix(train_z)
# パラメータの初期化
print (X.shape[1])
theta = np.random.randn(X.shape[1])
# 予測関数
def f(x):
return np.dot(x, theta)
# 目的関数
def E(x, y):
return 0.5 * np.sum((y - f(x)) ** 2)
# 正則化定数
LAMBDA = 0.5
# 学習率
ETA = 1e-4
# 誤差
diff = 1
# 正則化を適用せずに学習を繰り返す
error = E(X, train_y)
while diff > 1e-6:
theta = theta - ETA * (np.dot(f(X) - train_y, X))
current_error = E(X, train_y)
diff = error - current_error
error = current_error
theta1 = theta
# 正則化を適用して学習を繰り返す
theta = np.random.randn(X.shape[1])
diff = 1
error = E(X, train_y)
while diff > 1e-6:
reg_term = LAMBDA * np.hstack([0, theta[1:]])
theta = theta - ETA * (np.dot(f(X) - train_y, X) + reg_term)
current_error = E(X, train_y)
diff = error - current_error
error = current_error
theta2 = theta
# プロットして確認
plt.plot(train_z, train_y, 'o')
z = standardize(np.linspace(-2, 2, 100))
theta = theta1 # 正則化なし
plt.plot(z, f(to_matrix(z)), linestyle='dashed')
theta = theta2 # 正則化あり
plt.plot(z, f(to_matrix(z)))
plt.show()