函数调用的输出意外尺寸

时间:2018-08-15 17:09:28

标签: python logistic-regression gradient-descent

我似乎无法绕过这个头。我有一个对数回归函数的梯度,如下所示:

import numpy as np
def logreg_grad(x,D,c):
    z = np.diagflat(c).dot(D).dot(x)
    idxN, idxP = z<0, z>=0 # logical indexing
    y1 = [-1 + np.exp(x)/(1+np.exp(x)) for x in z[idxN]]
    y1 = np.array(y1)
    y2 = [-np.exp(-x)/(1+np.exp(-x)) for x in z[idxP]]
    y2 = np.array(y2)

    y = np.empty(z.shape, dtype='float')
    y[idxN] = y1                           # values for negative indices
    y[idxP] = y2                           # values for positive indices

    temp = np.transpose(D).dot(np.diagflat(c)).dot(y); # grad f(CDx) = D'*C'*f'(CDx)
    return temp

D,c的获取方式为:

[D, c] = create_classification_problem(200, 20, 1)

其中

def create_classification_problem(Nd,Nf,kappa):
    D = buildmat(Nd,Nf,kappa)
    w = np.random.randn(Nf,1);
    c = np.sign(D.dot(w));
    flipper = np.ones((Nd,1)) - 2*(np.random.rand(Nd,1)>.9);
    c = c*flipper;
    return D,c

我暂时定义

grad = lambda x:logreg_grad(x,D,c) 
x0 = np.zeros((np.size(D,1),1))

我希望grad(x0)返回一个(20,1)数组(当我逐行浏览该函数时),但是每次它都返回(200,1)。这似乎非常矛盾,但是我敢肯定我在某个地方错了。

0 个答案:

没有答案