本次作业是完成 一个“识别猫”的神经网络网络搭建。
源代码:
import numpy as np import matplotlib.pyplot as plt from lr_utils import load_dataset train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset() m_train = train_set_y.shape[1] m_test = test_set_y.shape[1] num_px = train_set_x_orig[1] # 降维 train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T # 标准化数据,让数据在【0,1】之间 train_set_x = train_set_x_flatten / 255 test_set_x = test_set_x_flatten / 255 # 建立神经网络的主要步骤是: # 1. 定义模型结构(例如输入特征的数量) # 2. 初始化模型的参数 # 3. 循环: # 3.1 计算当前损失(正向传播) # 3.2 计算当前梯度(反向传播) # 3.3 更新参数(梯度下降) def sigmoid(z): return 1 / (1 + np.exp(-z)) # 初始化w,b def iniialize_with_zeros(dim): """ :param dim: 所要的w的维度 :return: w,b """ b = 0 w = np.zeros(shape=(dim, 1)) # 断言保证格式的正确 assert (w.shape == (dim, 1)) assert (isinstance(b, float) or isinstance(b, int)) return (w, b) def propagate(w, b, X, Y): """ :param w: 权重 :param b: 偏差 :param X: 训练集 :param Y: 标签 :return: cost,w,b """ m = X.shape[1] # 正向传播 A = sigmoid(np.dot(w.T, X) + b) cost = -np.sum(Y * np.log(A) + (1 - Y) * np.log(1 - A)) / m # 反向传播 dw = np.dot(X, (A - Y).T) / m db = np.sum(A - Y) / m assert (dw.shape == w.shape) assert (db.dtype == float) cost = np.squeeze(cost) assert (cost.shape == ()) # 创建一个字典保存w,b grads = { 'dw': dw, 'db': db } return (grads, cost) def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost=False): """ 此函数通过运行梯度下降算法来优化w和b :param X:输入的训练集 :param Y:标签 :param w:权重 :param b:偏差 :param num_iterations:迭代次数 :param learning_rate:学习率 :param print_cost:打印时间 :return:w,b,dw,db costs:优化期间计算的所有成本列表,将用于绘制学习曲线 """ costs = [] for i in range(num_iterations): grads, cost = propagate(w, b, X, Y) dw = grads['dw'] db = grads['db'] # 更新参数 w = w - learning_rate * dw b = b - learning_rate * db # 记录成本 if i % 100 == 0: costs.append(cost) if (print_cost) and (i % 100 == 0): print("迭代的次数: %i , 误差值: %f" % (i, cost)) params = { 'w': w, 'b': b } grads = { 'dw': dw, 'db': db } return (params, grads, costs) def predict(w, b, X): """ :param w:权重 :param b:偏差 :param X:训练集 :return:Y_prediction - 包含X中所有图片的所有预测【0 | 1】的一个numpy数组(向量) """ m = X.shape[1] w = w.reshape(X.shape[0], 1) Y_prediction = np.zeros((1, m)) # 预测猫在图像中出现的概率 A = sigmoid(np.dot(w.T, X) + b) for i in range(A.shape[1]): Y_prediction[0, i] = 1 if A[0, i] > 0.5 else 0 assert (Y_prediction.shape == (1, m)) return Y_prediction def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False): """ :param X_train:训练集 :param Y_train:训练集标签 :param X_test:测试集 :param Y_test:测试集标签 :param num_iterations:迭代次数 :param learning_rate:学习率 :param print_cost:是否打印 :return:有关于所有信息的字典 """ w, b = iniialize_with_zeros(X_train.shape[0]) params, grads, costs=optimize(w,b,X_train,Y_train,num_iterations,learning_rate,print_cost) #检索w,b w=params['w'] b=params['b'] #预测训练集和测试集 Y_prediction_train=predict(w,b,X_train) Y_prediction_test=predict(w,b,X_test) #打印训练后的准确性 print("训练集准确性:", format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100), "%") print("测试集准确性:", format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100), "%") d={ 'costs':costs, 'Y_prediction_train':Y_prediction_train, 'Y_prediction_test':Y_prediction_test, 'w':w, 'b':b, 'learning_rate':learning_rate, 'num_iterations':num_iterations } return d d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 2000, learning_rate = 0.005, print_cost = True)
文章来源: https://blog.csdn.net/qq_41705596/article/details/90547564