Conceptos básicos de Python con Numpy

Original enlace
h

1 - La construcción de las funciones básicas con numpy

1.1 - función sigmoide

\ (sigmoide (x) = \ frac {1} {1 + e ^ {- x}} \) a veces también se conoce como la función logística. Es una función no lineal utilizado no sólo en Machine Learning (Regresión Logística), pero también en Deep Learning.

\ [\ Text {A} x \ in \ mathbb {R} ^ n \ text {,} sigmoide (x) = sigma \ begin {pmatrix} x_1 x_2 \\ \\ \\ ... x_n \\ \ end { pmatrix} = \ begin {pmatrix} \ frac {1} {1 + e ^ {- x 1}} \\ \ frac {1} {1 + e ^ {- x_2}} \\ \\ ... \ frac { 1} {1 + e ^ {- x_n}} \\ \ end {pmatrix} \ etiqueta {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 - sigmoide gradiente

\ [Sigmoide \ _derivative (x) = \ sigma '(x) = \ sigma (x) (1 - \ sigma (x)) \ etiqueta {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 - Remodelación de los arrays

# 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 - Normalizar las filas

Otra técnica común que utilizamos en el aprendizaje automático y Deep Learning es normalizar los datos. A menudo conduce a un mejor rendimiento, ya que converge gradiente descendente más rápido después de la normalización. En este caso, por la normalización nos referimos a cambiar x para \ (\ frac {x} {\ | x \ |} \) (dividiendo cada vector fila de x por su norma).

Por ejemplo, si

\ [X = \ begin {bmatrix} 0 y 3 y 4 \\ 2 y 6 y 4 \\ \ end {bmatrix} \ tag {3} \]

entonces

\ [\ | x \ | = Np.linalg.norm (x, eje Y = 1, keepdims = true) = \ begin {bmatrix} 5 \\ \ sqrt {56} \\ \ end {bmatrix} \ tag {4} \]

y

\ [X \ _normalized = \ frac {x} {\ | x \ |} = \ begin {bmatrix} 0 & \ frac {3} {5} & \ frac {4} {5} \\ \ frac {2} {\ sqrt {56}} y \ frac {6} { \ sqrt {56}} y \ frac {4} {\ sqrt {56}} \\ \ end {bmatrix} \ tag {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 - Difusión y la función softmax

\ [\ Text {for} x \ in \ mathbb {R} ^ {1 \ n veces} \ text {,} softmax (x) = softmax (\ begin {bmatrix} x_1 && x_2 && ... && x_n \ end {bmatrix}) = \ begin {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}} \ end {bmatrix} \]

\ [\ Text {para una matriz} x \ in \ mathbb {R} ^ {m \ times n} \ text {, $ x_ {ij} $ se asigna al elemento de la $ i ^ {ésimo} $ fila y $ j ^ {ésimo} $ de la columna $ x $, así tenemos:} \]

\ [Softmax (x) = softmax \ begin {bmatrix} x_ {11} y x_ {12} y x_ {13} y \ dots y x_ {1n} \\ x_ {21} y x_ {22} y x_ {23 } y \ puntos y x_ {2n} \\ \ vdots & \ vdots & \ vdots & \ ddots & \ vdots \\ x_ {m1} y x_ {m2} y x_ {m3} y \ puntos y x_ {mn} \ end {bmatrix} = \ begin {bmatrix} \ frac {e ^ {x_ {11}}} {\ sum_ {j} e ^ {x_ {1j}}} y \ frac {e ^ {x_ {12}}} {\ sum_ {j} e ^ {x_ {1j}}} y \ frac {e ^ {x_ {13}}} {\ sum_ {j} e ^ {x_ {1j}}} y \ dots & \ frac { e ^ {x_ {1n}}} {\ sum_ {j} e ^ {x_ {1j}}} \\ \ frac {e ^ {x_ {21}}} {\ sum_ {j} e ^ {x_ {2j }}} y \ frac {e ^ {x_ {22}}} {\ sum_ {j} e ^ {x_ {2j}}} y \ frac {e ^ {x_ {23}}} {\ sum_ {j} e ^ {x_ {2j}}} y \ dots & \ frac {e ^ {x_ {2n}}} {\ sum_ {j} e ^ {x_ {2j}}} \\ \ vdots & \ vdots & \ vdots y \ ddots & \ vdots \\ \ frac {e ^ {x_ {m1}}} {\ sum_ {j} e ^ {x_ {mj}}} y \ frac {e ^ {x_ {m2}}} {\ sum_ {j} e ^ {x_ {mj}}} &\ Frac {e ^ {x_ {m3}}} {\ sum_ {j} e ^ {x_ {mj}}} y \ dots & \ frac {e ^ {x_ {mn}}} {\ sum_ {j} e ^ {x_ {mj}}} \ end {bmatrix} = \ begin {pmatrix} softmax \ text {(primera fila de x)} \\ softmax \ text {(segunda fila de x)} \\ \\ ... softmax \ text {(última fila de x)} \\ \ end {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 - Vectorización

Método tradicional:

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]

Vectorización lograr:

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 - implementar las funciones de pérdida de L1 y L2

  • pérdida L1 se define como:

\ [\ Begin {* align} & L_1 (\ hat {y}, y) = \ sum_ {i = 0} ^ m | y ^ {(i)} - \ hat {y} ^ {(i)} | \ End {align *} \ tag {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
  • pérdida L2 se define como

\ [\ Begin {* align} & L_2 (\ hat {y}, y) = \ sum_ {i = 0} ^ m (y ^ {(i)} - \ hat {y} ^ {(i)}) ^ 2 \ end {align *} \ tag {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

Supongo que te gusta

Origin www.cnblogs.com/pengweiblog/p/12649842.html
Recomendado
Clasificación