吴恩达 深度学习 第一课 第二周

import numpy as np

def sigmoid(x):

    result=1/(1+np.exp(-x))

    return result

    

​

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

sigmoid(x)

array([0.73105858, 0.88079708, 0.95257413])

def SigmoidDerivate(x):

    s=sigmoid(x)

    ds=s*(1-s)

    return ds 

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

ds= SigmoidDerivate(x)

print(ds)

[0.19661193 0.10499359 0.04517666]

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)

[[[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]]]

image2vector(image)

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

def image2vector(image):

    v=image.reshape(image.shape[0]*image.shape[1]*image.shape[2],1)

    return v

def normalizeRows(x):

    x_norn=np.linalg.norm(x,axis=1,keepdims=True)

    x=x/x_norn

    return x

x=np.array([[0,3,4],[1,6,4]])

print(normalizeRows(x))

[[0.         0.6        0.8       ]
 [0.13736056 0.82416338 0.54944226]]

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

[[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]]

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)+"\n------computation time="+str(1000*(toc-tic))+"ms")

​

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)+"\n------computation time="+str(1000*(toc-tic))+"ms")

​

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

​

W = np.random.rand(3,len(x1))

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]*1

toc = time.process_time()

print("gdot = " + str(gdot) + " \n ------- computation time= "+ str(1000*(toc-tic))+"ms")

dot=278
------computation time=0.0ms
outer=[[81. 18. 18. 81.  0. 81. 18. 45.  0.  0. 81. 18. 45.  0.  0.]
 [18.  4.  4. 18.  0. 18.  4. 10.  0.  0. 18.  4. 10.  0.  0.]
 [45. 10. 10. 45.  0. 45. 10. 25.  0.  0. 45. 10. 25.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [63. 14. 14. 63.  0. 63. 14. 35.  0.  0. 63. 14. 35.  0.  0.]
 [45. 10. 10. 45.  0. 45. 10. 25.  0.  0. 45. 10. 25.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [81. 18. 18. 81.  0. 81. 18. 45.  0.  0. 81. 18. 45.  0.  0.]
 [18.  4.  4. 18.  0. 18.  4. 10.  0.  0. 18.  4. 10.  0.  0.]
 [45. 10. 10. 45.  0. 45. 10. 25.  0.  0. 45. 10. 25.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]]
------computation time=0.0ms
elementwise multiplication=[81.  4. 10.  0.  0. 63. 10.  0.  0.  0. 81.  4. 25.  0.  0.]
 ------ computation time0.0ms
gdot = [6.59392797 7.54018772 9.7645816 ] 
 ------- computation time= 0.0ms

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]

​

#vectoried

​

import numpy as np

yhat = np.array([.9, 0.2, 0.1, .4, .9])

y = np.array([1, 0, 0, 1, 1])

loss=np.sum(np.abs(yhat-y))

print(loss)

1.1

import numpy as np

​

a=np.array([[1,2],[1,0]])

b=np.array([2,3])

a*b

​

array([[2, 6],
       [2, 0]])

np.dot(a,b)

array([8, 2])

猜你喜欢

转载自blog.csdn.net/weixin_42454757/article/details/81568921
今日推荐