Skip to content

Commit ea8bf89

Browse files
Star WeStar We
authored andcommitted
Reformate code and imports
1 parent c7533ba commit ea8bf89

File tree

7 files changed

+403
-123
lines changed

7 files changed

+403
-123
lines changed

lesson4/train_with_numpy.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
import planar_utils
2626

27-
#定义函数:设置网络结构
27+
28+
# 定义函数:设置网络结构
2829
def layer_sizes(X, Y):
2930
"""
3031
参数含义:
@@ -36,11 +37,12 @@ def layer_sizes(X, Y):
3637
n_h -- 隐藏层节点数
3738
n_y -- 输出层节点数
3839
"""
39-
n_x = X.shape[0] #输入层大小(节点数)
40+
n_x = X.shape[0] # 输入层大小(节点数)
4041
n_h = 4
41-
n_y = Y.shape[0] #输出层大小(节点数)
42+
n_y = Y.shape[0] # 输出层大小(节点数)
4243
return (n_x, n_h, n_y)
4344

45+
4446
# 定义函数:初始化参数
4547
def initialize_parameters(n_x, n_h, n_y):
4648
"""
@@ -57,9 +59,9 @@ def initialize_parameters(n_x, n_h, n_y):
5759
b2 -- (输出层)偏移量,维度是 (n_y, 1)
5860
"""
5961

60-
np.random.seed(2) # 设置随机种子
62+
np.random.seed(2) # 设置随机种子
6163

62-
#随机初始化参数
64+
# 随机初始化参数
6365
W1 = np.random.randn(n_h, n_x) * 0.01
6466
b1 = np.zeros((n_h, 1))
6567
W2 = np.random.randn(n_y, n_h) * 0.01
@@ -76,6 +78,7 @@ def initialize_parameters(n_x, n_h, n_y):
7678
"b2": b2}
7779
return parameters
7880

81+
7982
# 定义函数:前向传播
8083
def forward_propagation(X, parameters):
8184
"""
@@ -97,7 +100,7 @@ def forward_propagation(X, parameters):
97100
Z2 = np.dot(W2, A1) + b2
98101
A2 = 1 / (1 + np.exp(-Z2))
99102

100-
assert(A2.shape == (1, X.shape[1]))
103+
assert (A2.shape == (1, X.shape[1]))
101104

102105
cache = {"Z1": Z1,
103106
"A1": A1,
@@ -106,6 +109,7 @@ def forward_propagation(X, parameters):
106109

107110
return A2, cache
108111

112+
109113
# 定义函数:成本函数
110114
def compute_cost(A2, Y, parameters):
111115
"""
@@ -120,17 +124,18 @@ def compute_cost(A2, Y, parameters):
120124
cost -- 成本函数
121125
"""
122126

123-
m = Y.shape[1] #样本个数
127+
m = Y.shape[1] # 样本个数
124128

125-
#计算成本
129+
# 计算成本
126130
logprobs = np.multiply(np.log(A2), Y) + np.multiply(np.log(1 - A2), 1 - Y)
127-
cost = -1. / m * np.sum(logprobs)
131+
cost = -1. / m * np.sum(logprobs)
128132

129-
cost = np.squeeze(cost) # 确保维度的正确性
130-
assert(isinstance(cost, float))
133+
cost = np.squeeze(cost) # 确保维度的正确性
134+
assert (isinstance(cost, float))
131135

132136
return cost
133137

138+
134139
# 定义函数:后向传播
135140
def backward_propagation(parameters, cache, X, Y):
136141
"""
@@ -145,7 +150,7 @@ def backward_propagation(parameters, cache, X, Y):
145150
"""
146151
m = X.shape[1]
147152

148-
#首先从"parameters"获取W1,W2
153+
# 首先从"parameters"获取W1,W2
149154
W1 = parameters["W1"]
150155
W2 = parameters["W2"]
151156

@@ -156,10 +161,10 @@ def backward_propagation(parameters, cache, X, Y):
156161
# 后向传播: 计算dW1, db1, dW2, db2.
157162
dZ2 = A2 - Y
158163
dW2 = 1. / m * np.dot(dZ2, A1.T)
159-
db2 = 1. / m * np.sum(dZ2, axis = 1, keepdims = True)
164+
db2 = 1. / m * np.sum(dZ2, axis=1, keepdims=True)
160165
dZ1 = np.dot(W2.T, dZ2) * (1 - np.power(A1, 2))
161166
dW1 = 1. / m * np.dot(dZ1, X.T)
162-
db1 = 1. / m * np.sum(dZ1, axis = 1, keepdims = True)
167+
db1 = 1. / m * np.sum(dZ1, axis=1, keepdims=True)
163168

164169
grads = {"dW1": dW1,
165170
"db1": db1,
@@ -168,7 +173,8 @@ def backward_propagation(parameters, cache, X, Y):
168173

169174
return grads
170175

171-
#定义函数:参数更新
176+
177+
# 定义函数:参数更新
172178
def update_parameters(parameters, grads, learning_rate=1.2):
173179
"""
174180
使用梯度更新参数
@@ -180,7 +186,7 @@ def update_parameters(parameters, grads, learning_rate=1.2):
180186
返回值:
181187
parameters -- 包含更新后参数的python
182188
"""
183-
#从"parameters"中读取全部参数
189+
# 从"parameters"中读取全部参数
184190
W1 = parameters["W1"]
185191
b1 = parameters["b1"]
186192
W2 = parameters["W2"]
@@ -192,7 +198,7 @@ def update_parameters(parameters, grads, learning_rate=1.2):
192198
dW2 = grads["dW2"]
193199
db2 = grads["db2"]
194200

195-
#更新参数
201+
# 更新参数
196202
W1 = W1 - learning_rate * dW1
197203
b1 = b1 - learning_rate * db1
198204
W2 = W2 - learning_rate * dW2
@@ -206,7 +212,7 @@ def update_parameters(parameters, grads, learning_rate=1.2):
206212
return parameters
207213

208214

209-
#定义函数:神经网络模型
215+
# 定义函数:神经网络模型
210216
def nn_model(X, Y, n_h, num_iterations=10000, print_cost=False):
211217
"""
212218
参数:
@@ -224,35 +230,35 @@ def nn_model(X, Y, n_h, num_iterations=10000, print_cost=False):
224230
n_x = layer_sizes(X, Y)[0]
225231
n_y = layer_sizes(X, Y)[2]
226232

