Scipy.optimize.fmin_cg给出TypeError:-:'tuple'和'tuple'的不受支持的操作数类型

时间:2019-03-25 19:16:15

标签: numpy machine-learning scipy

我正在python中建立一个oneVsAll分类器,并使用scipy.optimize.fmin_cg获得theta向量的最佳值。这是我的分类器功能。

def oneVsAll(X, y, K, reg_parameter):
    X = np.hstack((np.ones((X.shape[0],1)), X))
    theta = initialiseTheta((K,X.shape[1]))
    print(theta[0].shape)
    for i in range(K):
        print("In for loop")
        digit_class = i if i else 10
        theta[i] = opt.fmin_cg(f = lrCostFunction, fprime=None, x0=theta[i], args = (X, (y==digit_class).flatten() , reg_parameter), maxiter=50)

    return theta

这是我的成本函数

def lrCostFunction(theta, X, y, reg_parameter):
    m = y.shape[0]  #number of training examples
    J =0
    grad = np.zeros(theta.shape)
    Z = np.dot(X, theta)
    print("Z shape", Z.shape)
    hx = sigmoid(Z)
    print("hx - y shape", (hx-y).shape)
    print("X shape",(X[:,0].T).shape )
    print("dot product shape", np.dot(X[:,0].T, (hx -y)))
    J = -(1/m)*(np.sum(np.multiply(y, np.log(hx)) + np.multiply((1-y), np.log(1-hx)))) + (reg_parameter/(2*m))*(np.sum(np.power(theta[1:], 2)))
    grad_unregularized = (1/m)*(np.dot(X[:,1:].T, (hx -y)))
    grad[0] = (1/m)*(np.dot(X[:,0].T, (hx -y)))
    grad[1:] = grad_unregularized + (reg_parameter/m)*(theta[1:])

    return (J, grad)

0 个答案:

没有答案