【问题标题】:How to get dimensions right using fmin_cg in scipy.optimize如何在 scipy.optimize 中使用 fmin_cg 获得正确的尺寸
【发布时间】:2016-12-14 16:44:58
【问题描述】:

我一直在尝试使用 fmin_cg 来最小化 Logistic 回归的成本函数。

xopt = fmin_cg(costFn, fprime=grad, x0= initial_theta, 
                                 args = (X, y, m), maxiter = 400, disp = True, full_output = True )

这就是我调用 fmin_cg 的方式

这是我的 CostFn:

def costFn(theta, X, y, m):
    h = sigmoid(X.dot(theta))
    J = 0
    J = 1 / m * np.sum((-(y * np.log(h))) - ((1-y) * np.log(1-h)))
    return J.flatten()

这是我的毕业生:

def grad(theta, X, y, m):
    h = sigmoid(X.dot(theta))
    J = 1 / m * np.sum((-(y * np.log(h))) - ((1-y) * np.log(1-h)))
    gg = 1 / m * (X.T.dot(h-y))
    return gg.flatten()

好像是在抛出这个错误:

/Users/sugethakch/miniconda2/lib/python2.7/site-packages/scipy/optimize/linesearch.pyc in phi(s)
     85     def phi(s):
     86         fc[0] += 1
---> 87         return f(xk + s*pk, *args)
     88 
     89     def derphi(s):

ValueError: operands could not be broadcast together with shapes (3,) (300,) 

我知道这与我的尺寸有关。但我似乎无法弄清楚。 我是菜鸟,所以我可能会犯一个明显的错误。

我已阅读此链接:

fmin_cg: Desired error not necessarily achieved due to precision loss

但是,它似乎对我不起作用。

有什么帮助吗?


更新了 X,y,m,theta 的大小

(100, 3) ----> X

(100, 1) -----> 是的

100 ----> 米

(3, 1) ----> θ


这就是我初始化 X,y,m 的方式:

data = pd.read_csv('ex2data1.txt', sep=",", header=None)                        
data.columns = ['x1', 'x2', 'y']                                                       
x1 = data.iloc[:, 0].values[:, None]                                                     
x2 = data.iloc[:, 1].values[:, None]                                                    
y = data.iloc[:, 2].values[:, None]
# join x1 and x2 to make one array of X
X = np.concatenate((x1, x2), axis=1)
m, n = X.shape

ex2data1.txt:

34.62365962451697,78.0246928153624,0
30.28671076822607,43.89499752400101,0
35.84740876993872,72.90219802708364,0
.....

如果有帮助,我正在尝试用 Python 重新编写 Andrew Ng 为 Coursera 的 ML 课程编写的一项家庭作业

【问题讨论】:

  • X,Y,m,theta 的尺寸是多少?也许包括初始化这些变量的代码行。
  • 更新了问题以反映您的 cmets @user2241910
  • 如果您能以完整的可重现示例的形式发布您的代码,将会很有帮助。一个问题是你的目标函数似乎返回一个向量而不是一个标量,因为m 是一个(100,) 数组。
  • 我确实添加了初始化 X,y,m 的代码
  • 目标函数是指costFn()? 'm' 似乎是一个整数,它确实从 costFn() 中返回一个“

标签: python optimization machine-learning scipy gradient


【解决方案1】:

最后,我弄清楚了我最初程序中的问题所在。

我的 'y' 是 (100, 1) 并且 fmin_cg 期望 (100, )。一旦我展平了我的“y”,它就不再抛出最初的错误。但是,优化仍然不起作用。

 Warning: Desired error not necessarily achieved due to precision loss.
     Current function value: 0.693147
     Iterations: 0
     Function evaluations: 43
     Gradient evaluations: 41

这与我在没有优化的情况下获得的结果相同。

我想出优化它的方法是使用“Nelder-Mead”方法。我跟着这个答案:scipy is not optimizing and returns "Desired error not necessarily achieved due to precision loss"

Result = op.minimize(fun = costFn, 
                x0 = initial_theta, 
                args = (X, y, m),
                method = 'Nelder-Mead',
                options={'disp': True})#,
                #jac = grad)

此方法不需要“雅可比”。 我得到了我想要的结果,

Optimization terminated successfully.
     Current function value: 0.203498
     Iterations: 157
     Function evaluations: 287

【讨论】:

    【解决方案2】:

    好吧,因为我不知道你如何初始化mXytheta,所以我不得不做出一些假设。希望我的回答是相关的:

    import numpy as np
    from scipy.optimize import fmin_cg
    from scipy.special import expit
    
    def costFn(theta, X, y, m):
        # expit is the same as sigmoid, but faster
        h = expit(X.dot(theta))
    
        # instead of 1/m, I take the mean
        J =  np.mean((-(y * np.log(h))) - ((1-y) * np.log(1-h)))
        return J #should be a scalar
    
    
    def grad(theta, X, y, m):
        h = expit(X.dot(theta))
        J =  np.mean((-(y * np.log(h))) - ((1-y) * np.log(1-h)))
        gg =  (X.T.dot(h-y))    
        return gg.flatten()
    
    # initialize matrices
    X = np.random.randn(100,3)
    y = np.random.randn(100,) #this apparently needs to be a 1-d vector
    m = np.ones((3,)) # not using m, used np.mean for a weighted sum (see ali_m's comment)
    theta = np.ones((3,1))
    
    xopt = fmin_cg(costFn, fprime=grad, x0=theta, args=(X, y, m), maxiter=400, disp=True, full_output=True )
    

    在代码运行时,我对您的问题了解得不够多,无法知道这是否是您要查找的内容。但希望这可以帮助您更好地理解问题。检查答案的一种方法是使用fprime=None 致电fmin_cg 并查看答案的比较情况。

    【讨论】:

    • 啊。 'y' 是问题所在!我让它工作了,但它仍然没有像预期的那样最小化。由于精度损失,我得到了不一定实现的期望错误。当前函数值:0.693147 迭代次数:0 函数评估:43 梯度评估:41 这是我在没有使用高级优化的情况下得到的!!也许我的衍生品表现不佳?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多