## 二、图像降维和归一化

```import numpy as np #numpy科学计算库
import h5py #与H5文件中存储的数据集进行交互的常用软件包
import matplotlib.pyplot as plt #绘制图表
from dnn_utils import sigmoid, sigmoid_backward, relu, relu_backward```

```train_x, train_y, test_x, test_y ,classes = load_dataset()
index = 2
plt.imshow(train_x[index])
print("y="+ str(train_y[: ,index])+",it is a "+ classes[np.squeeze(train_y[:,index])].decode("utf-8"))```

```# 输出索引为1的图像
index = 1
plt.imshow(train_x[index])
print("y="+ str(train_y[:,index])+",it is a "+ classes[np.squeeze(train_y[:,index])].decode("utf-8"))```

```# 查看数据集具体情况
m_train = train_y.shape[1] #训练集样本数
m_test = test_y.shape[1] #测试集样本数
num_px = train_x.shape[1] #图片的宽/长
print("训练集样本数量："+str(m_train))
print("测试集样本数量："+str(m_test))
print("每张图片的宽/高："+str(num_px))
print("每张图片的大小：("+str(num_px)+", "+str(num_px)+", 3)")
print("训练集图片维度："+str(train_x.shape))
print("训练集标签维度："+str(train_y.shape))
print("测试集图片维度："+str(test_x.shape))
print("测试集标签维度："+str(test_y.shape))
# 为什幺图片维度多了一个3？因为每个像素点有三原色(R,G,B)构成```

```训练集样本数量：209

```#由于需要处理二维矩阵，所以需要降维
#即是把3个二维图像依次拉伸为(num_px)^2，再把m_train个样本列堆积
train_x_flatten = train_x.reshape(train_x.shape[0], -1).T
test_x_flatten = test_x.reshape(test_x.shape[0], -1).T
print("训练集降维后的维度："+str(train_x_flatten.shape))
print("测试集降维后的维度："+str(test_x_flatten.shape))```

```训练集降维后的维度：(12288, 209)

```对像素值0-255归一化处理，对图像简单除以255即可
train_x1 = train_x_flatten / 255
test_x1 = test_x_flatten / 255```

## 二、网络参数的随机初始化

```# 浅层神经网络的参数的随机初始化
def initialization(n_x, n_h, n_y):
W1 = np.random.randn(n_h, n_x) * 0.1 #乘0.1是为了初始化的参数尽可能小
b1 = np.random.randn(n_h, 1)
W2 = np.random.randn(n_y, n_h) * 0.1
b2 = np.random.randn(n_y, 1)

parameters = {
"W1": W1,
"b1": b1,
"W2": W2,
"b2": b2
}
return parameters```

```# L层神经网络的随机初始化
def initialization_deep(layer_dims):
L = len(layer_dims)
parameters = {}
for l in range(1, L):
parameters["W" + str(l)] = np.random.randn(layer_dims[l], layer_dims[l-1]) / np.sqrt(layer_dims[l-1]) # 除以平方根防止梯度爆炸/消失
parameters["b" + str(l)] = np.zeros((layer_dims[l], 1))
return parameters```

## 三、前向传播

```# 前向传播的线性处理
def linear_forward(A, W, b):
Z = np.dot(W, A) + b
cache = (A, W, b)
return Z, cache```

```# 前向传播的线性和激活处理
def linear_activation_forward(A_previous, W, b, activation):
Z, linear_cache = linear_forward(A_previous, W, b)
if activation == "sigmoid":
A, activation_cache = sigmoid(Z)
elif activation == "relu":
A, activation_cache = relu(Z)
cache = (linear_cache, activation_cache)
return A, cache```

```# L层神经网络的前向传播
def L_model_forward(X, parameters):
A = X
L = len(parameters) // 2 # 参数有w和b两种，除以2得到层数L
caches = []
for l in range(1, L):
A_previous = A
A, cache = linear_activation_forward(A_previous, parameters['W' + str(l)], parameters['b' + str(l)], activation = "relu")
caches.append(cache)
AL, cache = linear_activation_forward(A, parameters['W' + str(L)], parameters['b' + str(L)], activation = "sigmoid")
caches.append(cache)
return AL, caches```

