美文网首页
Python Basics with Numpy

Python Basics with Numpy

作者: 小企鹅吃黄鱼 | 来源:发表于2019-07-21 15:46 被阅读0次

    1. Building basic functions with numpy

    1.1 - sigmoid function, np.exp()

      在使用np.exp()之前,我们先用math.exp()来实现sigmoid函数: sigmoid(x)=\frac{1}{1+e^{-x}}

    # GRADED FUNCTION: basic_sigmoid
    
    import math
    
    def basic_sigmoid(x):
        """
        Compute sigmoid of x.
    
        Arguments:
        x -- A scalar
    
        Return:
        s -- sigmoid(x)
        """
        
        ### START CODE HERE ### (≈ 1 line of code)
        s = 1.0/(1+math.exp(-x))
        ### END CODE HERE ###
        
        return s
    

      实际上,在深度学习中,我们经常用矩阵啊,向量啊,所以我们很少使用“math”库,用“numpy”比较多。
      如果,x=(x_{1},x_{2},...,x_{n})是一个行向量,np.exp(x)则会对x中的每个元素进行指数运算,输出的结果为np.exp(x)=(e^{x_{1}},e^{x_{2}},...,e^{x_{n}})

    \text{For } x \in \mathbb{R}^n \text{, } sigmoid(x) = sigmoid\begin{pmatrix} x_1 \\ x_2 \\ ... \\ x_n \\ \end{pmatrix} = \begin{pmatrix} \frac{1}{1+e^{-x_1}} \\ \frac{1}{1+e^{-x_2}} \\ ... \\ \frac{1}{1+e^{-x_n}} \\ \end{pmatrix}\

    # GRADED FUNCTION: sigmoid
    
    import numpy as np # this means you can access numpy functions by writing np.function() instead of numpy.function()
    
    def sigmoid(x):
        """
        Compute the sigmoid of x
    
        Arguments:
        x -- A scalar or numpy array of any size
    
        Return:
        s -- sigmoid(x)
        """
        
        ### START CODE HERE ### (≈ 1 line of code)
        s = 1.0/(1+np.exp(-x))
        ### END CODE HERE ###
        
        return s
    

    1.2 - Sigmoid gradient

    As you've seen in lecture, you will need to compute gradients to optimize loss functions using backpropagation. Let's code your first gradient function.

    Exercise: Implement the function sigmoid_grad() to compute the gradient of the sigmoid function with respect to its input x. The formula is: sigmoid\_derivative(x) = \sigma'(x) = \sigma(x) (1 - \sigma(x))\
    You often code this function in two steps:

    1. Set s to be the sigmoid of x. You might find your sigmoid(x) function useful.
    2. Compute \sigma'(x) = s(1-s)

    1.3 - Reshaping arrays

    Two common numpy functions used in deep learning are np.shape and np.reshape().

    • X.shape is used to get the shape (dimension) of a matrix/vector X.
    • X.reshape(...) is used to reshape X into some other dimension.

    For example, in computer science, an image is represented by a 3D array of shape (length, height, depth = 3). However, when you read an image as the input of an algorithm you convert it to a vector of shape (length*height*3, 1). In other words, you "unroll", or reshape, the 3D array into a 1D vector.

    Exercise: Implement image2vector() that takes an input of shape (length, height, 3) and returns a vector of shape (length*height*3, 1). For example, if you would like to reshape an array v of shape (a, b, c) into a vector of shape (a*b,c) you would do:

    v = v.reshape((v.shape[0]*v.shape[1], v.shape[2])) # v.shape[0] = a ; v.shape[1] = b ; v.shape[2] = c
    
    • Please don't hardcode the dimensions of image as a constant. Instead look up the quantities you need with image.shape[0], etc.

    1.4 - Normalizing rows

    Another common technique we use in Machine Learning and Deep Learning is to normalize our data. It often leads to a better performance because gradient descent converges faster after normalization. Here, by normalization we mean changing x to \frac{x}{\| x\|} (dividing each row vector of x by its norm).

    For example, if x = \begin{bmatrix} 0 & 3 & 4 \\ 2 & 6 & 4 \\ \end{bmatrix}\ then \| x\| = np.linalg.norm(x, axis = 1, keepdims = True) = \begin{bmatrix} 5 \\ \sqrt{56} \\ \end{bmatrix}\and x\_normalized = \frac{x}{\| x\|} = \begin{bmatrix} 0 & \frac{3}{5} & \frac{4}{5} \\ \frac{2}{\sqrt{56}} & \frac{6}{\sqrt{56}} & \frac{4}{\sqrt{56}} \\ \end{bmatrix}\ Note that you can divide matrices of different sizes and it works fine: this is called broadcasting and you're going to learn about it in part 5.

    Exercise: Implement normalizeRows() to normalize the rows of a matrix. After applying this function to an input matrix x, each row of x should be a vector of unit length (meaning length 1).

    # GRADED FUNCTION: normalizeRows
    
    def normalizeRows(x):
        """
        Implement a function that normalizes each row of the matrix x (to have unit length).
        
        Argument:
        x -- A numpy matrix of shape (n, m)
        
        Returns:
        x -- The normalized (by row) numpy matrix. You are allowed to modify x.
        """
        
        ### START CODE HERE ### (≈ 2 lines of code)
        x_norm = np.linalg.norm(x,axis=1,keepdims=True)
        
        # Divide x by its norm.
        x = x/x_norm
        ### END CODE HERE ###
    
        return x
    

    1.5 - Broadcasting and the softmax function

    A very important concept to understand in numpy is "broadcasting". It is very useful for performing mathematical operations between arrays of different shapes. For the full details on broadcasting, you can read the official broadcasting documentation.

    Exercise: Implement a softmax function using numpy. You can think of softmax as a normalizing function used when your algorithm needs to classify two or more classes. You will learn more about softmax in the second course of this specialization.

    Instructions:
    \text{for } x \in \mathbb{R}^{1\times n} \text{, } softmax(x) = softmax(\begin{bmatrix} x_1 && x_2 && ... && x_n \end{bmatrix}) = \begin{bmatrix} \frac{e^{x_1}}{\sum_{j}e^{x_j}} && \frac{e^{x_2}}{\sum_{j}e^{x_j}} && ... && \frac{e^{x_n}}{\sum_{j}e^{x_j}} \end{bmatrix}

    softmax(x) = softmax\begin{bmatrix} x_{11} & x_{12} & x_{13} & \dots & x_{1n} \\ x_{21} & x_{22} & x_{23} & \dots & x_{2n} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ x_{m1} & x_{m2} & x_{m3} & \dots & x_{mn} \end{bmatrix} = \begin{bmatrix} \frac{e^{x_{11}}}{\sum_{j}e^{x_{1j}}} & \frac{e^{x_{12}}}{\sum_{j}e^{x_{1j}}} & \frac{e^{x_{13}}}{\sum_{j}e^{x_{1j}}} & \dots & \frac{e^{x_{1n}}}{\sum_{j}e^{x_{1j}}} \\ \frac{e^{x_{21}}}{\sum_{j}e^{x_{2j}}} & \frac{e^{x_{22}}}{\sum_{j}e^{x_{2j}}} & \frac{e^{x_{23}}}{\sum_{j}e^{x_{2j}}} & \dots & \frac{e^{x_{2n}}}{\sum_{j}e^{x_{2j}}} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ \frac{e^{x_{m1}}}{\sum_{j}e^{x_{mj}}} & \frac{e^{x_{m2}}}{\sum_{j}e^{x_{mj}}} & \frac{e^{x_{m3}}}{\sum_{j}e^{x_{mj}}} & \dots & \frac{e^{x_{mn}}}{\sum_{j}e^{x_{mj}}} \end{bmatrix} = \begin{pmatrix} softmax\text{(first row of x)} \\ softmax\text{(second row of x)} \\ ... \\ softmax\text{(last row of x)} \\ \end{pmatrix}

    # 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
    

    1.6 Vectorization

    As you may have noticed, the vectorized implementation is much cleaner and more efficient. For bigger vectors/matrices, the differences in running time become even bigger.

    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.

    2.1 Implement the L1 and L2 loss functions

    Exercise: Implement the numpy vectorized version of the L1 loss. You may find the function abs(x) (absolute value of x) useful.

    Reminder:

    • The loss is used to evaluate the performance of your model. The bigger your loss is, the more different your predictions (\hat{y}) are from the true values (y). In deep learning, you use optimization algorithms like Gradient Descent to train your model and to minimize the cost.
    • L1 loss is defined as:
      \begin{align*} & L_1(\hat{y}, y) = \sum_{i=0}^m|y^{(i)} - \hat{y}^{(i)}| \end{align*}\
    # GRADED FUNCTION: L1
    
    def L1(yhat, y):
        """
        Arguments:
        yhat -- vector of size m (predicted labels)
        y -- vector of size m (true labels)
        
        Returns:
        loss -- the value of the L1 loss function defined above
        """
        
        ### START CODE HERE ### (≈ 1 line of code)
        loss = np.sum(np.abs(y-yhat))
        ### END CODE HERE ###
        
        return loss
    

    Exercise: Implement the numpy vectorized version of the L2 loss. There are several way of implementing the L2 loss but you may find the function np.dot() useful. As a reminder, if x = [x_1, x_2, ..., x_n], then np.dot(x,x) = \sum_{j=0}^n x_j^{2}.

    • L2 loss is defined as \begin{align*} & L_2(\hat{y},y) = \sum_{i=0}^m(y^{(i)} - \hat{y}^{(i)})^2 \end{align*}\
    # GRADED FUNCTION: L2
    
    def L2(yhat, y):
        """
        Arguments:
        yhat -- vector of size m (predicted labels)
        y -- vector of size m (true labels)
        
        Returns:
        loss -- the value of the L2 loss function defined above
        """
        
        ### START CODE HERE ### (≈ 1 line of code)
        loss = np.sum(np.dot(y-yhat,y-yhat))
        ### END CODE HERE ###
        
        return loss
    

    相关文章

      网友评论

          本文标题:Python Basics with Numpy

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