课程一(Neural Networks and Deep Learning),第二周(Basics of Neural Network programming)—— 3、Python Basic

版权声明:流浪者 https://blog.csdn.net/gz153016/article/details/83615237

“”"
第一个
“”"
test = “Hello World”
print(“test:”+test)
“”"
第二个
“”"
import math
def basic_sigmoid(x):
“”"
:param x:
:return:s
“”"
s = 1/(1+math.exp(-x))
return s
#if name == ‘main’:
#print(basic_sigmoid(3))
x = [1,2,3]
#print(basic_sigmoid(x))
import numpy as np
x = np.array([1,2,3])
#print(np.exp(x))
x = np.array([1,2,3])
#print(x+3)
def sigmoid(x):
s = 1/(1+np.exp(-x))
return s
x = np.array([1,2,3])

print(sigmoid(x))

您将需要使用反向传播计算导数以优化损失函数

def sigmoid_derivative(x):
s = 1/(1+np.exp(-x))
ds = s*(1-s)
return ds

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

“”"
练习: 实现 image2vector (), 它接受维度为(长度、高度、3)的输入,
并返回维度为 (lengthheight3, 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(image.shape)
print ("image2vector(image) = " + str(image2vector(image)))

def normalizeRows(x):
x_norm = np.linalg.norm(x,axis = 1,keepdims=True)
x = x/x_norm
return x

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

def softmax(x):
x_exp = np.exp(x)
x_sum = np.sum(x_exp,axis=1,keepdims=True)
s = x_exp/x_sum
return s
x = np.array([
[9,2,5,0,0],
[7,5,0,0,0]])
print(softmax(x).shape)
print(“softmax(x)=”+str(softmax(x)))
print("--------------------------------------------------------------------")
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 3len(x1) numpy array
tic = time.process_time()
gdot = np.zeros(W.shape[0]) #W.shape[0]=3
for i in range(W.shape[0]): #W的每一行与x1进行点乘
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”)
print("-----------------------------------------------------------------")
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]
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”)
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”)

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

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”)
print(”-------------------------------------------------------------------------------------”)
def L1(yhat,y):
loss = np.sum(np.abs(y-yhat))
return loss
yhat = np.array([0.9, 0.2, 0.1, 0.4, 0.9])
y = np.array([1, 0, 0, 1, 1])
print(“L1 = " + str(L1(yhat,y)))
print(”-------------------------------")
def L2(yhat,y):
loss = np.dot(y-yhat,y-yhat)
return loss

yhat = np.array([0.9, 0.2, 0.1, 0.4, 0.9])
y = np.array([1, 0, 0, 1, 1])
print("L2 = " + str(L2(yhat,y)))

猜你喜欢

转载自blog.csdn.net/gz153016/article/details/83615237