## 四、计算成本函数

```# 计算成本值
def compute_cost(AL, Y):
m = Y.shape[1]
cost = -np.sum(np.multiply(np.log(AL),Y) + np.multiply(np.log(1 - AL), 1 - Y)) / m # 交叉熵损失函数
cost = np.squeeze(cost) # 表示向量的数组转换为秩为1的数组 方便plot连续图像
return cost```

## 五、反向传播

```# 反向传播的线性过程
def linear_backward(dZ, cache):
A_prev, W, b = cache
m = A_prev.shape[1]
dW = np.dot(dZ, A_prev.T) / m
db = np.sum(dZ, axis=1, keepdims=True) / m
dA_previous = np.dot(W.T, dZ)
return dA_previous, dW, db```

```# 反向传播的线性和激活过程
def linear_activation_backward(dA, cache, activation):
linear_cache, activation_cache = cache
if activation == "relu":
dZ = relu_backward(dA, activation_cache)
dA_previous, dW, db = linear_backward(dZ, linear_cache)
elif activation == "sigmoid":
dZ = sigmoid_backward(dA, activation_cache)
dA_previous, dW, db = linear_backward(dZ, linear_cache)
return dA_previous, dW, db```

```# L层神经网络的反向传播
def L_model_backward(AL, Y, caches):
L = len(caches)
m = AL.shape[1]
dAL = - (np.divide(Y, AL) - np.divide(1 - Y, 1 - AL))

current_cache = caches[L-1]
for l in reversed(range(L-1)):
current_cache = caches[l]
dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads["dA" + str(l + 2)], current_cache, "relu")
grads["dA" + str(l + 1)] = dA_prev_temp
grads["dW" + str(l + 1)] = dW_temp
grads["db" + str(l + 1)] = db_temp

## 六、参数更新

```# 参数更新
L = len(parameters) // 2
for l in range(1, L+1):
parameters['W' + str(l)] = parameters['W' + str(l)] - learning_rate * grads["dW" + str(l)]
parameters['b' + str(l)] = parameters['b' + str(l)] - learning_rate * grads["db" + str(l)]
return parameters```

## 七、搭建两层神经网络

```# 两层神经网络
def two_layer_model(X, Y, layers_dims, learning_rate=0.0075, num_iterations=3000, print_cost=False, isPlot=True):
costs = []
(n_x,n_h,n_y) = layers_dims
parameters = initialization(n_x, n_h, n_y)

W1 = parameters["W1"]
b1 = parameters["b1"]
W2 = parameters["W2"]
b2 = parameters["b2"]
for i in range(0, num_iterations):
#前向传播
A1, cache1 = linear_activation_forward(X, W1, b1, "relu")
A2, cache2 = linear_activation_forward(A1, W2, b2, "sigmoid")

#计算成本
cost = compute_cost(A2,Y)

#反向传播
dA2 = - (np.divide(Y, A2) - np.divide(1 - Y, 1 - A2))

#反向传播，输入：“dA2，cache2，cache1”。 输出：“dA1，dW2，db2;还有dA0（未使用），dW1，db1”。
dA1, dW2, db2 = linear_activation_backward(dA2, cache2, "sigmoid")
dA0, dW1, db1 = linear_activation_backward(dA1, cache1, "relu")

#更新参数
W1 = parameters["W1"]
b1 = parameters["b1"]
W2 = parameters["W2"]
b2 = parameters["b2"]

#打印成本值
if i % 100 == 0:
#记录成本
costs.append(cost)
#是否打印成本值
if print_cost:
print("第", i ,"次迭代，成本值为：" ,np.squeeze(cost))
#迭代完成，根据条件绘制图
if isPlot:
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per tens)')
plt.title("Learning rate =" + str(learning_rate))
plt.show()

