-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7-3 minist (L2).py
More file actions
165 lines (126 loc) · 4.43 KB
/
Copy path7-3 minist (L2).py
File metadata and controls
165 lines (126 loc) · 4.43 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
"""
test on dataset
hypothesis: relu -> softmax
loss function: cross entropy
Created on Tue Aug 31 10:08:39 2021
@author: lee
"""
import numpy as np
import matplotlib.pyplot as plt
import idx2numpy
#%% 数据生成
def deal_x(x):
return x.reshape(-1, 28*28) / 255
def deal_y(y):
_y = np.zeros((y.shape[0], 10))
_y[np.arange(y.shape[0]), y] = 1
return _y
data_set = 'mnist' # mnist or fmnist
train_x = idx2numpy.convert_from_file('data/'+ data_set + '/train-images-idx3-ubyte')
train_y = idx2numpy.convert_from_file('data/'+ data_set + '/train-labels-idx1-ubyte')
test_x = idx2numpy.convert_from_file('data/'+ data_set + '/t10k-images-idx3-ubyte')
test_y = idx2numpy.convert_from_file('data/'+ data_set + '/t10k-labels-idx1-ubyte')
train_x = deal_x(train_x)
test_x = deal_x(test_x)
train_y = deal_y(train_y)
test_y = deal_y(test_y)
data_size = train_x.shape[0]
#%% 参数设定(略显不合理的初始化)
w = np.random.randn(784, 128)
v = np.random.randn(128, 10)
#%% 超参数设定
train_count = 10000
batch_size = 128
lr = 1e-3
gamma = 1e-1
#%% 假说函数
def relu(x):
if not hasattr(relu, 'mask'):
relu.mask = np.zeros_like(x)
relu.mask = (x > 0)
return x * relu.mask
def softmax(x):
offset = np.max(x, 1)
x = x - offset.reshape(-1, 1)
x = np.exp(x)
return x/np.sum(x, 1).reshape(-1, 1)
def calHypo(x, theta):
_w, _v = theta[0], theta[1]
z_hat = np.matmul(x, _w)
z = relu(z_hat)
h_hat = np.matmul(z, _v)
h = softmax(h_hat)
return h
#%% 损失函数
def calLoss(x, y, theta):
h = calHypo(x, theta)
loss = - np.log(h[np.arange(x.shape[0]), np.argmax(y, 1)]) # 交叉熵损失
print(np.sum(loss)/y.shape[0])
loss += 0.5 * gamma * (np.sum(theta[0]**2) + np.sum(theta[1]**2)) # L2正则项
print(np.sum(loss)/y.shape[0], np.mean(np.abs(theta[0])), np.mean(np.abs(theta[1])))
print("*"*10)
return np.sum(loss)/y.shape[0]
#%% 准确率
def accuracy(x, y, theta):
h = calHypo(x, theta)
compare = (np.argmax(h, 1) == np.argmax(y, 1))
return np.sum(compare)/x.shape[0]
#%% 显示
fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)
ax2 = fig.add_subplot(1, 2, 2)
test_acc_list = []
train_acc_list = []
test_loss_list = []
train_loss_list = []
#%% 训练
for i in range(1, train_count + 1):
mask = np.random.choice(data_size, batch_size)
batch_train_x, batch_train_y = train_x[mask], train_y[mask]
# 正向传播
z_hat = np.matmul(batch_train_x, w)
z = relu(z_hat)
h_hat = np.matmul(z, v)
h = softmax(h_hat)
# 反向传播更新参数
grad_v = np.matmul(z.T, (h - batch_train_y)) + gamma*v
grad_w = np.matmul(batch_train_x.T, (np.matmul((h - batch_train_y), v.T) * relu.mask)) + gamma*w
v = v - lr * grad_v
w = w - lr * grad_w
if i % (train_count // 20) == 0:
train_acc = accuracy(train_x, train_y, [w, v])
train_acc_list.append(train_acc)
test_acc = accuracy(test_x, test_y, [w, v])
test_acc_list.append(test_acc)
ax1.cla()
ax1.plot(range(len(train_acc_list)), train_acc_list, label='train')
ax1.plot(range(len(test_acc_list)), test_acc_list, label='test')
ax1.set_ylabel('accuracy')
ax1.legend()
ax1.set_title('test acc: ' + str(test_acc) + '\ntrain acc' + str(train_acc))
train_loss = calLoss(train_x, train_y, [w, v])
train_loss_list.append(train_loss)
test_loss = calLoss(test_x, test_y, [w, v])
test_loss_list.append(test_loss)
ax2.cla()
ax2.plot(range(len(train_loss_list)), train_loss_list, label='train')
ax2.plot(range(len(test_loss_list)), test_loss_list, label='test')
ax2.set_ylabel('loss')
ax2.legend()
ax2.set_title('train_count: ' + str(i) + '/' + str(train_count))
plt.pause(0.1)
#%% 单例测试
dic = {0:'T-shirt', 1:'Trouser', 2:'Pullover', 3:'Dress', 4:'Coat', 5:'Sandal',
6:'Shirt', 7:'Sneaker', 8:'Bag', 9:'Ankle boot'}
def testPicture():
index = np.random.randint(0, 10000)
data = test_x[index].reshape(1, -1)
h = calHypo(data, [w, v])
h = np.argmax(h)
img = data.reshape((28, 28))
plt.imshow(img)
if data_set == 'mnist':
plt.title(h)
else:
plt.title(dic[h])