CPTbed-01 猫狗识别

1. 导入相关库

# numpy 用来进行向量化
import numpy as np

# matplotlib 用来画图的
import matplotlib.pyplot as plt

# h5py 用来加载训练数据集的。
# 我们数据集的保存格式是HDF。Hierarchical Data Format(HDF)是一种针对大量
# 数据进行组织和存储的文件格式,大数据行业和人工智能行业都用它来保存数据。
import h5py

# skimage 用来缩放图片
import skimage

import cv2

2. 加载数据集

def load_dataset():
    """从文件夹‘dataset’中加载数据"""
    train_dataset = h5py.File('datasets/train_catvnoncat.h5', 'r')  # 加载训练数据
    train_set_x_orig = np.array(train_dataset['train_set_x'][:])  # 从训练数据集中提取出图片的特征数据
    train_set_y_orig = np.array(train_dataset['train_set_y'][:])  # 从训练数据集中提取出图片的特征数据

    test_data = h5py.File('datasets/test_catvnoncat.h5', 'r')  # 加载测试数据
    test_set_x_orig = np.array(test_data['test_set_x'][:])
    test_set_y_orig = np.array(test_data['test_set_y'][:])

    classes = np.array(test_data['list_classes'][:])  # 加载标签类别数据,这里的类别只有两种,1代表有猫,0代表无猫

    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))  # 把数组的维度从(209,)变成(1, 209),这样好方便后面进行计算
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))  # 从(50,)变成(1, 50)

    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes

3. 工具函数–sigmoid

def sigmoid_activate(z):
    """
    Args:
        z: 可以是一个数,或一个数组

    Returns: 经过一些列sigmoid算法计算后的值,在[0,1]范围内
    """
    a = 1 / (1 + np.exp(-z))
    return a

4. 工具函数–权重初始化0

def initialize_with_zeros(dim):
    """
    这个函数用于初始化权重数组w和偏置/阈值b.
    Args:
        dim: w的大小,看上面的神经网络模型图可知,dim在本例中是12288,因为一个特征输入对应一个权重。

    Returns:
        w: 权重数组
        b: 偏置bias
    """
    w = np.zeros((dim, 1))
    b = 0
    return w, b

5. 工具函数–前向传播&反向传播

def propagate(w, b, X, Y):
    """
    参数:
    w -- 权重数组,维度是(12288, 1)
    b -- 偏置bias
    X -- 图片的特征数据,维度是 (12288, 209)
    Y -- 图片对应的标签,0或1,0是无猫,1是有猫,维度是(1,209)

    返回值:
    cost -- 成本
    dw -- w的梯度
    db -- b的梯度
    """
    m = X.shape[1]
    # 前向传播
    A = sigmoid_activate(np.dot(w.T, X) + b)
    cost = -np.sum(Y * np.log(A) + (1 - Y) * np.log(1 - A)) / m

    # 反向传播
    dZ = A - Y
    dw = np.dot(X, dZ.T) / m
    db = np.sum(dZ) / m

    # 保存 dw, db
    grads = {"dw": dw, "db": db}

    return grads, cost

6. 工具函数–优化w和b参数

def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost=False):
    """
    本函数会得出优化后的参数w和b。
    训练神经网络,其实就是通过使用海量数据来进行训练,从而得出这些优化后的参数。
    有了这些参数后,我们就可以使用它们来进行预测了,
    对于本文章来说,也就是可以用这些参数来对新的任意图片进行预测了,
    预测图片里有猫或没有猫

    参数:
    w -- 权重数组,维度是 (12288, 1)
    b -- 偏置bias
    X -- 图片的特征数据,维度是 (12288, 209)
    Y -- 图片对应的标签,0或1,0是无猫,1是有猫,维度是(1,209)
    num_iterations -- 指定要优化多少次
    learning_rate -- 学习步进,是我们用来控制优化步进的参数
    print_cost -- 为True时,每优化100次就把成本cost打印出来,以便我们观察成本的变化

    返回值:
    params -- 优化后的w和b
    costs -- 每优化100次,将成本记录下来,成本越小,表示参数越优化
    """
    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:
                print("优化%d次后的成本是:" % i, cost)
    params = {"w": w, "b": b}
    return params, costs

