np.dot

作者: 水星no1 | 来源:发表于2018-09-29 10:43 被阅读0次
    import numpy as np
    a = np.array([1,2,3,4,5])
    np.exp(a)
    np.log(a)
    np.abs(a)
    np.square(yhat-y)
    np.sum, np.dot, np.multiply, np.maximum, etc...
    print np.maximum(a,3)
    >>>[3 3 3 4 5]
    a/3
    >>>array([0, 0, 1, 1, 1])
    a.shape
    >>>(5,)
    np.sum(a)
    >>> 15
    
    np.zeros((2,1))
    >>>array([[0.],
           [0.]])
    

    broadcasting

    np.dot(w, x)+b
    # 如果b是实数,会根据前一项的结果将其变成向量
    
    a = np.array([
        [1.,2.,3.],
        [2.,3.,4.],
        [4.,5.,6.]
    ])
    np.sum(a,axis=0) # 垂直方向求和,axis=1水平方向求和
    >>>array([ 7., 10., 13.])
    percentage = 100*a/np.sum(a,axis=0).reshape(1,3)
    percentage
    >>>array([[14.28571429, 20.        , 23.07692308],
           [28.57142857, 30.        , 30.76923077],
           [57.14285714, 50.        , 46.15384615]])
    
    np.random.randn(1,5)
    # 不要使用,这样维度不确定。
    np.random.randn(5) + reshape
    # 
    assert(a.shape==(1,5))
    
    
    Suppose img is a (32,32,3) array, representing a 32x32 image with 3 color channels red, green and blue. How do you reshape this into a column vector?
    img.reshape((32*32*3,1))
    

    按照行归一化

    x_norm = np.linalg.norm(x,axis=1,keepdims=True)
     x = x/x_norm
    
    image.png
    image.png

    softmax函数,常用在对多分类结果进行归一化。

    # GRADED FUNCTION: softmax
    
    def softmax(x):
        """Calculates the softmax for each row of the input x.
    
        Your code should work for a row vector and also for matrices of shape (n, m).
    
        Argument:
        x -- A numpy matrix of shape (n,m)
    
        Returns:
        s -- A numpy matrix equal to the softmax of x, of shape (n,m)
        """
        
        ### START CODE HERE ### (≈ 3 lines of code)
        # Apply exp() element-wise to x. Use np.exp(...).
        x_exp = np.exp(x)
    
        # Create a vector x_sum that sums each row of x_exp. Use np.sum(..., axis = 1, keepdims = True).
        x_sum = np.sum(x_exp,axis=1,keepdims=True)
        
        # Compute softmax(x) by dividing x_exp by x_sum. It should automatically use numpy broadcasting.
        s = x_exp/x_sum
    
        ### END CODE HERE ###
        
        return s
    
    image.png
    Note that np.dot() performs a matrix-matrix or matrix-vector multiplication. This is different from np.multiply() and the * operator (which is equivalent to .* in Matlab/Octave), which performs an element-wise multiplication.
    

    相关文章

      网友评论

          本文标题:np.dot

          本文链接:https://www.haomeiwen.com/subject/fwmmoftx.html