#返回parameters
return parameters```

## 八、搭建L层神经网络

```# L层神经网络模型
def L_layer_model(X, Y, layers_dims, learning_rate=0.0075, num_iterations=3000, print_cost=False,isPlot=True):
np.random.seed(1)
costs = []
parameters = initialization_deep(layers_dims)

for i in range(0, num_iterations):
AL , caches = L_model_forward(X,parameters)
cost = compute_cost(AL,Y)
#打印成本值，如果print_cost=False则忽略
if i % 100 == 0:
#记录成本
costs.append(cost)
#是否打印成本值
if print_cost:
print("第", i ,"次迭代，成本值为：" ,np.squeeze(cost))
#迭代完成，根据条件绘制图
if isPlot:
plt.plot(np.squeeze(costs))
plt.ylabel('cost')
plt.xlabel('iterations (per tens)')
plt.title("Learning rate =" + str(learning_rate))
plt.show()
return parameters```

## 九、预测函数

```# 预测
def predict(X, y, parameters):
m = X.shape[1]
n = len(parameters) // 2
p = np.zeros((1,m))
#根据参数前向传播
probas, caches = L_model_forward(X, parameters)

for i in range(0, probas.shape[1]):
# 概率的四舍五入判断是否为猫
if probas[0,i] > 0.5:
p[0,i] = 1
else:
p[0,i] = 0

print("准确度为: "  + str(float(np.sum((p == y))/m)))
return p```

## 十、两层神经网络测试

```# 两层神经网络测试
n_x = 12288
n_h = 7
n_y = 1
layers_dims = (n_x,n_h,n_y)
parameters = two_layer_model(train_x1, train_y, layers_dims = (n_x, n_h, n_y), learning_rate = 0.0075, num_iterations = 2500, print_cost=True, isPlot=True)```

```第 0 次迭代，成本值为： 0.7063761340884793

```predictions_train = predict(train_x1, train_y, parameters) #训练集
predictions_test = predict(test_x1, test_y, parameters) #测试集```

```准确度为: 1.0

## 十一、L层神经网络测试

```layers_dims = [12288, 20, 7, 5, 1] #  5-layer model
parameters = L_layer_model(train_x1, train_y, layers_dims, learning_rate = 0.0075, num_iterations = 3000, print_cost = True, isPlot=True)```

```第 0 次迭代，成本值为： 0.7717493284237686

```predictions_train = predict(train_x1, train_y, parameters) #训练集
predictions_test = predict(test_x1, test_y, parameters) #测试集```

```准确度为: 0.9904306220095693

## 十二、显示错误分类的图像作分析

```def print_mislabeled_images(classes, X, y, p):
a = p + y
mislabeled_indices = np.asarray(np.where(a == 1))
plt.rcParams['figure.figsize'] = (40.0, 40.0) # set default size of plots
num_images = len(mislabeled_indices[0])
for i in range(num_images):
index = mislabeled_indices[1][i]

plt.subplot(2, num_images, i + 1)
plt.imshow(X[:,index].reshape(64,64,3), interpolation='nearest')
plt.axis('off')
plt.title("Prediction: " + classes[int(p[0,index])].decode("utf-8") + " \n Class: " + classes[y[0,index]].decode("utf-8"))```

`print_mislabeled_images(classes, test_x1, test_y, predictions_test)`

## 十三、预测自己的图片

```my_image = "cat.jpg"
my_label = [1]
fname = "./" + my_image
my_image = np.array(Image.fromarray(image).resize(size=(num_px,num_px))).reshape((1, num_px*num_px*3)).T
my_predicted_image = predict(my_image, my_label, parameters)
plt.imshow(image)
print ("y = " + str(np.squeeze(my_predicted_image)) + ", your L-layer model predicts a \"" + classes[int(np.squeeze(my_predicted_image)),].decode("utf-8") +  "\" picture.")```

```准确度为: 1.0
y = 1.0, your L-layer model predicts a "cat" picture.```