7. 工具函数–预测

def predict(w, b, X):
    """
    本函数会使用输入的参数w和b来对输入的待预测数据X进行预测。
    X可以是一张图片也可以是多张图片,当多张图片时,函数会给出对每张图片的预测结果。

    参数:
    w -- 权重数组,维度是 (12288, 1)
    b -- 偏置bias
    X -- 图片的特征数据,维度是 (12288, 图片张数)

    返回值:
    Y_prediction -- 对每张图片的预测结果
    """
    m = X.shape[1]
    Y_prediction = np.zeros((1, m))

    A = sigmoid_activate(np.dot(w.T, X) + b)  # 通过这行代码进行预测

    # 上面的预测结果是小数的形式,为方便后续显示将其转换成0或1的形式(>=0.5为1, 否则为0)
    for i in range(A.shape[1]):
        if A[0, i] >= 0.5:
            Y_prediction[0, i] = 1

    return Y_prediction

8. 构建神经网络模型

def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False):
    """
    构建神经网络模型函数
    
    参数:
    X_train -- 训练图片,维度是(12288, 209)
    Y_train -- 训练图片对应的标签,维度是 (1, 209)
    X_test -- 测试图片,维度是(12288, 50)
    Y_test -- 测试图片对应的标签,维度是 (1, 50)
    num_iterations -- 需要训练/优化多少次
    learning_rate -- 学习步进,是我们用来控制优化步进的参数
    print_cost -- 为True时,每优化100次就把成本cost打印出来,以便我们观察成本的变化

    返回值:
    d -- 返回一些信息
    """
    # 初始化待训练的参数
    w, b = initialize_with_zeros(X_train.shape[0])

    # 使用训练数据调优参数
    parameters, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)

    # 从字典中取出训练好的w和b
    w = parameters['w']
    b = parameters['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_test": Y_prediction_test,
         "Y_prediction_train": Y_prediction_train,
         "w": w,
         "b": b,
         "learning_rate": learning_rate,
         "num_iterations": num_iterations}

    return d

9. 编写主函数

