1.1.2 Building basic functions with numpy
1.1.2.2 numpy.exp, sigmoid, sigmoid gradient
import numpy as np def sigmoid(x): s = 1/(1+np.exp(-x)) return s # 设sigmoid为s, s‘ = s*(1-s) def sigmoid_derivative(x): s = 1/(1+np.exp(-x)) ds = s*(1-s) return ds plt.figure(1) # 编号为1的figure x = np.arange(-5, 5, 0.1) y = sigmoid(x) plt.subplot(211) # 将子图划分为2行,1列,选中2行中的第1行 plt.plot(x, y) y = sigmoid_derivative(x) plt.subplot(212) # 子图中2行中的第2行 plt.plot(x, y) plt.show()
1.1.2.3 numpy.reshape(), numpy.shape
def image2vector(image): """ Argument: image -- a numpy array of shape (length, height, depth) Returns: v -- a vector of shape (length*height*depth, 1) """ v = image.reshape(image.shape[0] * image.shape[1] * image.shape[2], 1) return v
1.1.2.4 Normalizing rows
np.linalg.norm求对矩阵x按axis作向量内积
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. """ # Compute x_norm as the norm 2 of x. Use np.linalg.norm(..., ord = 2, # axis = ..., keepdims = True) # linalg=linear+algebra. x_norm = np.linalg.norm(x, axis=1, keepdims=True) # Divide x by its norm. x = x/x_norm return x x = np.array([ [0, 3, 4], [1, 6, 4] ]) print("normalizeRows(x) = " + str(normalizeRows(x)))
1.1.2.5 Broadcasting and the softmax function
def softmax(x): x_exp = np.exp(x) s_sum = np.sum(x_exp, axis=1, keepdims=True) s = x_exp/s_sum return s
需要记得以下几点:
1.np.exp(x)对任何np.array的x都可以使用并且是对每个元素进行的求指数
2.sigmoid函数以及其导数
3.image2vector在深度学习中很常用
4.np.reshape应用很广泛。保持矩阵/向量的维度会消除大量的BUG。
5.numpy有很多高效的内建函数。
6.广播非常非常有用
原文地址:https://www.cnblogs.com/ocean1100/p/8457886.html
时间: 2024-10-03 23:00:37