Python:如何防止Scipy的optimize.minimize函数改变初始猜测的形状x0?

时间:2017-09-30 22:48:56

标签: python numpy optimization scipy minimize

我正在尝试从Scipy实现优化算法。当我实现它而不输入雅可比梯度函数时,它工作正常。我相信当我输入渐变时我得到的问题是因为最小化函数本身正在改变初始猜测x0的形状。您可以从下面代码的输出中看到这一点。

输入:

org.json.JSONException: Value [value of json] of type org.json.JSONArray cannot be converted to JSONObject
09-30 22:25:06.241 17310-17310/com.example.user.kotlinjson W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
09-30 22:25:06.242 17310-17310/com.example.user.kotlinjson W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:160)
09-30 22:25:06.242 17310-17310/com.example.user.kotlinjson W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
09-30 22:25:06.242 17310-17310/com.example.user.kotlinjson W/System.err:     at com.example.smoreno.kotlinprueba.MainActivity$read$stringRequest$1.onResponse(MainActivity.kt:139)
09-30 22:25:06.242 17310-17310/com.example.smoreno.kotlinprueba W/System.err:     at com.example.smoreno.kotlinprueba.MainActivity$read$stringRequest$1.onResponse(MainActivity.kt:22)
09-30 22:25:06.242 17310-17310/com.example.smoreno.kotlinprueba W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
09-30 22:25:06.242 17310-17310/com.example.smoreno.kotlinprueba W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)

输出:

import numpy as np
from costFunction import *
import scipy.optimize as op

def sigmoid(z):

    epsilon = np.finfo(z.dtype).eps

    g = 1/(1+np.exp(-z))
    g = np.clip(g,epsilon,1-epsilon)
    return g

def costFunction(theta,X,y):
    m = y.size
    h = sigmoid(X@theta)
    J = 1/(m)*(-y.T@np.log(h)-(1-y).T@np.log(1-h))
    grad = 1/m*X.T@(h-y)
    print ('Shape of theta is',np.shape(theta),'\n')
    print ('Shape of gradient is',np.shape(grad),'\n')
    return J, grad

X = np.array([[1, 3],[5,7]])
y = np.array([[1],[0]])

m,n = np.shape(X)
one_vec = np.ones((m,1))
X = np.hstack((one_vec,X))
initial_theta = np.zeros((n+1,1))

print ('Running costFunction before executing minimize function...\n')
cost, grad = costFunction(initial_theta,X,y) #To test the shape of gradient before calling minimize

print ('Executing minimize function...\n')
Result = op.minimize(costFunction,initial_theta,args=(X,y),method='TNC',jac=True,options={'maxiter':400})

7 个答案:

答案 0 :(得分:2)

我不会分析你的确切计算,但有些评论:

  • (1)你的渐变被破坏了!
    • scipy希望partial derivative生成一个等于x0的形状数组。
    • 您的渐变形状为(3,2),而(n+1, 1)则为
    • 与使用scipy.optimize.rosen_der(der =衍生)
    • 的教程中给出的示例进行比较
  • (2)看来你的scipy版本有点老了,因为我的(0.19.0)告诉我:
    • ValueError: tnc: invalid gradient vector from minimized function.

来自scipy的一些支持源代码:

