-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnormalization.py
122 lines (78 loc) · 1.82 KB
/
normalization.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import matplotlib.pyplot as plt
import numpy as np
# In[2]:
def g(x):
return 0.1 * (x ** 3 + x ** 2 + x)
train_x = np.linspace(-2, 2, 8)
train_y = g(train_x) + np.random.randn(train_x.size) * 0.05
x = np.linspace(-2, 2, 100)
plt.plot(train_x, train_y, 'o')
plt.plot(x, g(x), linestyle='dashed')
plt.ylim(-1, 2)
plt.show()
# In[4]:
mu = train_x.mean(axis = 0)
sigma = train_x.std(axis = 0)
def standardize(x):
return (x - mu)/sigma
train_z = standardize(train_x)
def to_matrix(x):
x0 = np.ones([x.shape[0], 1])
x3 = x[:, 0, np.newaxis] ** 2
return np.hstack([x0, x, x3])
# In[5]:
# 创建训练数据的矩阵
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)
# In[6]:
theta = np.random.randn(X.shape[1])
def f(x):
return np.dot(x, theta)
# 不应用正则化的实现
# In[12]:
def E(x,y):
return 0.5 * np.sum((y - f(x)) ** 2)
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
z = standardize(x)
plt.plot(train_z, train_y, 'o')
plt.plot(z, f(to_matrix(z)))
plt.show()
# 应用正则化的实现
# In[14]:
theta1 = theta
theta = np.random.randn(X.shape[1])
LAMBDA = 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
plt.plot(train_z, train_y, 'o')
plt.plot(z, f(to_matrix(z)))
plt.show()