Logistic 梯度下降在 Python fmin_tnc 中不收敛
Logistic gradient Descent not converging in Python fmin_tnc
我一直在学习 python 中的逻辑梯度下降教程。
这是 link:
http://www.johnwittenauer.net/machine-learning-exercises-in-python-part-3/
他的 ipython 笔记本 github 用于此特定练习:
https://github.com/jdwittenauer/ipython-notebooks/blob/master/notebooks/ml/ML-Exercise2.ipynb
这是我针对此问题的代码:
import pandas as pd
import matplotlib.pylab as plt
import numpy as np
import scipy.optimize as opt
def sigmoid(Z):
'''Compute the sigmoid function '''
return 1.0 / (1.0 + np.exp( -1.0 * Z))
###########################################
def compute_cost(theta,X,y, learningRate):
'''compute cost given '''
theta = np.matrix(theta)
X = np.matrix(X)
y = np.matrix(y)
m = y.size
theta0 = np.zeros((1,X.shape[1]))
theta0[0,1:] = theta[0,1:]
reg = np.dot((learningRate/2*m),(theta0.T.dot(theta0)))
Z = X.dot(theta.T)
hypothesis = sigmoid(Z)
exp1 = (-y.T.dot(np.log(hypothesis)))
exp2 = ((1.0 - y).T.dot(np.log(1.0 - hypothesis)))
J = (exp1 - exp2).dot(1/m)
return J.sum() + reg.sum()
def grad(theta,X,y,learningRate):
theta = theta.T
X = np.matrix(X)
y = np.matrix(y)
m = y.shape[0]
theta0 = np.zeros(X.shape[1])
theta0[1:] = theta[1:]
theta = np.matrix(theta)
theta0 = np.matrix(theta0)
reg = np.dot(learningRate / m, theta)
Z = X.dot(theta.T)
hypothesis = sigmoid(Z)
error = hypothesis - y
grad = np.dot((X.T.dot(error).flatten()),1/m) + reg
grad= grad.flatten()
grad
##
def predict(theta, X):
probability = sigmoid(X * theta.T)
return [1 if (x >= 0.5) else 0 for x in probability]
以下是代码的调用方式:
data2 = pd.read_csv('ex2data2.txt', header=None, names=['Test 1', 'Test 2', 'Accepted'])
y = data2[data2.columns[-1]].as_matrix()
m = len(y)
y = y.reshape(m, 1)
X = data2[data2.columns[:-1]]
X = X.as_matrix()
_lambda = 1
from sklearn.preprocessing import PolynomialFeatures
#Get all high order parameters
feature_mapper = PolynomialFeatures(degree=6)
X = feature_mapper.fit_transform(X)
# convert to numpy arrays and initalize the parameter array theta
theta = np.zeros(X.shape[1])
learningRate = 1
compute_cost(theta, X, y, learningRate)
result = opt.fmin_tnc(func=compute_cost,x0=theta,fprime=grad,args= (X,y,learningRate))
对于一个变量,一切都运行良好,但是对于更多的特性(练习 2),它运行不佳。在使用优化的梯度下降函数(fmin_tnc)之前,一切都完全相同。
不知何故,即使他的代码也没有收敛到预期值。他是他的博客示例,显示了 fmin_tnc
的结果
但是如果你按照他的代码的每一步操作,你会得到以下结果:
好吧,如您所见,它有点不同。但我在他的代码中发现了不同的东西。他删除了 2 列 'Test 1' 和 'Test 2' 并仅保留高阶参数。这感觉很奇怪,因为在 Andrew Ng 的解决方案中他没有删除 table 的任何列,但他使用了 28 个特征 。这个仅使用 11 个特征。我找到了其他代码,我希望我的 cost_function 和渐变函数能够工作。我相信他们陷入了某个局部最小值并且没有收敛。
我最后一次尝试使用所有 28 个功能,就像 Andrew 的 dataFrame 一样。遗憾的是,我得到了不同的结果,如下所示:
如您所见,我获得了更高的准确度,但我的成本仍然高于预期,即:0.52900
我的意图不是降低博客的代码质量。我仍在按照他在其他教程中的步骤进行操作,这似乎是一个很好的来源。
下面是我的代码的 link ,我正在使用 fmin_tnc 就像他正在做的那样。我刚刚创建了一个更矢量化的 gradient_function。文件名是逻辑回归 Regularized.py
Github: https://github.com/vinipachecov/Machine-Learning/tree/master/Logistic%20Regression
问题是我用的是python 3.6,而作者用的是python 2.7.X。将版本更改为 python 2.7.13 解决了这个问题。
我一直在学习 python 中的逻辑梯度下降教程。
这是 link:
http://www.johnwittenauer.net/machine-learning-exercises-in-python-part-3/
他的 ipython 笔记本 github 用于此特定练习:
https://github.com/jdwittenauer/ipython-notebooks/blob/master/notebooks/ml/ML-Exercise2.ipynb
这是我针对此问题的代码:
import pandas as pd
import matplotlib.pylab as plt
import numpy as np
import scipy.optimize as opt
def sigmoid(Z):
'''Compute the sigmoid function '''
return 1.0 / (1.0 + np.exp( -1.0 * Z))
###########################################
def compute_cost(theta,X,y, learningRate):
'''compute cost given '''
theta = np.matrix(theta)
X = np.matrix(X)
y = np.matrix(y)
m = y.size
theta0 = np.zeros((1,X.shape[1]))
theta0[0,1:] = theta[0,1:]
reg = np.dot((learningRate/2*m),(theta0.T.dot(theta0)))
Z = X.dot(theta.T)
hypothesis = sigmoid(Z)
exp1 = (-y.T.dot(np.log(hypothesis)))
exp2 = ((1.0 - y).T.dot(np.log(1.0 - hypothesis)))
J = (exp1 - exp2).dot(1/m)
return J.sum() + reg.sum()
def grad(theta,X,y,learningRate):
theta = theta.T
X = np.matrix(X)
y = np.matrix(y)
m = y.shape[0]
theta0 = np.zeros(X.shape[1])
theta0[1:] = theta[1:]
theta = np.matrix(theta)
theta0 = np.matrix(theta0)
reg = np.dot(learningRate / m, theta)
Z = X.dot(theta.T)
hypothesis = sigmoid(Z)
error = hypothesis - y
grad = np.dot((X.T.dot(error).flatten()),1/m) + reg
grad= grad.flatten()
grad
##
def predict(theta, X):
probability = sigmoid(X * theta.T)
return [1 if (x >= 0.5) else 0 for x in probability]
以下是代码的调用方式:
data2 = pd.read_csv('ex2data2.txt', header=None, names=['Test 1', 'Test 2', 'Accepted'])
y = data2[data2.columns[-1]].as_matrix()
m = len(y)
y = y.reshape(m, 1)
X = data2[data2.columns[:-1]]
X = X.as_matrix()
_lambda = 1
from sklearn.preprocessing import PolynomialFeatures
#Get all high order parameters
feature_mapper = PolynomialFeatures(degree=6)
X = feature_mapper.fit_transform(X)
# convert to numpy arrays and initalize the parameter array theta
theta = np.zeros(X.shape[1])
learningRate = 1
compute_cost(theta, X, y, learningRate)
result = opt.fmin_tnc(func=compute_cost,x0=theta,fprime=grad,args= (X,y,learningRate))
对于一个变量,一切都运行良好,但是对于更多的特性(练习 2),它运行不佳。在使用优化的梯度下降函数(fmin_tnc)之前,一切都完全相同。
不知何故,即使他的代码也没有收敛到预期值。他是他的博客示例,显示了 fmin_tnc
的结果
但是如果你按照他的代码的每一步操作,你会得到以下结果:
好吧,如您所见,它有点不同。但我在他的代码中发现了不同的东西。他删除了 2 列 'Test 1' 和 'Test 2' 并仅保留高阶参数。这感觉很奇怪,因为在 Andrew Ng 的解决方案中他没有删除 table 的任何列,但他使用了 28 个特征 。这个仅使用 11 个特征。我找到了其他代码,我希望我的 cost_function 和渐变函数能够工作。我相信他们陷入了某个局部最小值并且没有收敛。
我最后一次尝试使用所有 28 个功能,就像 Andrew 的 dataFrame 一样。遗憾的是,我得到了不同的结果,如下所示:
如您所见,我获得了更高的准确度,但我的成本仍然高于预期,即:0.52900
我的意图不是降低博客的代码质量。我仍在按照他在其他教程中的步骤进行操作,这似乎是一个很好的来源。
下面是我的代码的 link ,我正在使用 fmin_tnc 就像他正在做的那样。我刚刚创建了一个更矢量化的 gradient_function。文件名是逻辑回归 Regularized.py
Github: https://github.com/vinipachecov/Machine-Learning/tree/master/Logistic%20Regression
问题是我用的是python 3.6,而作者用的是python 2.7.X。将版本更改为 python 2.7.13 解决了这个问题。