def main():
    # 调用load_dataset函数将数据加载到各个变量中
    train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()

    # 随机显示一张图片。index可以随便改动,图片的像素很低,是因为如果用高像素图片的话就会需要更多的计算时间。
    # 我的目的只是教学,所以没有必要用高像素的图片
    index = 30
    # print(train_set_x_orig[index])
    plt.imshow(train_set_x_orig[index])
    plt.show()
    print("标签为" + str(train_set_y[:, index]) + ", 这是一个'" + classes[np.squeeze(train_set_y[:, index])].decode(
        "utf-8") + "' 图片.")

    # 我们要清楚变量的维度,否则后面会出很多问题,下面把维度打印出来
    print('train_set_x_orig.shape:', train_set_x_orig.shape)  # (209, 64, 64, 3)   (样本数,图片宽,图片长,3个RGB通道)
    print('train_set_y.shape:', train_set_y.shape)  # (1, 209)
    print("test_set_x_orig.shape:", test_set_x_orig.shape)  # (50, 64, 64, 3)
    print("test_set_y.shape:", test_set_y.shape)  # (1, 50)

    # 我们后面要用到样本数和长宽像素值,下面我分别把它们提取出来了。
    m_train = train_set_x_orig.shape[0]
    m_test = test_set_x_orig.shape[0]
    num_px = test_set_x_orig.shape[1]

    print("训练样本数: m_train = " + str(m_train))  # 209
    print("测试样本数: m_test = " + str(m_test))  # 50
    print("每张图片的宽/高: num_px = " + str(num_px))  # 64

    # 为了方便后面进行矩阵运算,我们需要将样本数据进行扁平化和转置
    # 处理后的数组各维度的含义是(图片数据,样本数)
    train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T  # 209*64*64*3 -> 209 * 12288 -> 转置
    test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T  # 50*64*64*3 -> 50 * 12288 -> 转置

    print("train_set_x_flatten.shape:", train_set_x_flatten.shape)  # (12288, 209)
    print("test_set_x_flatten.shape:", test_set_x_flatten.shape)  # (12288, 50)

    # 下面我们对特征数据进行了简单的标准化处理(除以255,使所有值都在[0,1]范围内)
    # 为什么要对数据进行标准化处理呢?简单来说就是为了方便后面进行计算,详情以后再给大家解释
    train_set_x = train_set_x_flatten / 255.
    test_set_x = test_set_x_flatten / 255.

    """
    前面已经加载了数据,并且对数据做了简单的预处理,
    下面会构建一个如“神经网络模型结构.png”所示的神经网络模型对上面的数据进行运算
    """

    d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations=10000, learning_rate=0.005,
              print_cost=True)

    """
    从上面打印出的信息可知,随着优化的次数越来越多,成本越来越小,说明优化后的参数使预测越来越准确了。
     对于训练图片的预测准确率达到了99%。 对于测试图片是70%,其实已经很不错了,
     因为出于教学的目的,我们的训练数据集很小,而且构建的是最最简单的单神经元神经网络。
    """

    # 改变index查看预测结果
    index = 8
    plt.imshow(test_set_x[:, index].reshape((num_px, num_px, 3)))
    print("这张图的标签是 " + str(test_set_y[0, index]) + ", 预测结果是 " + str(int(d["Y_prediction_test"][0, index])))

    # 下面绘制成本函数随训练次数增加时的变化的图像。可以直观的看出,训练次数越多,成本越小,预测结果越精确
    costs = np.squeeze(d['costs'])  # d['costs']是list,经过np.squeeze()变为np.array
    plt.plot(costs)
    plt.xlabel("iterations (per hundreds)")
    plt.ylabel("cost")
    plt.title("learning rate = " + str(d['learning_rate']))
    plt.show()

    # 选择一个合适的学习率很重要,否则神经网络可能会永远找不到损失函数的最小值处,即你的神经网络预测得永远不会很准。
    # 下面我使用了3个不同的学习率来直观地展示展示它们对训练过程的影响。

    learning_rates = [0.01, 0.001, 0.0001]
    models = {}

    for i in learning_rates:
        print("学习率为: " + str(i) + "时")
        models[str(i)] = model(train_set_x, train_set_y, test_set_x, test_set_y,
                               num_iterations=1500, learning_rate=i, print_cost=False)
        print('\n' + "-------------------------------------------------------" + '\n')
    for i in learning_rates:
        plt.plot(np.squeeze(models[str(i)]["costs"]), label=str(models[str(i)]["learning_rate"]))

    plt.ylabel('cost')
    plt.xlabel('iterations (hundreds)')

    legend = plt.legend(loc='upper center', shadow=True)
    frame = legend.get_frame()
    frame.set_facecolor('0.90')
    plt.show()

    # 在本文档的同目录下创建一个文件夹images,把你的任意图片改名成my_image1.jpg后放入文件夹
    my_image = "my_image1.jpg"
    fname = "images/" + my_image

    image = np.array(plt.imread(fname))
    my_image = cv2.resize(image, (num_px, num_px)).reshape((1, num_px * num_px * 3)).T
    my_predicted_image = predict(d["w"], d["b"], my_image)

    plt.imshow(image)
    print("预测结果为 " + str(int(np.squeeze(my_predicted_image))))

10. 运行主函数

if __name__ == '__main__':
    main()

11. 完整代码

# numpy 用来进行向量化
import numpy as np

# matplotlib 用来画图的
import matplotlib.pyplot as plt