227-
#根据n_x, n_h, n_y初始化参数,并取出W1,b1,W2,b2
233+
# 根据n_x, n_h, n_y初始化参数,并取出W1,b1,W2,b2
228234
parameters = initialize_parameters(n_x, n_h, n_y)
229235
W1 = parameters["W1"]
230236
b1 = parameters["b1"]
231237
W2 = parameters["W2"]
232238
b2 = parameters["b2"]
233239

234-
235240
for i in range(0, num_iterations):
236241

237-
#前向传播, 输入: "X, parameters". 输出: "A2, cache".
242+
# 前向传播, 输入: "X, parameters". 输出: "A2, cache".
238243
A2, cache = forward_propagation(X, parameters)
239244

240-
#成本计算. 输入: "A2, Y, parameters". 输出: "cost".
245+
# 成本计算. 输入: "A2, Y, parameters". 输出: "cost".
241246
cost = compute_cost(A2, Y, parameters)
242247

243-
#后向传播, 输入: "parameters, cache, X, Y". 输出: "grads".
248+
# 后向传播, 输入: "parameters, cache, X, Y". 输出: "grads".
244249
grads = backward_propagation(parameters, cache, X, Y)
245250

246-
#参数更新. 输入: "parameters, grads". 输出: "parameters".
251+
# 参数更新. 输入: "parameters, grads". 输出: "parameters".
247252
parameters = update_parameters(parameters, grads)
248253

249-
#每1000次训练打印一次成本函数值
254+
# 每1000次训练打印一次成本函数值
250255
if print_cost and i % 1000 == 0:
251256
print ("Cost after iteration %i: %f" % (i, cost))
252257

253258
return parameters
254259

255-
#定义函数:预测
260+
261+
# 定义函数:预测
256262
def predict(parameters, X):
257263
"""
258264
使用训练所得参数,对每个训练样本进行预测
@@ -265,13 +271,14 @@ def predict(parameters, X):
265271
predictions -- 模型预测值向量(红色: 0 / 蓝色: 1)
266272
"""
267273

