吴恩达Deep Learning Course1 第二周编程作业
吴恩达deep learning Course1 第二周编程作业-Logistic回归
本文参考https://blog.****.net/u013733326/article/details/79639509并加以改写代码和课程所需的数据已上传百度盘https://pan.baidu.com/s/1JswlNVyNVLEEBJma77QP0A
import numpy as np
import matplotlib.pylab as plt
%matplotlib inline
import h5py
#数据预处理
def load_dataset():
train_dataset = h5py.File('train_catvnoncat.h5', "r")
train_set_x_orig = np.array(train_dataset["train_set_x"][:])
train_set_y_orig = np.array(train_dataset["train_set_y"][:])
test_dataset = h5py.File('test_catvnoncat.h5', "r")
test_set_x_orig = np.array(test_dataset["test_set_x"][:])
test_set_y_orig = np.array(test_dataset["test_set_y"][:])
classes = np.array(test_dataset["list_classes"][:])
train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes
train_set_x_orig , train_set_y , test_set_x_orig , test_set_y , classes = load_dataset()
train_set_x=train_set_x_orig.reshape((train_set_x_orig.shape[0],-1)).T
test_set_x=test_set_x_orig.reshape((test_set_x_orig.shape[0],-1)).T
#归一化
train_set_x=train_set_x/255
test_set_x=test_set_x/255
train_set_x.shape,test_set_x.shape
#定义sigomid函数
def sigmoid(z):
return 1/(1+np.exp(-z))
x_1=np.arange(-10,10,0.1)
plt.plot(x_1,sigmoid(x_1),'r')
效果图:
定义成本函数:
def cost_fuc(x,y,w,b):
m=x.shape[1]
A = sigmoid(np.dot(w.T,x) + b) #计算**值,请参考公式2。
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)
return cost,dw,db
实现前向和反向及其梯度:
def gradient(x,y,w,b,alpha,maxIteration,print_cost=False):
costs=[]
for i in range(maxIteration):
cost,dw,db=cost_fuc(x,y,w,b)
w=w-alpha*dw
b=b-alpha*db
if (print_cost) and (i % 100 == 0):
costs.append(cost)
print("迭代的次数: %i , 误差值: %f" % (i,cost))
return w,b,costs
初始化及运行:
w=np.zeros((12288,1))
b=0
w,b,costs=gradient(train_set_x,train_set_y,w,b,0.005,2000,print_cost=True) # alpha取值很重要
迭代的次数: 0 , 误差值: 0.693147
迭代的次数: 100 , 误差值: 0.584508
迭代的次数: 200 , 误差值: 0.466949
迭代的次数: 300 , 误差值: 0.376007
迭代的次数: 400 , 误差值: 0.331463
迭代的次数: 500 , 误差值: 0.303273
迭代的次数: 600 , 误差值: 0.279880
迭代的次数: 700 , 误差值: 0.260042
迭代的次数: 800 , 误差值: 0.242941
迭代的次数: 900 , 误差值: 0.228004
迭代的次数: 1000 , 误差值: 0.214820
迭代的次数: 1100 , 误差值: 0.203078
迭代的次数: 1200 , 误差值: 0.192544
迭代的次数: 1300 , 误差值: 0.183033
迭代的次数: 1400 , 误差值: 0.174399
迭代的次数: 1500 , 误差值: 0.166521
迭代的次数: 1600 , 误差值: 0.159305
迭代的次数: 1700 , 误差值: 0.152667
迭代的次数: 1800 , 误差值: 0.146542
迭代的次数: 1900 , 误差值: 0.140872
预测:
def predict(w , b , X ):
m = X.shape[1] #图片的数量
Y_prediction = np.zeros((m,1))
# w = w.reshape(X.shape[0],1)
#计预测猫在图片中出现的概率
A = sigmoid(np.dot(w.T , X) + b)
A=A.T
for i in range(m):
Y_prediction[i] = 1 if A[i] > 0.5 else 0
return Y_prediction
Y_prediction_test = predict(w , b, test_set_x)
Y_prediction_train = predict(w , b, train_set_x)
#打印训练后的准确性
print("训练集准确性:" , format(100 - np.mean(np.abs(Y_prediction_train - train_set_y.T)) * 100) ,"%")
print("测试集准确性:" , format(100 - np.mean(np.abs(Y_prediction_test - test_set_y.T)) * 100) ,"%")
结果:
训练集准确性: 99.04306220095694 %
测试集准确性: 70.0 %
绘图:
costs=np.squeeze(costs)
plt.plot(costs,'r')