logistic回归的公式:
logistic做图片分类的基本思想:
损失函数公式:
为了让成本函数最小,也就是误差越小,就需要通过更新w和b的值,最小化成本函数J,采用梯度下降的方法。下图可以比较生动形象的理解:
用一个简单二次函数说一下梯度下降法:
logistic梯度下降具体推导:
上文都是一个样本,对于m个样本:
但是显式的for循环会降低效率,可以采用向量化编程的思想,如果有m个x样本,X是这样表示,其他也同理。
##梯度下降核心代码
for i in range(num_iterations):#迭代num_iterations次
A=sigmoid(np.dot(w.T,X)+b)
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)
w = w - learning_rate * dw
b = b - learning_rate * db
#代码
import numpy as np
import matplotlib.pyplot as plt
import h5py
from lr_utils import load_dataset
# Loading the data (cat/non-cat)
train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()
#X_flatten = X.reshape(X.shape [0],-1).T #X.T是X的转置,将形状(a,b,c,d)的矩阵X平铺成形状(b * c * d,a)的矩阵X_flatten
#将训练集的维度降低并转置。
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 / (1 + np.exp(-z))
return s
def initialize_with_zeros(dim):
w=np.zeros(shape=(dim,1))
b=0
return (w , b)
def optimize(w , b , X , Y , num_iterations , learning_rate , print_cost = False):
costs = []
for i in range(num_iterations):
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)))
#反向传播
dw=(1/m)*np.dot(X,(A-Y).T)
db=(1/m)*np.sum(A-Y)
cost = np.squeeze(cost)
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("迭代的次数: %i , 误差值: %f" % (i,cost))
return (w,b, costs)
#计算 Yˆ=A=σ(wTX+b)
#如果A<= 0.5分类值为0,A> 0.5分类值为1
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]):
#将概率a [0,i]转换为实际预测p [0,i]
Y_prediction[0,i] = 1 if A[0,i] > 0.5 else 0
#使用断言
assert(Y_prediction.shape == (1,m))
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])
w,b, costs = optimize(w , b , X_train , Y_train,num_iterations , learning_rate , print_cost)
#预测测试/训练集的例子
Y_prediction_test = predict(w , b, X_test)
Y_prediction_train = predict(w , b, X_train)
#打印训练后的准确性
print("训练集准确性:" , format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100) ,"%")
print("测试集准确性:" , format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100) ,"%")
d = {
"costs" : costs,
"Y_prediction_test" : Y_prediction_test,
"Y_prediciton_train" : Y_prediction_train,
"w" : w,
"b" : b,
"learning_rate" : learning_rate,
"num_iterations" : num_iterations }
return d
print("====================测试model====================")
#这里加载的是真实的数据,请参见上面的代码部分。
d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 2000, learning_rate = 0.005, print_cost = True)
参考博客:
https://blog.csdn.net/u013733326/article/details/79827273
http://www.ai-start.com/