深度学习中numpy的一些基本操作

sigmoid gradient:
import numpy as np
def sigmoid_derivative(x):
    s = 1 / (1+np.exp(-x))
    ds = s*(1-s)
    return ds
Reshaping arrays:

There are two common numpy function used in deep learning are np.shape() and np.reshape()

def image2vector(image):
    v = image.reshape(image.shape[0]*image.shape[1]*image.shape[2], 1)
    return v
normalizing rows:

It often leads to a better performance because gradient descent coverges faster after normalizations.
Here, by normalizing means changing x to \(\frac{x}{\Vert x \Vert}\)(deviding each of row vector of x by its norm.

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("normalizeRows(x) = " + str(normalizeRows(x)))
normalizeRows(x) = [[0.         0.6        0.8       ]
 [0.13736056 0.82416338 0.54944226]]
broadcasting and softmax function:

Broadcasting is very useful for performing mathematical operations between arrays of different shapes.

softmax function: It can be thought as normalizing function used when your algorithm needs to classify two or more classes.

for \(x\in\mathbb{R}^{1\times n}\),

$softmax(x) = softmax([x_1 x_2 ... x_n])$
$ = \lbrack\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}\rbrack $

for a matrix \(x\in\mathbb{R}^{m\times n}\)

\(softmax(x)=\begin{pmatrix} softmax(first\ row\ of\ x)\\softmax(second\ of\ row\ of\ x) \\ ... \\softmax(last\ row\ of\ x) \end{pmatrix}\)
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
L1 and L2 loss function:

L1 loss is defined as:

$L_1(\hat{y} ,y) = \sum_{i=0}^{m}|y^{(i)} - \hat{y}^{(i)}|$
def L1(yhat, y):
    
    loss = sum(abs(y-yhat))
    
    return loss

L2 loss is defined as:

$L_2(\hat{y}, y)=\sum_{i=0}^{m}(y^{(i)} - \hat{y}^{(i)})^2$
def L2(yhat, y):
    
    loss = np.dot(y-yhat, y-yhat)
    
    return loss
yhat = np.array([0.9, 0.2 ,0.1, 0.4, .9])
y = np.array([1, 0, 0, 1, 1])
print("L2 = " + str(L2(yhat, y)))
L2 = 0.43

转载于:https://www.cnblogs.com/patrolli/p/11315876.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值