# h5py 用来加载训练数据集的。
# 我们数据集的保存格式是HDF。Hierarchical Data Format(HDF)是一种针对大量
# 数据进行组织和存储的文件格式,大数据行业和人工智能行业都用它来保存数据。
import h5py

# skimage 用来缩放图片
import skimage

import tensorflow as tf
import cv2


def load_dataset():
    """从文件夹‘dataset’中加载数据"""
    train_dataset = h5py.File('datasets/train_catvnoncat.h5', 'r')  # 加载训练数据
    train_set_x_orig = np.array(train_dataset['train_set_x'][:])  # 从训练数据集中提取出图片的特征数据
    train_set_y_orig = np.array(train_dataset['train_set_y'][:])  # 从训练数据集中提取出图片的特征数据

    test_data = h5py.File('datasets/test_catvnoncat.h5', 'r')  # 加载测试数据
    test_set_x_orig = np.array(test_data['test_set_x'][:])
    test_set_y_orig = np.array(test_data['test_set_y'][:])

    classes = np.array(test_data['list_classes'][:])  # 加载标签类别数据,这里的类别只有两种,1代表有猫,0代表无猫

    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))  # 把数组的维度从(209,)变成(1, 209),这样好方便后面进行计算
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))  # 从(50,)变成(1, 50)

    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes


# 为了构建神经网络模型,
# 首先要编写一些函数工具,这些函会对相面的数据进行特定功能的运算,
# 最后将这些单独的函数组合起来,构建出一个神经网络模型

# 1. sigmoid函数
def sigmoid_activate(z):
    """
    Args:
        z: 可以是一个数,或一个数组

    Returns: 经过一些列sigmoid算法计算后的值,在[0,1]范围内
    """
    a = 1 / (1 + np.exp(-z))
    return a


# 2. 初始化权重0
def initialize_with_zeros(dim):
    """
    这个函数用于初始化权重数组w和偏置/阈值b.
    Args:
        dim: w的大小,看上面的神经网络模型图可知,dim在本例中是12288,因为一个特征输入对应一个权重。

    Returns:
        w: 权重数组
        b: 偏置bias
    """
    w = np.zeros((dim, 1))
    b = 0
    return w, b


def propagate(w, b, X, Y):
    """
    参数:
    w -- 权重数组,维度是(12288, 1)
    b -- 偏置bias
    X -- 图片的特征数据,维度是 (12288, 209)
    Y -- 图片对应的标签,0或1,0是无猫,1是有猫,维度是(1,209)

    返回值:
    cost -- 成本
    dw -- w的梯度
    db -- b的梯度
    """
    m = X.shape[1]
    # 前向传播
    A = sigmoid_activate(np.dot(w.T, X) + b)
    cost = -np.sum(Y * np.log(A) + (1 - Y) * np.log(1 - A)) / m

    # 反向传播
    dZ = A - Y
    dw = np.dot(X, dZ.T) / m
    db = np.sum(dZ) / m

    # 保存 dw, db
    grads = {"dw": dw, "db": db}

    return grads, cost


def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost=False):
    """
    本函数会得出优化后的参数w和b。
    训练神经网络,其实就是通过使用海量数据来进行训练,从而得出这些优化后的参数。
    有了这些参数后,我们就可以使用它们来进行预测了,
    对于本文章来说,也就是可以用这些参数来对新的任意图片进行预测了,
    预测图片里有猫或没有猫

    参数:
    w -- 权重数组,维度是 (12288, 1)
    b -- 偏置bias
    X -- 图片的特征数据,维度是 (12288, 209)
    Y -- 图片对应的标签,0或1,0是无猫,1是有猫,维度是(1,209)
    num_iterations -- 指定要优化多少次
    learning_rate -- 学习步进,是我们用来控制优化步进的参数
    print_cost -- 为True时,每优化100次就把成本cost打印出来,以便我们观察成本的变化

    返回值:
    params -- 优化后的w和b
    costs -- 每优化100次,将成本记录下来,成本越小,表示参数越优化
    """
    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:
                print("优化%d次后的成本是:" % i, cost)
    params = {"w": w, "b": b}
    return params, costs