if (PyArray_SIZE(arr_grad) != py_state->n)
{
  PyErr_SetString(PyExc_ValueError,
    "tnc: invalid gradient vector from minimized function.");
  goto failure;

备注: 5年前,此代码已更改/触及/介绍。如果您在使用列出的代码时没有收到此错误(删除了costFunction的导入),那么您似乎正在使用scipy&lt; v0.13.0b1,我不推荐!我假设您正在使用一些已弃用的基于Windows的非正式分发与过时的scipy。你应该改变它!

答案 1 :(得分:1)

我有同样的问题,Scipy试图和你做同样的事情。我不明白为什么这可以解决这个问题但是使用数组形状直到它起作用给了我以下内容:

梯度函数定义如下

def Gradient(theta,X,y):
    #Initializing variables
    m = len(y)
    theta = theta[:,np.newaxis]     #<---- THIS IS THE TRICK
    grad = np.zeros(theta.shape)

    #Vectorized computations
    z = X @ theta
    h = sigmoid(z)
    grad = (1/m)*(X.T @ ( h - y));

    return grad    #< --- also works with grad.ravel()

Initial_theta初始化为

initial_theta = np.zeros((n+1))
initial_theta.shape

(3)

即。一个简单的numpy数组而不是列向量。

渐变函数返回

Gradient(initial_theta,X,y).shape

(3,1)或(3,)取决于函数是返回grad还是grad.ravel

scipy.optimize称为

import scipy.optimize as opt
model = opt.minimize(fun = CostFunc, x0 = initial_theta, args = (X, y), method = 'TNC', jac = Gradient)

什么不适用于Scipy

使用initial_theta = np.zeros((n+1))[:,np.newaxis]形状(3,1)的initial_theta崩溃了scipy.minimize函数调用。

  

ValueError:tnc:来自最小化函数的无效梯度向量。

如果有人能澄清这些意义重大的话!感谢

答案 2 :(得分:0)

您的costFunctuion代码错误,也许您应该看一下

def costFunction(theta,X,y):
   h_theta = sigmoid(X@theta)
   J = (-y) * np.log(h_theta) - (1 - y) * np.log(1 - h_theta)
return np.mean(J)

答案 3 :(得分:0)

请复制并粘贴到分隔单元中的jpuiter in1等中

In 1
                        import pandas as pd
                        import numpy as np
                        import matplotlib.pyplot as plt
                        import seaborn as sns
                        %matplotlib inline
                        filepath =('C:/Pythontry/MachineLearning/dataset/couresra/ex2data1.txt')
                        data =pd.read_csv(filepath,sep=',',header=None)
                        #print(data)
                        X = data.values[:,:2]  #(100,2)
                        y = data.values[:,2:3] #(100,1)
                        #print(np.shape(y))
                        #In 2
                        #%% ==================== Part 1: Plotting ====================
                        postive_value = data.loc[data[2] == 1]
                        #print(postive_value.values[:,2:3])
                        negative_value = data.loc[data[2] == 0]
                        #print(len(postive_value))
                        #print(len(negative_value))
                        ax1 = postive_value.plot(kind='scatter',x=0,y=1,s=50,color='b',marker="+",label="Admitted") # S is line width #https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.scatter.html#matplotlib.axes.Axes.scatter 
                        ax2 = negative_value.plot(kind='scatter',x=0,y=1,s=50,color='y',ax=ax1,label="Not Admitted")
                        ax1.set_xlabel("Exam 1 score")
                        ax2.set_ylabel("Exam 2 score")
                        plt.show()
                        #print(ax1 == ax2)
                        #print(np.shape(X))

                # In 3
                        #============ Part 2: Compute Cost and Gradient ===========
                        [m,n] = np.shape(X) #(100,2)
                        print(m,n)
                        additional_coulmn = np.ones((m,1))
                        X = np.append(additional_coulmn,X,axis=1)
                        initial_theta = np.zeros((n+1), dtype=int)
                        print(initial_theta)

                        # In4
                        #Sigmoid and cost function
                        def sigmoid(z):
                            g = np.zeros(np.shape(z));
                            g = 1/(1+np.exp(-z));
                            return g
                        def costFunction(theta, X, y):
                               J = 0;
                               #print(theta)
                               receive_theta = np.array(theta)[np.newaxis] ##This command is used to create the 1D array 
                               #print(receive_theta)
                               theta = np.transpose(receive_theta)
                               #print(np.shape(theta))       
                               #grad = np.zeros(np.shape(theta))
                               z = np.dot(X,theta) # where z = theta*X
                               #print(z)
                               h = sigmoid(z) #formula h(x) = g(z) whether g = 1/1+e(-z) #(100,1)
                               #print(np.shape(h))
                               #J = np.sum(((-y)*np.log(h)-(1-y)*np.log(1-h))/m); 
                               J = np.sum(np.dot((-y.T),np.log(h))-np.dot((1-y).T,np.log(1-h)))/m
                               #J = (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
                               #error = h-y
                               #print(np.shape(error))
                               #print(np.shape(X))
                               grad =np.dot(X.T,(h-y))/m
                               #print(grad)
                               return J,grad
            #In5
                        [cost, grad] = costFunction(initial_theta, X, y)
                        print('Cost at initial theta (zeros):', cost)
                        print('Expected cost (approx): 0.693\n')
                        print('Gradient at initial theta (zeros): \n',grad)
                        print('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n')

            In6 # Compute and display cost and gradient with non-zero theta
            test_theta = [-24, 0.2, 0.2]
            #test_theta_value = np.array([-24, 0.2, 0.2])[np.newaxis]  #This command is used to create the 1D row array 

            #test_theta = np.transpose(test_theta_value) # Transpose 
            #test_theta = test_theta_value.transpose()
            [cost, grad] = costFunction(test_theta, X, y)

            print('\nCost at test theta: \n', cost)
            print('Expected cost (approx): 0.218\n')
            print('Gradient at test theta: \n',grad);
            print('Expected gradients (approx):\n 0.043\n 2.566\n 2.647\n')


        #IN6
    # ============= Part 3: Optimizing using range  =============
    import scipy.optimize as opt
    #initial_theta_initialize = np.array([0, 0, 0])[np.newaxis]
    #initial_theta = np.transpose(initial_theta_initialize)
    print ('Executing minimize function...\n')
    # Working models
    #result = opt.minimize(costFunction,initial_theta,args=(X,y),method='TNC',jac=True,options={'maxiter':400})
    result = opt.fmin_tnc(func=costFunction, x0=initial_theta, args=(X, y))
    # Not working model
    #costFunction(initial_theta,X,y)
    #model = opt.minimize(fun = costFunction, x0 = initial_theta, args = (X, y), method = 'TNC',jac = costFunction)
    print('Thetas found by fmin_tnc function: ', result);
    print('Cost at theta found : \n', cost);
    print('Expected cost (approx): 0.203\n');
    print('theta: \n',result[0]);
    print('Expected theta (approx):\n');
    print(' -25.161\n 0.206\n 0.201\n');

结果: 正在执行最小化功能...

通过fmin_tnc函数发现的Thetas:(array([-25.16131854,0.20623159,0.20147149]),36,0) 找到theta的费用:  0.218330193827 预期费用(约):0.203

theta:  [-25.16131854 0.20623159 0.20147149] 预期theta(大约):

-25.161  0.206  0.201

答案 4 :(得分:0)

  

scipy的fmin_tnc不适用于列或行向量。它期望参数采用数组格式。

Python Implementation of Andrew Ng’s Machine Learning Course (Part 2.1)

opt.fmin_tnc(func = costFunction, x0 = theta.flatten(),fprime = gradient, args = (X, y.flatten()))

答案 5 :(得分:0)

对我有用的是将y重整为向量(1-D)而不是矩阵(2-D数组)。我只使用了以下代码,然后重新运行了SciPy的最小化函数,就可以了。

y = np.reshape(y,100)#例如,如果您的y变量具有100个数据点。

答案 6 :(得分:0)

有点晚了,但是我也开始了安德鲁(Anderw)分配以用Python来实现,并付出了很多努力来解决上述问题。终于对我有用。

blog对我有帮助,但在 fmin_tnc 函数调用中进行了一项更改,请参阅以下内容:-

result = op.fmin_tnc(func=costFunction, x0=initial_theta, fprime=None, approx_grad=True, args=(X, y))here

获得了此信息