logistics回归是一种二分类问题,采用的激活函数是sigmoid函数,使得输出值转换为(0,1)之间的概率
A = sigmoid(np.dot(w.T, X) + b ) 表示预测函数
dz = A - Y , A 表示的是预测结果, y 表示的是实际结果
cost = -y*logA - (1-y)*log(1-A) #表示损失函数
dw = np.dot(X, dz.T)/m
db = np.sum(dz)/m
w := w - a*dw # 更新w,a 表示学习率
b : = b - a*db #更新b
第一步: 定义lr_utils.py 导入数据
import numpy as np import h5py 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 = np.array(train_dataset['train_set_y'][:]) test_dataset = h5py.File('datasets/test_catvnoncat.h5', 'r') test_set_x_orig = np.array(test_dataset['test_set_x'][:]) test_set_y = np.array(test_dataset['test_set_y'][:]) # 对矩阵预测值的维度构造成(1, train_set_x_orig.shape[0]) 即一张照片一个预测值 train_set_y = train_set_y.reshape((1, train_set_x_orig.shape[0])) test_set_y = test_set_y.reshape((1, test_set_x_orig.shape[0])) classes = np.array(test_dataset["list_classes"][:]) return train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes
第二步:调用函数,进行数据导入
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 train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset() # 查看图片 index = 7 plt.imshow(train_set_x_orig[7]) plt.show() #训练集的图片个数 m_train = train_set_x_orig.shape[0] m_test = test_set_x_orig.shape[0] # 图片的尺寸 num_px = train_set_x_orig.shape[1] # 对每张图片的像素点进行一个竖直排列[x1, x2, x3, x4] train_set_x_flatten = train_set_x_orig.reshape(m_train, -1).T test_set_x_flatten = test_set_x_orig.reshape(m_test, -1).T # 进行像素点归一化 train_set_x = train_set_x_flatten / 255 test_set_x = test_set_x_flatten / 255
第三步:定义sigmoid函数和进行参数初始化
def sigmoid(z): return 1/(1+np.exp(-z)) s = sigmoid(np.array([[0, 2]])) def initialize_with_zeros(dim): w = np.zeros((dim, 1), dtype=float) b = 0.0 return w, b
第四步:定义反向传播函数
def propgate(w, b, X, Y): m = X.shape[1]
# 预测函数 A = sigmoid(np.dot(w.T, X) + b)
# 预测函数与真实值之差 dz = A - Y
# A与w的导数 dw = 1/m * np.dot(X, dz.T)
# b与w的导数 db = 1/m * np.sum(dz, axis=1)
#损失函数 cost = np.sum(-Y * np.log(A) - (1 - Y) * np.log(1 - A), axis=1) / m
#更新参数的幅度 grade = {'dw':dw, 'db':db}
#损失函数 cost = np.squeeze(cost) return cost, grade
第5步:通过反向传播函数, 训练样本
# 迭代优化 def optimize(w, b, X, Y, num_iteration, learning_rate, print_cost):
# w, b 为初始参数, X, Y为训练集的变量和标签, num_iteration为训练次数, learning_rate为学习率, print_cost为是否打印 now_num_iteration = 0 costs = [] for i in range(num_iteration): cost, grade = propgate(w, b, X, Y) dw = grade['dw'] db = grade['db'] w = w - learning_rate*dw b = b - learning_rate*db now_num_iteration += 1 if i%100 == 0: costs.append(cost) if print_cost and i%100: print(cost, i) param = {'w':w, 'b':b} grade = {'dw':dw, 'db':db}
第6步:定义预测函数,使用的是前向传播函数
def predict(w, b, X):
# w,b表示参数,X表示的是输入的图片,y轴表示图片的数目
# m表示预测的图片的数目 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) # 概率大于0.5,预测结果为1 for i in range(A.shape[1]): if A[0, i] > 0.5: Y_prediction[0, i] = 1 return Y_prediction
第7步:定义最终的训练模型, 输出训练的准确度
def model(train_set_x, train_set_y, test_set_x, test_set_y, num_iteration, learning_rate, print_cost): m = train_set_x.shape[0] # 创建初始值 w, b = initialize_with_zeros(m) costs, param, grade = optimize(w, b,train_set_x, train_set_y, num_iteration, learning_rate, print_cost) w = param['w'] b = param['b'] Y_train_prediciton = predict(w, b, train_set_x) Y_test_prediction = predict(w, b, test_set_x) # 输出训练的准确度 print('train_accuracy {}'.format(100 - np.mean(np.abs(Y_train_prediciton - train_set_y))*100)) print('test_accuracy {}'.format(100 - np.mean(np.abs(Y_test_prediction - test_set_y ))*100)) d = {'costs':costs, 'Y_train_prediciton':Y_train_prediciton, 'Y_test_prediction':Y_test_prediction, 'w':w, 'b':b, 'learning_rate':learning_rate, 'num_iteration': num_iteration} return d
第8步:我们挑选一张猫的图片进行预测
# 预测图片 my_image = "cat_1.jpg" # change this to the name of your image file ## END CODE HERE ## # We preprocess the image to fit your algorithm. fname = "images/" + my_image image = np.array(ndimage.imread(fname, flatten=False)) # 重构模型大小, 使得模型的shape为(:,1) my_image = scipy.misc.imresize(image, size=(num_px,num_px)).reshape((1, num_px*num_px*3)).T
# 输出预测的结果 my_predicted_image = 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.")