def predict(w, b, X):
    """
    本函数会使用输入的参数w和b来对输入的待预测数据X进行预测。
    X可以是一张图片也可以是多张图片,当多张图片时,函数会给出对每张图片的预测结果。

    参数:
    w -- 权重数组,维度是 (12288, 1)
    b -- 偏置bias
    X -- 图片的特征数据,维度是 (12288, 图片张数)

    返回值:
    Y_prediction -- 对每张图片的预测结果
    """
    m = X.shape[1]
    Y_prediction = np.zeros((1, m))

    A = sigmoid_activate(np.dot(w.T, X) + b)  # 通过这行代码进行预测

    # 上面的预测结果是小数的形式,为方便后续显示将其转换成0或1的形式(>=0.5为1, 否则为0)
    for i in range(A.shape[1]):
        if A[0, i] >= 0.5:
            Y_prediction[0, i] = 1

    return Y_prediction


"""到此,我们已经编写了所需的所有工具函数了。下面我们将这些函数组合起来,构建出一个最终的神经网络模型函数。"""


def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False):
    """
    构建神经网络模型函数

    参数:
    X_train -- 训练图片,维度是(12288, 209)
    Y_train -- 训练图片对应的标签,维度是 (1, 209)
    X_test -- 测试图片,维度是(12288, 50)
    Y_test -- 测试图片对应的标签,维度是 (1, 50)
    num_iterations -- 需要训练/优化多少次
    learning_rate -- 学习步进,是我们用来控制优化步进的参数
    print_cost -- 为True时,每优化100次就把成本cost打印出来,以便我们观察成本的变化

    返回值:
    d -- 返回一些信息
    """
    # 初始化待训练的参数
    w, b = initialize_with_zeros(X_train.shape[0])

    # 使用训练数据调优参数
    parameters, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)

    # 从字典中取出训练好的w和b
    w = parameters['w']
    b = parameters['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_test": Y_prediction_test,
         "Y_prediction_train": Y_prediction_train,
         "w": w,
         "b": b,
         "learning_rate": learning_rate,
         "num_iterations": num_iterations}

    return d


