numpyのとPythonの基礎知識

オリジナルリンク
時間

1 - numpyの持つ基本機能の構築

1.1 - シグモイド関数

\(シグモイド(X)= \ FRAC {1} {1つの+ E ^ { - Xは}} \)時にはロジスティック関数として知られています。これは、機械学習(ロジスティック回帰)で、だけでなく、深い学習だけでなく使用非線形関数です。

\ [\ X \で\ mathbb {R} ^ N \テキスト{、}シグモイド(X)=シグモイド\開始{pmatrixのX_1} \\ X_2 \\ ... \\ x_nに関する\\ \端{について}テキスト{ pmatrixの} = \ {pmatrixの} \ FRAC {1} {1つの+ E ^ { - X_1}開始} \\ \ FRAC {1} {1つの+ E ^ { - X_2}} \\ ... \\ \ FRAC { 1} {1つの+ E ^ { - x_nに関する}} \\ \端{pmatrixの} \タグ{1} \]

# 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

1.2 - シグモイド勾配

\ [シグモイド\ _derivative(X)= \シグマ '(X)= \シグマ(X)(1 - \シグマ(X))\タグ{2} \]

# 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

1.3 - 配列をリシェイプ

# 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])
    ### END CODE HERE ###
    
    return v

1.4 - ノーマライズ行

我々は機械学習と深い学習に使用する他の一般的な技術は、我々のデータを正規化することです。勾配降下収束速いため、正規化後のより良いパフォーマンスには、多くの場合、リード。ここで、正規化とは、xを変化意味\(\ \ FRAC {X} {| | X \ \}) そのノルムによるXの各行ベクトルを分割します)。

たとえば、

\ [X = \開始{bmatrix} 0&3&4 \\ 2&6&4 \\ \端{bmatrix} \タグ{3} \]

それから

\ [\ | X \ | = np.linalg.norm(X軸= 1、keepdims = TRUE)= \] \ {bmatrix} 5 \\ \ SQRT {56} \\ \端{bmatrix} \タグ{4}を始めます

そして

\ [X \ _normalized = \ FRAC {X} {\ | X \ |} = \開始{bmatrix} 0&\ FRAC {3} {5}&\ FRAC {4} {5} \\ \ FRAC {2} {\ SQRT {56}}&\ FRAC {6} { \ SQRT {56}}&\ FRAC {4} {\ SQRT {56}} \\ \端{bmatrix} \タグ{5} \]

# 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 - 放送とソフトマックス機能

\ [\テキストX \ {}のために、\ mathbb {R} ^ {1 \回N} \テキスト{、}ソフトマックス(X)=ソフトマックス(\開始{bmatrix} X_1 && X_2 && ... && x_nに関する\端{bmatrix})= \開始{bmatrix} \ FRAC {E ^ {X_1}} {\ sum_ {J} E ^ {X - jが}} && \ FRAC {eは^ {X_2}} {\ sum_ {J} E ^ { X - jが}} && ... && \ FRAC {E ^ {x_nに関する}} {\ sum_ {J} E ^ {X - jが}} \端{bmatrix} \]

\ [\ {マトリックス用}テキストX \で\ mathbb {R} ^ {M \回N} \ {テキスト、$ X_ {IJ} $は$ I ^ {番目の要素} $行と$にマッピングJ ^ {番目} $ X $の$列、したがって、私たちは持っている:} \]

\ [ソフトマックス(X)=ソフトマックス\ \ {bmatrix} X_ {11}&X_ {12}&X_ {13}を開始&ドット&X_ {1N} \\ X_ {21}&X_ {22}&X_ {23 }&\ドット&X_ {2N} \\ \ vdots&\ vdots&\ vdots&\ ddots&\ vdots \\ X_ {M1}&X_ {M2}&X_ {M3}&\ドット&X_ {MN} \端{bmatrix} = \ {bmatrix}開始\ FRACを{E ^ {X_ {11}}} {\ sum_ {J} E ^ {X_ {1J}}}&\ FRAC {E ^ {X_ {12}}} {\ sum_ {J} E ^ {X_ {1J}}}&\ FRAC {E ^ {X_ {13}}} {\ sum_ {J} E ^ {X_ {1J}}}&\ドット&\ FRAC { E ^ {X_ {1N}}} {\ sum_ {J} E ^ {X_ {1J}}} \\ \ FRAC {E ^ {X_ {21}}} {\ sum_ {J} E ^ {X_ {2J }}}&\ FRAC {E ^ {X_ {22}}} {\ sum_ {J} E ^ {X_ {2J}}}&\ FRAC {E ^ {X_ {23}}} {\ sum_ {J} E ^ {X_ {2J}}}&\ドット&\ FRAC {E ^ {X_ {2N}}} {\ sum_ {J} E ^ {X_ {2J}}} \\ \ vdots&\ vdots&\ vdots &\ ddots&\ vdots \\ \ FRAC {E ^ {X_ {M1}}} {\ sum_ {J} E ^ {X_ {MJ}}}&\ FRAC {E ^ {X_ {M2}}} {\ sum_ {J} E ^ {X_ {MJ}}}&\ FRAC {E ^ {X_ {M3}}} {\ sum_ {J} E ^ {X_ {MJ}}}&\ドット&\ FRAC {E ^ {X_ {MN}}} {\ sum_ {J}電子^ {X_ {MJ}}} \端{bmatrix} = \開始{pmatrixのソフトマックス} \テキスト{(xの最初の行)} \\ソフトマックス\テキスト{(xの第二列)} \\ ... \\ソフトマックス\テキスト{(xの最終行)} \\ \端{pmatrixの} \]

# 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 (m,n).

    Argument:
    x -- A numpy matrix of shape (m,n)

    Returns:
    s -- A numpy matrix equal to the softmax of x, of shape (m,n)
    """
    
    ### 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

2 - ベクトル化

伝統的な方法:

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 ###
dot = 0
for i in range(len(x1)):
    dot+= x1[i]*x2[i]

### CLASSIC OUTER PRODUCT IMPLEMENTATION ###
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]

### CLASSIC ELEMENTWISE IMPLEMENTATION ###
mul = np.zeros(len(x1))
for i in range(len(x1)):
    mul[i] = x1[i]*x2[i]

### CLASSIC GENERAL DOT PRODUCT IMPLEMENTATION ###
W = np.random.rand(3,len(x1)) # Random 3*len(x1) numpy array
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]

ベクトル化は達成します:

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 ###
dot = np.dot(x1,x2)

### VECTORIZED OUTER PRODUCT ###
outer = np.outer(x1,x2)

### VECTORIZED ELEMENTWISE MULTIPLICATION ###
mul = np.multiply(x1,x2)

### VECTORIZED GENERAL DOT PRODUCT ###
dot = np.dot(W,x1)

2.1 - L1とL2損失関数を実装

  • L1損失は次のように定義されています。

\ [開始\ {ALIGN *}&L_1(\ハット{Y}、Y)= \ sum_ {i = 0} ^ M | y ^ {(I)} - \帽子{Y} ^ {(I)} | \端{ALIGN *} \タグ{6} \]

# 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
  • L2の損失は以下のように定義されます

\ [\開始{ALIGN *}&L_2(\ハット{Y}、Y)= \ sum_ {i = 0} ^ M(y ^ {(I)} - \帽子{Y} ^ {(I)}) ^ 2 \端{ALIGN *} \タグ{7} \]

# 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.sum(np.dot((y - yhat), (y - yhat)))
    ### END CODE HERE ###
    
    return loss

おすすめ

転載: www.cnblogs.com/pengweiblog/p/12649842.html