深度学习作业L1W2(1):Python Basics With Numpy v3

本次作业主要是熟悉python和numpy以及简单的向量化。

打印Hello World

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


利用math库求sigmoid

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

然而这个方法应用在np数组上时会出错,math库的exp只能处理单个值。
所以我们应当利用numpy库当中的exp,np.exp可以对数组内的每一个元素单独进行运算。

广播

numpy对于形状相同的两个数组,会依次按位进行加减乘除运算
例如A=np.array([1, 2, 3, 4]) B=np.array([1, 2, 3, 4])
A+B = [2, 4, 6, 8]
A*B = [1, 4, 9, 16]
当两个数组的形状不同时,如果满足条件,会触发广播机制,将较小的数组扩充为较大的一个进行运算,条件如下:
AB在某个维度上的长度要么相等,要么较小的一个为1

例如A =[1, 2, 3, 4], A+1由于1是常数,各个维度都是1,所以A+1=[2, 3, 4, 5]
再比如
A= [[1, 2, 3]
  [4, 5, 6]]
B=[1, 2, 1]
B的第一维长度为1,第二维长度和A相同,所以可以相加结果为
A+B= [[2, 4, 4]
   [5, 7, 7]]
相当于对B进行了复制,加在了A身上

拥有了广播机制,我们很容易就能为一个数组编写sigmoid函数

# 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/(1+np.exp(-x))
    ### END CODE HERE ###
    
    return s


求sigmiod导数:

# 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 = sigmoid(x)
    ds = s*(1-s)
    ### END CODE HERE ###
    
    return ds


修改数组的形状:利用reshape方法,我们可以重新组织数组的行列结构。但是应当注意的是,新数组和原数组共用一块储存空间,会互相影响

# 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 = image.reshape(image.shape[0]*image.shape[1]*image.shape[2],1)
    ### END CODE HERE ###
    
    return v


对数组进行 行标准化,有利于提升梯度下降的速度。我们的做法是先求出每行的范数,每个元素除自己行的范数值。如果输入数组shape为(m,n)话,那么我们的范数应该是(m,1)的数组,利用广播的性质就可以简单进行除法。
利用np.linalg.norm(x,ord = 2, axis = 1,keepdims= True)进行范数的求取,axis=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 = np.linalg.norm(x,ord = 2, axis = 1,keepdims= True)
    
    # Divide x by its norm.
    x = x/x_norm
    ### END CODE HERE ###

    return x


参照题目定义,利用np函数和广播完成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

向量化是一种十分重要的加速手段。原因np自带的向量计算可以充分利用计算机的并行性,从而大幅加速运算,我们在写代码时尽量减少for循环,多采用向量化的形式

1
在这里插入图片描述
要求我们实现这个损失函数,可以直接利用向量减法和np的abs函数求得各项绝对值,再用np.sum求取总和即可

# 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(yhat-y))
    ### END CODE HERE ###
    
    return loss


2
要求我们求在这里插入图片描述
相当于均方误差的n倍,我们可以采用向量内积的方式求取

# 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 = (yhat-y).dot((yhat-y).T)
    ### END CODE HERE ###
    
    return loss
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值