今天我们主要来实现logistic回归,简单的神经网络。
logistic回归的实现
本实验用于通过训练来判断一副图像是否为猫。
在这个过程中,我们将会用到如下库:
numpy:Python科学计算中最重要的库
h5py:Python与H5文件交互的库
mathplotlib:Python画图的库
PIL:Python图像相关的库
scipy:Python科学计算相关的库
数据说明:
对于训练集的标签而言,对于猫,标记为1,否则标记为0。
每一个图像的维度都是(num_px, num_px, 3),其中,长宽相同,3表示是RGB图像。
train_set_x_orig和test_set_x_orig中,包含_orig是由于我们稍候需要对图像进行预处理,预处理后的变量将会命名为train_set_x和train_set_y。
我们将按照如下步骤来实现Logistic:
定义模型结构
初始化模型参数
循环
3.1 前向传播
3.2 反向传递
3.3 更新参数
整合成为一个完整的模型
#!/usr/bin/env python # -*- coding:utf-8 -*- """ @author: Z.y @software: PyCharm Community Edition @time: 2017/10/20 下午9:55 """ import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' import numpy as np import matplotlib.pyplot as plt import h5py import scipy import pylab from PIL import Image from scipy import ndimage def load_dataset(): train_dataset=h5py.File('datasets/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('datasets/test_catvnoncat.h5',"r") test_set_x_org=np.array(test_dataset["test_set_x"][:]) test_set_y_org=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_org=test_set_y_org.reshape((1,test_set_y_org.shape[0])) return train_set_x_orig,train_set_y_orig,test_set_x_org,test_set_y_org,classes train_set_x_orig,train_set_y,test_set_x_orig,test_set_y,classes=load_dataset() m_train=train_set_x_orig.shape[0] m_test=test_set_x_orig.shape[0] num_px=train_set_x_orig.shape[1] print(m_train,m_test,num_px) 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 train_set_x=train_set_x_flatten/255 test_set_x=test_set_x_flatten/255 def sigmoid(z): s=1.0/(1+1/np.exp(z)) return s def initialize_with_zeros(dim): w=np.zeros((dim,1)) b=0 return w,b def propagate(w,b,X,Y): m=X.shape[1] A=sigmoid(np.dot(w.T,X)+b) cost=-1.0/m*np.sum(Y*np.log(A)+(1.0-Y)*np.log(1.0-A)) dw=1.0/m*np.dot(X,(A-Y).T) db=1.0/m*np.sum(A-Y) cost=np.squeeze(cost) grads={ "dw":dw, "db":db } return grads,cost def optimize(w,b,X,Y,num_iterations,learning_rate,print_cost=False): 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 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]=1 else: Y_prediction[0][i]=0 return Y_prediction def model(X_train,Y_train,X_test,Y_test,num_iterations=2000,learning_rate=0.5,print_cost=False): 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=predict(w,b,X_test) Y_prediction_train=predict(w,b,X_train) 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_iteration":num_iterations } return d d=model(train_set_x,train_set_y,test_set_x,test_set_y,num_iterations=2000,learning_rate=0.005,print_cost=True) index = 14 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.") pylab.show()
运行结果(显示的图片略):
/Users/Z.y/anaconda3/bin/python /Users/Z.y/PycharmProjects/tensorflow_test/.idea/Logistic_cat.py
y=[1],it's a cat picture.
209 50 64
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
Cost after iteration 1000:0.214820
Cost after iteration 1100:0.203078
Cost after iteration 1200:0.192544
Cost after iteration 1300:0.183033
Cost after iteration 1400:0.174399
Cost after iteration 1500:0.166521
Cost after iteration 1600:0.159305
Cost after iteration 1700:0.152667
Cost after iteration 1800:0.146542
Cost after iteration 1900:0.140872
train accuracy:99.04306220095694 %
test accuracy:70.0 %
y=0,you predicted that it is a "non-cat" picture.
预测准确率70%,此处不考虑过拟合。