def main():
    # 调用load_dataset函数将数据加载到各个变量中
    train_set_x_orig, train_set_y, test_set_x_orig, test_set_y, classes = load_dataset()

    # 随机显示一张图片。index可以随便改动,图片的像素很低,是因为如果用高像素图片的话就会需要更多的计算时间。
    # 我的目的只是教学,所以没有必要用高像素的图片
    index = 30
    # print(train_set_x_orig[index])
    plt.imshow(train_set_x_orig[index])
    plt.show()
    print("标签为" + str(train_set_y[:, index]) + ", 这是一个'" + classes[np.squeeze(train_set_y[:, index])].decode(
        "utf-8") + "' 图片.")

    # 我们要清楚变量的维度,否则后面会出很多问题,下面把维度打印出来
    print('train_set_x_orig.shape:', train_set_x_orig.shape)  # (209, 64, 64, 3)   (样本数,图片宽,图片长,3个RGB通道)
    print('train_set_y.shape:', train_set_y.shape)  # (1, 209)
    print("test_set_x_orig.shape:", test_set_x_orig.shape)  # (50, 64, 64, 3)
    print("test_set_y.shape:", test_set_y.shape)  # (1, 50)

    # 我们后面要用到样本数和长宽像素值,下面我分别把它们提取出来了。
    m_train = train_set_x_orig.shape[0]
    m_test = test_set_x_orig.shape[0]
    num_px = test_set_x_orig.shape[1]

    print("训练样本数: m_train = " + str(m_train))  # 209
    print("测试样本数: m_test = " + str(m_test))  # 50
    print("每张图片的宽/高: num_px = " + str(num_px))  # 64

    # 为了方便后面进行矩阵运算,我们需要将样本数据进行扁平化和转置
    # 处理后的数组各维度的含义是(图片数据,样本数)
    train_set_x_flatten = train_set_x_orig.reshape(train_set_x_orig.shape[0], -1).T  # 209*64*64*3 -> 209 * 12288 -> 转置
    test_set_x_flatten = test_set_x_orig.reshape(test_set_x_orig.shape[0], -1).T  # 50*64*64*3 -> 50 * 12288 -> 转置

    print("train_set_x_flatten.shape:", train_set_x_flatten.shape)  # (12288, 209)
    print("test_set_x_flatten.shape:", test_set_x_flatten.shape)  # (12288, 50)

    # 下面我们对特征数据进行了简单的标准化处理(除以255,使所有值都在[0,1]范围内)
    # 为什么要对数据进行标准化处理呢?简单来说就是为了方便后面进行计算,详情以后再给大家解释
    train_set_x = train_set_x_flatten / 255.
    test_set_x = test_set_x_flatten / 255.

    """
    前面已经加载了数据,并且对数据做了简单的预处理,
    下面会构建一个如“神经网络模型结构.png”所示的神经网络模型对上面的数据进行运算
    """

    d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations=10000, learning_rate=0.005,
              print_cost=True)

    """
    从上面打印出的信息可知,随着优化的次数越来越多,成本越来越小,说明优化后的参数使预测越来越准确了。
     对于训练图片的预测准确率达到了99%。 对于测试图片是70%,其实已经很不错了,
     因为出于教学的目的,我们的训练数据集很小,而且构建的是最最简单的单神经元神经网络。
    """

    # 改变index查看预测结果
    index = 8
    plt.imshow(test_set_x[:, index].reshape((num_px, num_px, 3)))
    print("这张图的标签是 " + str(test_set_y[0, index]) + ", 预测结果是 " + str(int(d["Y_prediction_test"][0, index])))

    # 下面绘制成本函数随训练次数增加时的变化的图像。可以直观的看出,训练次数越多,成本越小,预测结果越精确
    costs = np.squeeze(d['costs'])  # d['costs']是list,经过np.squeeze()变为np.array
    plt.plot(costs)
    plt.xlabel("iterations (per hundreds)")
    plt.ylabel("cost")
    plt.title("learning rate = " + str(d['learning_rate']))
    plt.show()

    # 选择一个合适的学习率很重要,否则神经网络可能会永远找不到损失函数的最小值处,即你的神经网络预测得永远不会很准。
    # 下面我使用了3个不同的学习率来直观地展示展示它们对训练过程的影响。

    learning_rates = [0.01, 0.001, 0.0001]
    models = {}

    for i in learning_rates:
        print("学习率为: " + str(i) + "时")
        models[str(i)] = model(train_set_x, train_set_y, test_set_x, test_set_y,
                               num_iterations=1500, learning_rate=i, print_cost=False)
        print('\n' + "-------------------------------------------------------" + '\n')
    for i in learning_rates:
        plt.plot(np.squeeze(models[str(i)]["costs"]), label=str(models[str(i)]["learning_rate"]))

    plt.ylabel('cost')
    plt.xlabel('iterations (hundreds)')

    legend = plt.legend(loc='upper center', shadow=True)
    frame = legend.get_frame()
    frame.set_facecolor('0.90')
    plt.show()

    # 在本文档的同目录下创建一个文件夹images,把你的任意图片改名成my_image1.jpg后放入文件夹
    my_image = "my_image1.jpg"
    fname = "images/" + my_image

    image = np.array(plt.imread(fname))
    my_image = cv2.resize(image, (num_px, num_px)).reshape((1, num_px * num_px * 3)).T
    my_predicted_image = predict(d["w"], d["b"], my_image)
    plt.imshow(cv2.resize(image, (num_px, num_px)))
    plt.show()
    print("预测结果为 " + str(int(np.squeeze(my_predicted_image))))


if __name__ == '__main__':
    main()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值