ipython:
import numpy as np
def sigmoid(x):
s = 1/(1 + np.exp(-x))
return s
x = np.array([1,2,3])
sigmoid(x)
array([0.73105858, 0.88079708, 0.95257413])
创建函数sigmoid_grad()计算sigmoid函数相对于其输入x的梯度
def sigmoid_derivative(x):
s = sigmoid(x)
ds = s * (1 - s)
return ds
x = np.array([1, 2, 3])
print ("sigmoid_derivative(x) = " + str(sigmoid_derivative(x)))
sigmoid_derivative(x) = [0.19661193 0.10499359 0.04517666]
import numpy as np
def image2vector(image):
v = image.reshape(image.shape[0] * image.shape[1] * image.shape[2], 1)
print(image.shape[0])#读取矩阵第一维度,3个小矩阵
print(image.shape[1])#读取矩阵第二维度,小矩阵李有3个小小矩阵
print(image.shape[2])#读取矩阵第三维度,小小矩阵有两个数据
#不能单纯的理解shape[0]为矩阵的行数
print(image.shape[0] * image.shape[1] * image.shape[2])
return v
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(str(image2vector(image)))
3
3
2
18
[[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]]
import numpy as np
def normalizeRows(x):
x_norm = np.linalg.norm(x, axis = 1, keepdims = True)#axis = 1 行;axis = 0 列;keepdim = True 表示保持其维度
x = x / x_norm
print(x_norm.shape)
print(x.shape)
return x
x = np.array([
[0, 3, 4],
[1, 6, 4]])
print("normalizeRows(x) = " + str(normalizeRows(x)))
(2, 1)
(2, 3)
normalizeRows(x) = [[0. 0.6 0.8 ]
[0.13736056 0.82416338 0.54944226]]
import numpy as np
def softmax(x):
x_exp = np.exp(x)
x_sum = np.sum(x_exp, axis = 1, keepdims = True)
s = x_exp / x_sum
print(x_exp)
print("x_sum:" + str(x_sum))
return s
x = np.array([
[9, 2, 5, 0, 0],
[7, 5, 0, 0 ,0]])
print("softmax(x) = " + str(softmax(x)))
[[8.10308393e+03 7.38905610e+00 1.48413159e+02 1.00000000e+00
1.00000000e+00]
[1.09663316e+03 1.48413159e+02 1.00000000e+00 1.00000000e+00
1.00000000e+00]]
x_sum:[[8260.88614278]
[1248.04631753]]
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]]
np.dot()函数主要有两个功能,向量点积和矩阵乘法
np.dot(a, b), 其中a为一维的向量,b为一维的向量,当然这里a和b都是np.ndarray类型的, 此时因为是一维的所以是向量点积
np.dot(a, b), 其中a为二维矩阵,b为一维向量,这时b会被当做一维矩阵进行计算
注意的是一维矩阵和一维向量的区别,一维向量的shape是(5, ), 而一维矩阵的shape是(5, 1)
np.dot(a ,b), 其中a和b都是二维矩阵,此时dot就是进行的矩阵乘法运算
注意:不同于np.multiply()和* 操作符(相当于Matlab / Octave中的 .*)执行逐元素的乘法,np.dot()执行的是矩阵-矩阵或矩阵向量乘法,
import numpy as np
x1 = [1,2,3]
x2 = [4,5,6]
dot= np.dot(x1,x2)
print(dot)
32
np.outer()函数
①对于多维向量,全部展开变为一维向量
②第一个参数表示倍数,使得第二个向量每次变为几倍。
③第一个参数确定结果的行,第二个参数确定结果的列
import numpy as np
x1 = [1,2,3]
x2 = [4,5,6]
outer = np.outer(x1,x2)
print(outer)
x1 = [[1,2],[3,4]]
x2 = [[1,1],[1,1]]
outer = np.outer(x1,x2)
print(outer)
[[ 4 5 6]
[ 8 10 12]
[12 15 18]]
[[1 1 1 1]
[2 2 2 2]
[3 3 3 3]
[4 4 4 4]]
np.multiply()
①multiply(a,b)就是个乘法,如果a,b是两个数组,那么对应元素相乘
②两个参数和结果的shape应该一致,不一致,在计算中会进行广播处理
import numpy as np
x1 = [1,2,3]
x2 = [4,5,6]
mul = np.multiply(x1,x2)
print(mul)
x1 = np.array([[1,2,3]])
x2 = np.array([4,5,6])
mul = np.multiply(x1,x2)
print(x1.shape)
print(x2.shape)
print(mul)
[ 4 10 18]
(1, 3)
(3,)
[[ 4 10 18]]
L1损失函数
def L1(yhat, y):
#yhat:预测标签 y:实际标签
loss = np.sum(np.abs(y - yhat)) #abs(x): x的绝对值
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)))
L1 = 1.1
L2损失函数
def L2(yhat, y):
loss = np.dot((y - yhat),(y - yhat).T) # .T 矩阵的转置
return loss
yhat = np.array([.9, 0.2, 0.1, .4, .9])
y = np.array([1, 0, 0, 1, 1])
print("L2 = " + str(L2(yhat,y)))
L2 = 0.43