268-
#使用训练所得参数进行前向传播计算,并将模型输出值转化为预测值(大于0.5视作1,即True)
274+
# 使用训练所得参数进行前向传播计算,并将模型输出值转化为预测值(大于0.5视作1,即True)
269275
A2, cache = forward_propagation(X, parameters)
270276
predictions = A2 > 0.5
271277

272278
return predictions
273279

274-
#定义函数:main函数
280+
281+
# 定义函数:main函数
275282
def main():
276283
"""
277284
参数:
@@ -284,14 +291,15 @@ def main():
284291
# 预测训练集
285292
predictions = predict(parameters, train_x)
286293
# 输出准确率
287-
print('Train Accuracy: %d' % float((np.dot(train_y, predictions.T)+
294+
print('Train Accuracy: %d' % float((np.dot(train_y, predictions.T) +
288295
np.dot(1 - train_y, 1 - predictions.T)) /
289296
float(train_y.size) * 100) + '%')
290-
#预测测试集
297+
# 预测测试集
291298
predictions = predict(parameters, test_x)
292299
print('Test Accuracy: %d' % float((np.dot(test_y, predictions.T) +
293300
np.dot(1 - test_y, 1 - predictions.T)) /
294301
float(test_y.size) * 100) + '%')
295302

303+
296304
if __name__ == '__main__':
297305
main()

lesson4/train_with_paddle.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
7.预测并测试准确率train_accuracy和test_accuracy
2020
"""
2121

22-
2322
import matplotlib
2423
import numpy as np
2524
import paddle.v2 as paddle
25+
2626
matplotlib.use('Agg')
2727
import matplotlib.pyplot as plt
2828

@@ -204,6 +204,7 @@ def test_accuracy(probs_test, test_data):
204204

205205
return test_accuracy
206206

207+
207208
# 搭建神经网络结构
208209
def netconfig():
209210
"""
@@ -234,6 +235,7 @@ def netconfig():
234235

235236
return image, y_predict, y_label
236237

238+
237239
# 展示模型训练曲线
238240
def plot_costs(costs):
239241
"""
@@ -265,7 +267,7 @@ def main():
265267
# 载入数据
266268
load_data()
267269

268-
#配置网络结构
270+
# 配置网络结构
269271
image, y_predict, y_label = netconfig()
270272

271273
# 定义成本函数为交叉熵损失函数multi_binary_label_cross_entropy_cost
@@ -338,9 +340,9 @@ def event_handler(event):
338340
print("train_accuracy: {} %".format(train_accuracy(probs_train, train_data)))
339341
print("test_accuracy: {} %".format(test_accuracy(probs_test, test_data)))
340342

341-
#绘制成本函数折线图
343+
# 绘制成本函数折线图
342344
plot_costs(costs)
343345

344346

345347
if __name__ == '__main__':
346-
main()
348+
main()

lesson5/train_with_numpy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ def main():
5656
print('Test accuracy:')
5757
pred_test = dnn_app_utils_v2.predict(test_x, test_y, parameters)
5858

59+
5960
if __name__ == '__main__':
6061
main()
61-

lesson5/train_with_paddle.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,12 @@
1919
7.预测并测试准确率train_accuracy和test_accuracy
2020
"""
2121

22-
import sys
22+
import matplotlib
2323
import numpy as np
24-
2524
import paddle.v2 as paddle
26-
import h5py
27-
import scipy
28-
import matplotlib
25+
2926
matplotlib.use('Agg')
3027
import matplotlib.pyplot as plt
31-
from scipy import ndimage
32-
from PIL import Image
3328

3429
from lr_utils import load_dataset
3530

@@ -289,7 +284,7 @@ def main():
289284
# 载入数据
290285
load_data()
291286

292-
#构建神经网络结构
287+
# 构建神经网络结构
293288
image, y_predict, y_label = netconfig()
294289

295290
# 定义成本函数为交叉熵损失函数multi_binary_label_cross_entropy_cost
@@ -366,4 +361,4 @@ def event_handler(event):
366361

367362

368363
if __name__ == '__main__':
369-
main()
364+
main()

0 commit comments

Comments
 (0)