美文网首页我爱编程Computer Science
吴恩达编程作业及答案(一)

吴恩达编程作业及答案(一)

作者: vincewi | 来源:发表于2018-03-13 18:11 被阅读0次

    Python Basics with Numpy

    Hello World

    Exercise: Set test to "Hello World" in the cell below to print "Hello World" and run the two cells below.

    ### START CODE HERE ### (≈ 1 line of code)
    #test = None
    test = "Hello World"
    ### END CODE HERE ###
    
    print ("test: " + test)
    

    Expected output: test: Hello World

    1. Building basic functions with numpy

    1.1 sigmoid function, np.exp()

    sigmoid function.png

    Exercise: Build a function that returns the sigmoid of a real number x. Use math.exp(x) for the exponential function.

    # 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 = None
        s = 1 / (1 + math.exp(-x))
        ### END CODE HERE ###
        
        return s
    
    basic_sigmoid(3)
    

    Expected Output: 0.9525741268224334

    Exercise: Implement the sigmoid function using numpy.

    # 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 = None
        s = 1 / (1 + np.exp(-x))
        ### END CODE HERE ###
        
        return s
    
    x = np.array([1, 2, 3])
    sigmoid(x)
    

    Expected Output: array([ 0.73105858, 0.88079708, 0.95257413])

    1.2 Sigmoid gradient

    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)=σ′(x)=σ(x)(1−σ(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 σ′(x)=s(1−s)
    # GRADED FUNCTION: sigmoid_derivative
    
    def sigmoid_derivative(x):
        """
        Compute the gradient (also called the slope or derivative) of the sigmoid function with respect to its input x.
        You can store the output of the sigmoid function into variables and then use it to calculate the gradient.
        
        Arguments:
        x -- A scalar or numpy array
    
        Return:
        ds -- Your computed gradient.
        """
        
        ### START CODE HERE ### (≈ 2 lines of code)
        #s = None
        #ds = None
        s = 1 / (1 + np.exp(-x))
        ds = s * (1 - s)
        ### END CODE HERE ###
        
        return ds
    
    x = np.array([1, 2, 3])
    print ("sigmoid_derivative(x) = " + str(sigmoid_derivative(x)))
    

    Expected Output:sigmoid_derivative(x) = [ 0.19661193 0.10499359 0.04517666]

    1.3 Reshaping arrays

    Exercise: Implement image2vector() that takes an input of shape (length, height, 3) and returns a vector of shape (lengthheight3, 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.
    # GRADED FUNCTION: image2vector
    def image2vector(image):
        """
        Argument:
        image -- a numpy array of shape (length, height, depth)
        
        Returns:
        v -- a vector of shape (length*height*depth, 1)
        """
        
        ### START CODE HERE ### (≈ 1 line of code)
        v = None
        v = image.reshape((image.shape[0]*image.shape[1]*image.shape[2], 1))
        ### END CODE HERE ###
        
        return v
    
    # This is a 3 by 3 by 2 array, typically images will be (num_px_x, num_px_y,3) where 3 represents the RGB values
    image = np.array([[[ 0.67826139,  0.29380381],
            [ 0.90714982,  0.52835647],
            [ 0.4215251 ,  0.45017551]],
    
           [[ 0.92814219,  0.96677647],
            [ 0.85304703,  0.52351845],
            [ 0.19981397,  0.27417313]],
    
           [[ 0.60659855,  0.00533165],
            [ 0.10820313,  0.49978937],
            [ 0.34144279,  0.94630077]]])
    
    print ("image2vector(image) = " + str(image2vector(image)))
    

    Expected Output: image2vector(image) = [[ 0.67826139]
    [ 0.29380381]
    [ 0.90714982]
    [ 0.52835647]
    [ 0.4215251 ]
    [ 0.45017551]
    [ 0.92814219]
    [ 0.96677647]
    [ 0.85304703]
    [ 0.52351845]
    [ 0.19981397]
    [ 0.27417313]
    [ 0.60659855]
    [ 0.00533165]
    [ 0.10820313]
    [ 0.49978937]
    [ 0.34144279]
    [ 0.94630077]]

    1.4 Normalizing rows

    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)
        # Compute x_norm as the norm 2 of x. Use np.linalg.norm(..., ord = 2, axis = ..., keepdims = True)
        #x_norm = None
        x_norm = np.linalg.norm(x, ord = 2, axis = 1, keepdims = True)
        # Divide x by its norm.
        #x = None
        x = x / x_norm
        ### END CODE HERE ###
    
        return x
    
    x = np.array([
        [0, 3, 4],
        [1, 6, 4]])
    print("normalizeRows(x) = " + str(normalizeRows(x)))
    

    Expected Output: normalizeRows(x) = [[ 0. 0.6 0.8 ]
    [ 0.13736056 0.82416338 0.54944226]]

    1.5 Broadcasting and the softmax function

    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.

    # 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 = None
        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 = None
        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 = None
        s = x_exp / x_sum
    
        ### END CODE HERE ###
        
        return s
    
    x = np.array([
        [9, 2, 5, 0, 0],
        [7, 5, 0, 0 ,0]])
    print("softmax(x) = " + str(softmax(x)))
    

    Expected Output:softmax(x) = [[ 9.80897665e-01 8.94462891e-04 1.79657674e-02 1.21052389e-04
    1.21052389e-04]
    [ 8.78679856e-01 1.18916387e-01 8.01252314e-04 8.01252314e-04
    8.01252314e-04]]

    2 Vectorization

    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.

    # 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 = None
        loss = np.sum(np.abs(yhat-y))
        ### END CODE HERE ###
        
        return loss
    
    yhat = np.array([.9, 0.2, 0.1, .4, .9])
    y = np.array([1, 0, 0, 1, 1])
    print("L1 = " + str(L1(yhat,y)))
    

    Expected Output:L1 = 1.1

    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.

    # 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 = None
        loss = np.sum(np.dot(y-yhat, y-yhat))
        ### END CODE HERE ###
        
        return loss
    
    yhat = np.array([.9, 0.2, 0.1, .4, .9])
    y = np.array([1, 0, 0, 1, 1])
    print("L2 = " + str(L2(yhat,y)))
    

    Expected Output: L2 = 0.43

    相关文章

      网友评论

        本文标题:吴恩达编程作业及答案(一)

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