代码之家  ›  专栏  ›  技术社区  ›  Abhishek Kumar

**和pow函数的使用差异

  •  -1
  • Abhishek Kumar  · 技术社区  · 7 年前

    def cost_function(x,y,theta):
        m = np.size(y)
        j = (1/(2*m))*np.sum(np.power(np.matmul(x,theta)-y),2)
        return j
    

    给出错误的成本函数:

    def cost_function(x,y,theta):
            m = np.size(y)
            j = (1/(2*m))*np.sum((np.matmul(x,theta)-y)**2)
            return j
    

    def gradient_descent(x,y,theta,learn_rate,iters):
        x = np.mat(x);y = np.mat(y); theta= np.mat(theta);
        m = np.size(y)
        j_hist = np.zeros(iters)
        for i in range(0,iters):
            temp = theta - (learn_rate/m)*(x.T*(x*theta-y))
            theta = temp
            j_hist[i] = cost_function(x,y,theta)
        return (theta),j_hist
    

    可变值

    theta  = np.zeros((2,1))
    learn_rate = 0.01
    iters = 1000
    x is (97,2) matrix
    y is (97,1) matrix
    

    成本函数计算精细,值为32.0727 在梯度下降中使用相同的函数会产生错误。

    我得到的错误是

    1 回复  |  直到 7 年前
        1
  •  1
  •   hpaulj    7 年前

    首先让我们区分 pow , ** np.power . 战俘 **

    第二,你申请 np.mat 到阵列,使 np.matrix 物体。根据其文件:

    * (矩阵乘法)和 **

    矩阵幂:

    In [475]: np.mat([[1,2],[3,4]])**2
    Out[475]: 
    matrix([[ 7, 10],
            [15, 22]])
    

    In [476]: np.array([[1,2],[3,4]])**2
    Out[476]: 
    array([[ 1,  4],
           [ 9, 16]])
    In [477]: np.power(np.mat([[1,2],[3,4]]),2)
    Out[477]: 
    matrix([[ 1,  4],
            [ 9, 16]])
    

    矩阵幂:

    In [478]: arr = np.array([[1,2],[3,4]])
    In [479]: arr@arr            # np.matmul
    Out[479]: 
    array([[ 7, 10],
           [15, 22]])
    

    In [480]: np.power(np.mat([[1,2]]),2)
    Out[480]: matrix([[1, 4]])             # elementwise
    

    matrix_power 在非平方矩阵上:

    In [481]: np.mat([[1,2]])**2
    ---------------------------------------------------------------------------
    LinAlgError                               Traceback (most recent call last)
    <ipython-input-481-18e19d5a9d6c> in <module>()
    ----> 1 np.mat([[1,2]])**2
    
    /usr/local/lib/python3.6/dist-packages/numpy/matrixlib/defmatrix.py in __pow__(self, other)
        226 
        227     def __pow__(self, other):
    --> 228         return matrix_power(self, other)
        229 
        230     def __ipow__(self, other):
    
    /usr/local/lib/python3.6/dist-packages/numpy/linalg/linalg.py in matrix_power(a, n)
        600     a = asanyarray(a)
        601     _assertRankAtLeast2(a)
    --> 602     _assertNdSquareness(a)
        603 
        604     try:
    
    /usr/local/lib/python3.6/dist-packages/numpy/linalg/linalg.py in _assertNdSquareness(*arrays)
        213         m, n = a.shape[-2:]
        214         if m != n:
    --> 215             raise LinAlgError('Last 2 dimensions of the array must be square')
        216 
        217 def _assertFinite(*arrays):
    
    LinAlgError: Last 2 dimensions of the array must be square
    

    矩阵功率 . 这就是为什么我们经常要求查看整个回溯。

    x y 和 theta 到 ? 这个 cost_function 使用 matmul . 有了这个功能 @ 操作员,使用 np矩阵

    战俘 . 这让我和至少一位评论员感到困惑。我试着找一个 np.pow scipy 版本。