吴恩达微专业 神经网络和深度学习第二周课后测试

#import numpy as np
# def basic_sigmoid(x):
#     s = 1/(1+np.exp(-x))
#     return s
# 
# x = np.array([1,2,3])
# 
# print(basic_sigmoid(x))

#Sigmoid gradient

#import numpy as np
# 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) = ' + str(sigmoid_derivative(x)))

#Reshaping arrays

#import numpy as np
# def image2vector(image):
#     v = image.reshape((image.shape[0] * image.shape[1] * image.shape[2]),1)
#     return v
# image = np.array([[[0.1,0.2],[0.1,0.2],
#                   [0.1,0.2],[0.1,0.2],
#                   [0.1,0.2],[0.1,0.2]],
#                    
#                  [[0.1,0.2],[0.1,0.2],
#                   [0.1,0.2],[0.1,0.2],
#                   [0.1,0.2],[0.1,0.2]],
#                    
#                  [[0.1,0.2],[0.1,0.2],
#                   [0.1,0.2],[0.1,0.2],
#                   [0.1,0.2],[0.1,0.2]]])
# 
# print("image2vector(image) = " + str(image2vector(image)))


#normalizing rows

#import numpy as np
#change X to X/||X||
# def normalizeRows(x):
#     x_norm = np.linalg.norm(x,ord = 2,axis = 1,keepdims = True)
#     print('x_norm:',x_norm)
#     x = x/x_norm
#     return x
# 
# x = np.array([[0,3,4],[1,6,4]])
# print('normalizeRows(x) = ' + str(normalizeRows(x)))


#Broadcasting and the softmax function
"""
通过softmax函数一作用,就映射成为(0,1)的值,而这些值的累和为1(满足概率的性质),那么我们就可以将它理解成概率,
在最后选取输出结点的时候,我们就可以选取概率最大(也就是值对应最大的)结点,作为我们的预测目标!
 axis = 0 列累加   往下累加
 axis = 1 行累加  往右累加
 
"""
#import numpy as np
# def softmac(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) = ' + str(softmac(x)))
# print('sum:',np.sum(softmac(x)[1]))
# print(softmac(x)[1])


#Vectorization
# import numpy as np
# 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]
# 
# 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),'time:',str(1000*(toc-tic)))
# 
# tic = time.process_time()
# outer = np.zeros((len(x1),len(x2)))
# 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),'time:',str(1000*(toc-tic)))
# 
# tic = time.process_time()
# mul = np.zeros(len(x1))
# for i in range(len(x1)):
#     for j in range(len(x2)):
#         mul[i] = x1[i]*x2[i]
# toc = time.process_time()
# print('element multiplication:',str(mul),'time:',str(1000*(toc-tic)))        
# 
# w = np.random.rand(3,len(x1))
# tic = time.process_time()
# gdot = np.zeros(w.shape[0])#应该是不合适的 最好换成3*15形式
# print('gdot.shape[0]:',gdot.shape)
# 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(mul),'time:',str(1000*(toc-tic))) 


# import numpy as np
# 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]
# 
# tic = time.process_time()
# dot = np.dot(x1,x2)
# toc = time.process_time()
# print('dot:',str(dot),'time:',str(1000*(toc-tic)))
# 
# tic = time.process_time()
# outer = np.outer(x1, x2)
# toc = time.process_time()
# print('outer:',str(outer),'time:',str(1000*(toc-tic)))
# 
# tic = time.process_time()
# mul = np.multiply(x1,x2)
# toc = time.process_time()
# print('element multiplication:',str(mul),'time:',str(1000*(toc-tic)))        
# 
# w = np.random.rand(3,len(x1)) # 3*15
# tic = time.process_time()
# print('w:',w)
# print('x1:',x1)
# gdot = np.dot(w,x1)
# print('gdot:',gdot.shape)
# toc = time.process_time()
# print('gdot:',str(gdot),'time:',str(1000*(toc-tic))) 
# 
# print('test:',np.dot(x1,w[2].T))

#Implement the L1 and L2 loss functions
import numpy as np
def L1(yhat,y):
    loss = sum(abs(yhat-y))
    return loss

def L2(yhat,y):
    loss = np.dot(yhat-y,yhat-y)
    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)))
print('L2 = ' + str(L2(yhat,y)))

猜你喜欢

转载自blog.csdn.net/zhuisaozhang1292/article/details/81050094