您的位置:首页 > 理论基础 > 计算机网络

《神经网络和深度学习》之神经网络基础(第二周)课后作业——Python与Numpy基础知识

2018-01-30 01:41 1101 查看

1 用numpy 建立基本函数

1.1 s型函数,np.exp()



# 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)


事实上,因为,在深度学习中我们使用的是向量和矩阵,所以,我们很少使用“math”。这也是为什么“numpy”非常有用的原因。

import numpy as np

# example of np.exp
x = np.array([1, 2, 3])
print(np.exp(x)) # result is (exp(1), exp(2), exp(3))


输出:[ 2.71828183 7.3890561 20.08553692]



# 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

x = np.array([1, 2, 3])
sigmoid(x)


输出:array([ 0.73105858, 0.88079708, 0.95257413])

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

return ds

x = np.array([1, 2, 3])
print ("sigmoid_derivative(x) = " + str(sigmoid_derivative(x)))


输出:sigmoid_derivative(x) = [ 0.19661193 0.10499359 0.04517666]

1.3 重塑数组

X.shape 可以得到矩阵或向量的维度。

X.reshape 可以改变矩阵或向量的维度。

举个例子,在计算机科学中,一张图片可以被(3,3,3)表示。当这幅图像作为算法输入时,你需要将其转化为(3*3*3,1)的向量作为输入。

# 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

# 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)))


输出: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 归一化行



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, axis = 1,keepdims = True)

# Divide x by its norm.
x = x/x_norm
### END CODE HERE ###

return x

x = np.array([
[0, 3, 4],
[1, 6, 4]])
print("normalizeRows(x) = " + str(normalizeRows(x)))


输出:normalizeRows(x) = [[ 0. 0.6 0.8 ]

[ 0.13736056 0.82416338 0.54944226]]

1.5 广播和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

x = np.array([
[9, 2, 5, 0, 0],
[7, 5, 0, 0 ,0]])
print("softmax(x) = " + str(softmax(x)))


输出: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]]

注意:x_exp是(2,5),x_sum 是(2,1),因为python广播的原因s为(2,5)

在这部分你需要记住的是

np.exp(x)适用于任何数组,并作用于每一个坐标。

求sigmoid函数的梯度。

image2vector通常用在深度学习。

np.reshape用处非常广泛。保存向量和矩阵的维数会消除大量的错误。

numpy有非常高效的内置函数。

python 的传播非常有用。

2 向量化

import time

x1 = [9, 2, 5, 0, 0, 7, 5, 0, 0, 0, 9, 2, 5, 0, 0]
x2 = [9, 2, 2, 9, 0, 9, 2, 5, 0, 0, 9, 2, 5, 0, 0]

### CLASSIC DOT PRODUCT OF VECTORS IMPLEMENTATION ###
tic = time.process_time()
dot = 0
for i in range(len(x1)):
dot+= x1[i]*x2[i]
toc = time.process_time()
print ("dot = " + str(dot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

### CLASSIC OUTER PRODUCT IMPLEMENTATION ###
tic = time.process_time()
outer = np.zeros((len(x1),len(x2))) # we create a len(x1)*len(x2) matrix with only zeros
for i in range(len(x1)):
for j in range(len(x2)):
outer[i,j] = x1[i]*x2[j]
toc = time.process_time()
print ("outer = " + str(outer) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

### CLASSIC ELEMENTWISE IMPLEMENTATION ###
tic = time.process_time()
mul = np.zeros(len(x1))
for i in range(len(x1)):
mul[i] = x1[i]*x2[i]
toc = time.process_time()
print ("elementwise multiplication = " + str(mul) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

### CLASSIC GENERAL DOT PRODUCT IMPLEMENTATION ###
W = np.random.rand(3,len(x1)) # Random 3*len(x1) numpy array
tic = time.process_time()
gdot = np.zeros(W.shape[0])
for i in range(W.shape[0]):
for j in range(len(x1)):
gdot[i] += W[i,j]*x1[j]
toc = time.process_time()
print ("gdot = " + str(gdot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")


x1 = [9, 2, 5, 0, 0, 7, 5, 0, 0, 0, 9, 2, 5, 0, 0]
x2 = [9, 2, 2, 9, 0, 9, 2, 5, 0, 0, 9, 2, 5, 0, 0]

### VECTORIZED DOT PRODUCT OF VECTORS ###
tic = time.process_time()
dot = np.dot(x1,x2)
toc = time.process_time()
print ("dot = " + str(dot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

### VECTORIZED OUTER PRODUCT ###
tic = time.process_time()
outer = np.outer(x1,x2)
toc = time.process_time()
print ("outer = " + str(outer) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

### VECTORIZED ELEMENTWISE MULTIPLICATION ###
tic = time.process_time()
mul = np.multiply(x1,x2)
toc = time.process_time()
print ("elementwise multiplication = " + str(mul) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

### VECTORIZED GENERAL DOT PRODUCT ###
tic = time.process_time()
dot = np.dot(W,x1)
toc = time.process_time()
print ("gdot = " + str(dot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")


笔记:np.dot()适用于向量和矩阵,矩阵和矩阵相乘。

2.1实现L1,L2损失函数

L1定义为



# 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

yhat = np.array([.9, 0.2, 0.1, .4, .9])
y = np.array([1, 0, 0, 1, 1])
print("L1 = " + str(L1(yhat,y)))


L2定义为



# 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.dot((y-yhat),(y-yhat.T))
### 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)))


还需要记住的是

在深度学习中,向量化是非常重要的概念,它使得计算更加有效和清晰。

回顾L1,L2损失函数。

熟悉numpy的函数,例如np.sum, np.dot, np.multiply, np.maximum, 等等。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