- 批量梯度下降法
使用矩阵的形式实现的批量梯度下降法
"""
Logistic Regression
by batch gradient descent
auther:Daniel He
date:2021-06-26
"""
import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
class LogisticRegression(object):
def __init__(self, learning_rate=0.01, max_iter=100):
self.learning_rate = learning_rate
self.max_iter = max_iter
def sigmoid(self,z):
return 1.0 / (1.0 + np.exp(-z))
def fit(self,X,y):
self.X = X
self.y = y
self.row, self.col = self.X.shape
self.theta = np.ones(self.col)
self.lr = np.ones(self.col) * self.learning_rate
"""参数的更新没有用到目标函数(损失函数)值,计算损失函数仍需要代价"""
for i in range(self.max_iter):
gradient = (self.sigmoid(self.X @ self.theta) - self.y) @ self.X
self.theta -= self.learning_rate * gradient
if __name__ == '__main__':
X,y = datasets.make_blobs(n_features=2,n_samples=500,centers=2,cluster_std=[3,3],random_state=1)
X = np.hstack((np.ones(X.shape[0]).reshape((-1,1)),X))
print(X)
lr = LogisticRegression()
lr.fit(X=X,y=y)
theta = lr.theta
print(theta)
plt.scatter(X[:,1],X[:,2],c=y)
a = np.arange(-15,4,0.2)
b = (-theta[0]-theta[1]*a)/theta[2]
plt.plot(a,b)
plt.show()
- miniBatch Gradient Descent
"""
Logistic Regression
by nimibatch gradient descent
auther:Daniel He
date:2021-06-26
"""
import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
import time
class LogisticRegression(object):
def __init__(self, learning_rate=0.01, max_iter=100):
self.learning_rate = learning_rate
self.max_iter = max_iter
def sigmoid(self,z):
return 1.0 / (1.0 + np.exp(-z))
def fit(self,X,y):
self.X = X
self.y = y
self.row, self.col = self.X.shape
self.theta = np.ones(self.col)
# self.lr = np.ones(self.col) * self.learning_rate
"""参数的更新没有用到目标函数(损失函数)值,计算损失函数仍需要代价"""
index = [_ for _ in range(self.row)]
batch_size = int(self.row / 10)
for i in range(self.max_iter):
"""这里的梯度没有做平均处理(标准情况是要做平均处理的)"""
# ids_batch = np.random.choice(index,size=batch_size,replace=False)
gradient = (self.sigmoid(self.X @ self.theta) - self.y) @ self.X
self.theta -= self.learning_rate * gradient
class LogisticRegression_minibatch(object):
def __init__(self, learning_rate=0.3, max_iter=200):
self.learning_rate = learning_rate
self.max_iter = max_iter
def sigmoid(self,z):
return 1.0 / (1.0 + np.exp(-z))
def fit(self,X,y):
self.X = X
self.y = y
self.row, self.col = self.X.shape
self.theta = np.ones(self.col)
# self.lr = np.ones(self.col) * self.learning_rate
"""参数的更新没有用到目标函数(损失函数)值,计算损失函数仍需要代价"""
index = [_ for _ in range(self.row)]
batch_size = int(self.row / 20)
for i in range(self.max_iter):
"""这里的梯度没有做平均处理(标准情况是要做平均处理的)"""
ids_batch = np.random.choice(index,size=batch_size,replace=False)
gradient = (self.sigmoid(self.X[ids_batch] @ self.theta) - self.y[ids_batch]) @ self.X[ids_batch]
self.theta -= self.learning_rate * gradient
if __name__ == '__main__':
X,y = datasets.make_blobs(n_features=2,n_samples=5000,centers=2,cluster_std=[3,3],random_state=1)
X = np.hstack((np.ones(X.shape[0]).reshape((-1,1)),X))
# print(X)
#########################
Start_time = time.time()
# lr = LogisticRegression()
lr = LogisticRegression_minibatch()
lr.fit(X=X,y=y)
theta = lr.theta
End_time = time.time()
print("消耗时间==",End_time-Start_time)
##########################
plt.scatter(X[:,1],X[:,2],c=y)
a = np.arange(-15,4,0.2)
b = (-theta[0]-theta[1]*a)/theta[2]
plt.plot(a,b)
plt.show()
minibatch 模式并没有比批量形式快!而且效果也下降了。
参考:
https://zhuanlan.zhihu.com/p/74874291
https://www.cnblogs.com/BYRans/p/4713624.html
https://www.cnblogs.com/pinard/p/6029432.html