课程1 神经网络和深度学习
第2周 具有神经网络思维的Logistic回归 L1W2
我是参考此博文,完成该作业。
https://www.heywhale.com/mw/project/5dd23dbf00b0b900365ecef1
我是在这个博主下的资源
https://blog.csdn.net/u013733326/article/details/79639509
完整的代码实现(过程讲解和练习看上面的链接)
进入你资源包所在的文件夹
cd D:\software\config\jupytercode\L1W2
D:\software\config\jupytercode\L1W2
import numpy as np
import matplotlib.pyplot as plt
import h5py
from lr_utils import load_dataset
%matplotlib inline
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
标准化
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: " + str(train_set_x_flatten.shape))
print ("train_set_y shape: " + str(train_set_y.shape))
print ("test_set_x_flatten shape: " + str(test_set_x_flatten.shape))
print ("test_set_y shape: " + str(test_set_y.shape))
print ("sanity check after reshaping: " + str(train_set_x_flatten[0:5,0]))
train_set_x_flatten shape: (12288, 209)
train_set_y shape: (1, 209)
test_set_x_flatten shape: (12288, 50)
test_set_y shape: (1, 50)
sanity check after reshaping: [17 31 56 22 33]
train_set_x = train_set_x_flatten/255
test_set_x = test_set_x_flatten/255
(12288, 50)
实现sigmoid函数
def sigmoid(z):
s = 1/(1+np.exp(-z))
return s
初始化参数w,b
def initialize_with_zeros(dim):
w = np.zeros((dim,1))
b = 0
assert(w.shape==(dim,1))
assert(isinstance(b,float) or isinstance(b,int))
return w,b
向前向后宣传,就是求dw和db还有损失
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))
dz = A-Y
dw = 1/m*np.dot(X,dz.T)
db = 1/m*np.sum(dz)
assert(dw.shape==w.shape)
assert(db.dtype == float)
cost = np.squeeze(cost)
assert(cost.shape==())
grads = {
"dw":dw,
"db":db
}
return grads,cost
计算梯度下降
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = True):
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:
print("Cost after iteration %i: %f" % (i,cost))
params = {
"w":w,
"b":b
}
return params,grads,cost
预测,返回预测向量Y_predict
def predict(w, b, X):
m = X.shape[1]
Y_prediction = np.zeros((1,m))
w = w.reshape(X.shape[0],1)
A = sigmoid(np.dot(w.T,X)+b)
for i in range(A.shape[1]):
if A[0,i]<=0.5:
Y_prediction[0,i]=0
else:
Y_prediction[0,i]=1
assert(Y_prediction.shape==(1,m))
return Y_prediction
将上述主函数initialize_with_zeros初始化,optimize梯度下降,predict预测三个函数整合在一起
def model(X_train, Y_train, X_test, Y_test, num_iterations = 5000, learning_rate = 0.5, print_cost = False):
w,b = initialize_with_zeros(X_train.shape[0])
params,grads,cost = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)
w = params["w"]
b = params["b"]
Y_prediction_train = predict(w, b, X_train)
Y_prediction_test = predict(w, b, X_test)
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 = {
"cost":cost,
"Y_prediction_train":Y_prediction_train,
"Y_prediction_test":Y_prediction_test,
"w":w,
"b":b,
"learning_rate":learning_rate,
"num_iterations":num_iterations
}
return d
调用函数对数组图片进行训练预测
d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 1000, learning_rate = 0.005, print_cost = True)
Cost after iteration 0: 0.693147
Cost after iteration 100: 0.584508
Cost after iteration 200: 0.466949
Cost after iteration 300: 0.376007
Cost after iteration 400: 0.331463
Cost after iteration 500: 0.303273
Cost after iteration 600: 0.279880
Cost after iteration 700: 0.260042
Cost after iteration 800: 0.242941
Cost after iteration 900: 0.228004
train accuracy: 96.65071770334929 %
test accuracy: 72.0 %
检验成果,测试图片是否为猫(准确率不太高,有些测出来是错误的)
index =27
num_px = train_set_x_orig.shape[1]
plt.imshow(test_set_x[:,index].reshape((num_px, num_px, 3)))
print ("y = " + str(test_set_y[0,index]) + ", you predicted that it is a \"" + classes[int(d["Y_prediction_test"][0,index])].decode("utf-8") + "\" picture.")
y = 0, you predicted that it is a “non-cat” picture.