神经网络基础编程作业

导包

1
2
3
4
5
6
7
8
9
import numpy as np
import matplotlib.pyplot as plt
import h5py
import scipy
from PIL import Image
from scipy import ndimage
from lr_utils import load_dataset

%matplotlib inline

加载数据

1
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
1
2
3
m_train = train_set_x_orig.shape[0]
m_test = test_set_x_orig.shape[0]
num_px = test_set_x_orig.shape[1]

转换数据的形状,转换为:每一列代表一个图片的RGB,行代表所有的图片

1
2
3
4
train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T
test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T
print(train_set_x_flatten.shape)
print(test_set_x_flatten.shape)
(12288, 209)
(12288, 50)

将数据集标准化(颜色值都是0-255,要把颜色值转化成0-1)

1
2
train_set_x = train_set_x_flatten/255
test_set_x = test_set_x_flatten/255

sigmoid 函数

1
2
3
def sigmoid(z):
s = 1/(1+np.exp(-z))
return s

初始化w、b(这里是logistics regression)

1
2
3
4
5
6
7
def initialize_with_zeros(dim):
w = np.zeros(shape=(dim,1))
b = 0
assert(w.shape == (dim, 1)) #断言确保维度正确
assert(isinstance(b, float) or isinstance(b, int))

return w,b

向前向后传播

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def propagate(w, b, X, Y):
#向前传播
m = X.shape[1] #样本个数
A = sigmoid(np.dot(w.T, X)+b)
cost = (-1/m)*np.sum(Y*np.log(A)+(1-Y)*np.log(1-A))

#向后传播
dw = 1/m * np.dot(X , (A - Y).T)
db = 1/m * np.sum(A - Y)

assert(dw.shape == w.shape)
assert(db.dtype == float)
cost = np.squeeze(cost)
assert(cost.shape == ())

grads = {"dw": dw, #将dw、db存入字典
"db": db}

return grads,cost

优化w、b

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
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost=False):
"""
num_iterations - 优化循环的迭代次数
learning_rate - 梯度下降更新规则的学习率
print_cost - 每100步打印一次损失值
"""
costs = []
for i in range(num_iterations):
grads, cost = propagate(w,b,X,Y)

dw = grads["dw"]
db = grads["db"]

w = w-learning_rate*dw
b = b-learning_rate*db

if i%100 == 0:
costs.append(cost)
if print_cost and i % 100 == 0:
print("Cost after iteration %i: %f" %(i, cost))

params = {"w": w,
"b": b}

grads = {"dw": dw,
"db": db}

return params,grads,costs

预测

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def predict(w, b, X):
m = X.shape[1]
Y_prediction = np.zeros((1,m)) #每一列对应一张图片
w = w.reshape(X.shape[0], 1) #X.shape[0]代表一章图片的每一个颜色值,要和w权值相乘

A = sigmoid(np.dot(w.T, X)+b) #激活函数将值对应在0-1(logistics regression)

for i in range(A.shape[1]):
if(A[0,i]>0.5):
Y_prediction[0,i]=1
else:
Y_prediction[0,i]=0
assert(Y_prediction.shape == (1, m))
return Y_prediction,A

制作模型

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
def model(X_train, Y_train, X_test, Y_test, num_iterations = 2000, learning_rate = 0.5, print_cost = False):
"""
num_iterations - 优化循环的迭代次数
learning_rate - 梯度下降更新规则的学习率
print_cost - 每100步打印一次损失值
"""
# 初始化
w, b = initialize_with_zeros(X_train.shape[0])

parameters, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)

w = parameters["w"]
b = parameters["b"]

Y_prediction_test, A_test = predict(w, b, X_test)
Y_prediction_train, A_train = predict(w, b, X_train)

#打印错误预测
# Print train/test Errors
print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))
print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))

d = {"costs": costs,
"Y_prediction_test": Y_prediction_test,
"Y_prediction_train" : Y_prediction_train,
"w" : w,
"b" : b,
"learning_rate" : learning_rate,
"num_iterations": num_iterations}

return d
1
d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 2000, learning_rate = 0.004, print_cost = True)
Cost after iteration 0: 0.693147
Cost after iteration 100: 0.506765
Cost after iteration 200: 0.442269
Cost after iteration 300: 0.397201
Cost after iteration 400: 0.362439
Cost after iteration 500: 0.334271
Cost after iteration 600: 0.310725
Cost after iteration 700: 0.290608
Cost after iteration 800: 0.273138
Cost after iteration 900: 0.257771
Cost after iteration 1000: 0.244114
Cost after iteration 1100: 0.231873
Cost after iteration 1200: 0.220823
Cost after iteration 1300: 0.210787
Cost after iteration 1400: 0.201623
Cost after iteration 1500: 0.193217
Cost after iteration 1600: 0.185474
Cost after iteration 1700: 0.178317
Cost after iteration 1800: 0.171678
Cost after iteration 1900: 0.165504
train accuracy: 98.08612440191388 %
test accuracy: 70.0 %
1
2
3
4
5
6
7
# 学习速率对损失函数影响的曲线
costs = np.squeeze(d['costs'])
plt.plot(costs)
plt.ylabel('cost')
plt.xlabel('iterations (per hundreds)')
plt.title("Learning rate =" + str(d["learning_rate"]))
plt.show()

测试自己的图片

1
2
3
4
5
6
7
8
9
10
my_image = "cat2.jpg"

fname = "C:\\Users\\董润泽\\Desktop\\cat_picture\\" + my_image
image = np.array(ndimage.imread(fname, flatten=False))
my_image = scipy.misc.imresize(image, size=(num_px,num_px)).reshape((1, num_px*num_px*3)).T
my_predicted_image,A = predict(d["w"], d["b"], my_image)

plt.imshow(image)
print("y = " + str(np.squeeze(my_predicted_image)) + ", your algorithm predicts a \"" + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") + "\" picture.")
print(A[0][0])
y = 1.0, your algorithm predicts a "cat" picture.
1.0


C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: DeprecationWarning: `imread` is deprecated!
`imread` is deprecated in SciPy 1.0.0.
Use ``matplotlib.pyplot.imread`` instead.
  after removing the cwd from sys.path.
C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: DeprecationWarning: `imresize` is deprecated!
`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.3.0.
Use Pillow instead: ``numpy.array(Image.fromarray(arr).resize())``.
  """


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 2470290795@qq.com

文章标题:神经网络基础编程作业

文章字数:993

本文作者:runze

发布时间:2020-02-12, 15:12:19

最后更新:2020-02-23, 08:29:54

原始链接:http://yoursite.com/2020/02/12/%E5%90%B4%E6%81%A9%E8%BE%BE%20%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/01%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C%E5%92%8C%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C%E5%9F%BA%E7%A1%80%E7%BC%96%E7%A8%8B%E4%BD%9C%E4%B8%9A/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