吴恩达作业1:逻辑回归实现猫的分类
生活随笔
收集整理的這篇文章主要介紹了
吴恩达作业1:逻辑回归实现猫的分类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
思路:輸入樣本X與隨機初始權重W相乘,利用sigmoid激活函數輸出值,對于二分類問題,用交叉熵損失函數來計算損失值,通過交叉熵損失函數利用鏈式法則求出W和b的偏導,梯度下降更新W和b即可,(梯度下降又有很多,Momentum,Adam等后面在詳細介紹)剩下的就是迭代次數和學習率的問題。
第一課作業直接給了數據集,無須對數據集操作,下面是讀取數據集的代碼,數據集鏈接https://download.csdn.net/download/fanzonghao/10539175
命名為:lr_utils.py
?
? import numpy as np import h5pydef load_dataset():train_dataset = h5py.File('datasets/train_catvnoncat.h5', "r")train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set featurestrain_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labelstest_dataset = h5py.File('datasets/test_catvnoncat.h5', "r")test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set featurestest_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labelsclasses = np.array(test_dataset["list_classes"][:]) # the list of classestrain_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 if __name__ == '__main__':train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes=load_dataset()print('訓練樣本數={}'.format(train_set_x_orig.shape))print('訓練樣本對應的標簽={}'.format(train_set_y_orig.shape))print('前10張訓練樣本標簽={}'.format(train_set_y_orig[:,:10]))print('測試樣本數={}'.format(test_set_x_orig.shape))print('測試樣本對應的標簽={}'.format(test_set_y_orig.shape))print('{}'.format(classes))可看見打印結果:209個樣本,64x64x3。
?
?下面通過測試代碼看看標簽0 1 代表的是什么
?
import cv2 from lr_utils import load_dataset train_set_x_orig,train_set_y,test_set_x_orig,test_set_y,classes=load_dataset() cv2.imshow('img0',train_set_x_orig[0]) cv2.waitKey() cv2.imshow('img2',train_set_x_orig[2]) cv2.waitKey()可知0代表不是貓,1代表是貓。
由于訓練的標簽結果是Y=(1,209),X將其拉成一個樣本一行向量,X=(209,64*64*3)又W*X=Y,故權重W為(64*64*3,1),最終采用的是樣本X=(64*64*3,209),W=(64*64*3,1),計算過程中W要采用轉置。
先初始化權重W,激活函數采用sigmoid,輸出值A;損失函數采用交叉熵,通過鏈式法則反向求W和b的導數,在更新W和b即可。計算過程中,注意維度的統一,可用assert 判斷。
代碼如下:
? import numpy as np from lr_utils import load_dataset from matplotlib import pyplot as plt """ 函數功能:邏輯回歸實現小貓分類 """ import cv2 #sigmoid激活函數 def sigmoid(z):s=1.0/(1+np.exp(-z))return s #初始化權值 def initialize_zeros(dim):w=np.zeros(dim).reshape(dim,1)b=0return w,b ######w(64*64*3,1)#傳播過程 def propagate(w,b,X,Y):m=X.shape[1]A=sigmoid(np.dot(w.T,X)+b)assert np.dot(w.T,X).shape==Y.shapecost=-1/m*(np.dot(Y,np.log(A).T)+np.dot((1-Y),np.log(1-A).T))dw=1/m*(np.dot(X,(A-Y).T))db= 1 / m * (np.sum(A-Y))grads={'dw':dw,'db': db}cost=np.squeeze(cost)return cost,grads ''' 函數功能:更新權重 +迭代次數+學習率 返回最終更新的權重和損失值 ''' def optimize(w,b,X,Y,num_iterations,learning_rate,print_cost=False):costs=[]for i in range(num_iterations):cost, grads = propagate(w, b, X, Y)dw=grads['dw']db=grads['db']w = w - learning_rate * dwb = b - learning_rate * dbif i%100==0:costs.append(cost)if print_cost and i%100==0:print('after iteration %i:%f'%(i,cost))params={'w':w,'b':b}grads = {'dw': dw,'db': db}return params,grads,costs """ 函數功能:實現利用更新好的權重預測小貓 """ 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]=1else:Y_prediction[0,i]=0return Y_prediction """ 函數功能:測試函數,在編寫過程中,檢查W和b的更新,最終注銷掉,不調用 """ def test():dim = 2w, b = initialize_zeros(dim)print('initialize w,b=', w, b)w, b, X, Y = np.array([[1], [2]]), 2, np.array([[1, 2], [3, 4]]), np.array([[1, 0]])cost, grads = propagate(w, b, X, Y)print('cost=', cost)print('dw=', grads['dw'])print('db=', grads['db'])params, grads, costs = optimize(w, b, X, Y, num_iterations=100, learning_rate=0.009, print_cost=False)print('w', params['w'])print('b', params['b'])print('iterations dw=', grads['dw'])print('iterations db=', grads['db'])print('costs=', costs)Y_prediction = predict(w, b, X)print('Y_prediction=', Y_prediction)def model(X_train,Y_train,X_test,Y_test,num_iterations,learning_rate,print_cost):w,b=initialize_zeros(X_train.shape[0])params, grads,costs=optimize(w,b,X_train,Y_train,num_iterations,learning_rate,print_cost=True)Y_prediction_train=predict(params['w'],params['b'],X_train)Y_prediction_test = predict(params['w'], params['b'], X_test)print('train accuracy is {}'.format(np.mean(Y_prediction_train==Y_train)))print('test accuracy is {}'.format(np.mean(Y_prediction_test==Y_test)))d = {"costs":costs,'w':w,'b':b,'Y_prediction_train':Y_prediction_train,'Y_prediction_test':Y_prediction_test,'learning_rate':learning_rate,'num_iterations':num_iterations}return d if __name__=='__main__':#test()train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()##traintrain_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0],train_set_x_orig.shape[1] *train_set_x_orig.shape[2] * 3).Ttrain_set_x = train_set_x_flatten / 255.train_set_y_flatten = train_set_y.reshape(train_set_y.shape[0], -1)###testtest_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0],test_set_x_orig.shape[1]* test_set_x_orig.shape[2] * 3).Ttest_set_x = test_set_x_flatten / 255.test_set_y_flatten = test_set_y.reshape(test_set_y.shape[0], -1)d=model(train_set_x,train_set_y_flatten,test_set_x,test_set_y_flatten,num_iterations=2000,learning_rate=0.002,print_cost=False)#paint costs lineplt.plot(d['costs'])#print(d['costs'])plt.xlabel('iteration')plt.ylabel('cost')plt.show()#用自帶的小貓檢測img=cv2.imread('images/my_image2.jpg')imgsize = cv2.resize(img, (64, 64), interpolation=cv2.INTER_CUBIC)cv2.imshow('imgsize', imgsize)cv2.waitKey(0)my_cat=np.array(imgsize.reshape(-1,1))#print(my_cat.shape)My_cat_prediction=predict(d['w'], d['b'], my_cat)print('My_cat_prediction',My_cat_prediction)打印如下:
測試精度還行,由于樣本量少,小貓還是預測錯了。
?
?
?
?
?
?
?
總結
以上是生活随笔為你收集整理的吴恩达作业1:逻辑回归实现猫的分类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android之自定义属性
- 下一篇: pycharm远程连接服务器(docke