def model_01():
    """
    猫图像识别,两层网络结构
    :return:
    """
    X_train, Y_train, X_test, Y_test, classes = load_data()  # 猫图像数据

    # 模型参数设置
    layers_dims = [X_train.shape[0], 7, 1]
    num_iter = 2500
    learning_rate = 0.0075
    print_cost = True
    initialization = "sqrt_n"

    parameters, costs = basic_model(X_train,
                                    Y_train,
                                    layers_dims=layers_dims,
                                    num_iter=num_iter,
                                    lr=learning_rate,
                                    print_cost=print_cost,
                                    initialization=initialization)

    # 预测及评估
    prediction_train = predict(parameters, X_train)
    prediction_test = predict(parameters, X_test)

    print("Train准确率: {}".format(evaluate(prediction_train, Y_train)))
    print("test准确率: {}".format(evaluate(prediction_test, Y_test)))

    costs_draw(costs, learning_rate=learning_rate)
def model_03():
    """
    单隐层平面数据分类
    :return:
    """
    # 加载数据集
    planar = load_planar_dataset()
    noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure = load_extra_datasets(
    )

    datasets = {
        "planar": planar,
        "noisy_circles": noisy_circles,
        "noisy_moons": noisy_moons,
        "blobs": blobs,
        "gaussian_quantiles": gaussian_quantiles
    }

    data_set = "planar"  # 选择数据集
    X_train, Y_train = datasets[data_set]

    if data_set != "planar":
        X_train, Y_train = X_train.T, Y_train.reshape(1, Y_train.shape[0])

    if data_set == "blobs":
        Y_train = Y_train % 2

    # 绘制散点图
    data_draw(X_train, Y_train)

    # 模型参数设置
    layers_dims = [X_train.shape[0], 4, Y_train.shape[0]]
    num_iter = 10000
    learning_rate = 1.2
    print_cost = True
    initialization = "random_small"

    parameters, costs = basic_model(X_train,
                                    Y_train,
                                    layers_dims=layers_dims,
                                    num_iter=num_iter,
                                    lr=learning_rate,
                                    print_cost=print_cost,
                                    initialization=initialization)

    # Plot the decision boundary
    plot_decision_boundary(lambda x: predict(parameters, x.T), X_train,
                           Y_train)
    plt.title("Decision Boundary for hidden layer size " + str(4))
    plt.show()

    # 预测及评估
    prediction_train = predict(parameters, X_train)
    print("Train准确率: {}".format(evaluate(prediction_train, Y_train)))

    costs_draw(costs, learning_rate=learning_rate)
def model_00():
    """
    猫图像识别,逻辑回归模型
    :return:
    """
    # 加载数据
    X_train, Y_train, X_test, Y_test, classes = load_data()

    # 模型参数设置
    layers_dims = [X_train.shape[0], 1]
    num_iter = 2000
    learning_rate = 0.005
    print_cost = True
    initialization = "zeros"

    # 调用模型
    parameters, costs = basic_model(X_train,
                                    Y_train,
                                    layers_dims=layers_dims,
                                    num_iter=num_iter,
                                    lr=learning_rate,
                                    print_cost=print_cost,
                                    initialization=initialization)

    costs_draw(costs, learning_rate=learning_rate)

    # 预测及评估
    prediction_train = predict(parameters, X_train)
    prediction_test = predict(parameters, X_test)

    print("Train准确率: {}".format(evaluate(prediction_train, Y_train)))
    print("test准确率: {}".format(evaluate(prediction_test, Y_test)))

    # 预测新图像
    new_image = "images/my_image.jpg"
    image_reshape = read_image(new_image, show_image=True)
    new_image_prediction = predict(parameters, image_reshape)
    print("新图像预测值 y = {}".format(int(np.squeeze(new_image_prediction))))

    # 不同学习率
    different_lr(X_train, Y_train, X_test, Y_test)
示例#4
0
def model_reg(lambd, keep_prob, title):
    # 加载数据
    X_train, Y_train, X_test, Y_test = load_2D_dataset()

    # 模型参数设置
    layers_dims = [X_train.shape[0], 20, 3, 1]
    num_iter = 30000
    learning_rate = 0.3
    print_cost = False
    initialization = "sqrt_n"

    # 调用模型
    parameters, costs = basic_model(X_train,
                                    Y_train,
                                    layers_dims=layers_dims,
                                    num_iter=num_iter,
                                    lr=learning_rate,
                                    print_cost=print_cost,
                                    initialization=initialization,
                                    lambd=lambd,
                                    keep_prob=keep_prob)

    costs_draw(costs, learning_rate=learning_rate)

    # 预测及评估
    prediction_train = predict(parameters, X_train)
    prediction_test = predict(parameters, X_test)

    print("Train准确率: {}".format(evaluate(prediction_train, Y_train)))
    print("test准确率: {}".format(evaluate(prediction_test, Y_test)))

    plt.title(title)
    axes = plt.gca()
    axes.set_xlim([-0.75, 0.40])
    axes.set_ylim([-0.75, 0.65])
    plot_decision_boundary(lambda x: predict(parameters, x.T), X_train,
                           Y_train)
def model_02():
    """猫图像识别,四层网络结构
    :return:
    """

    X_train, Y_train, X_test, Y_test, classes = load_data()  # 猫图像数据

    # 模型参数设置
    layers_dims = [X_train.shape[0], 20, 7, 5, 1]
    num_iter = 2500
    learning_rate = 0.0075
    print_cost = True
    initialization = "sqrt_n"

    parameters, costs = basic_model(X_train,
                                    Y_train,
                                    layers_dims=layers_dims,
                                    num_iter=num_iter,
                                    lr=learning_rate,
                                    print_cost=print_cost,
                                    initialization=initialization)

    # 预测及评估
    prediction_train = predict(parameters, X_train)
    prediction_test = predict(parameters, X_test)

    print("Train准确率: {}".format(evaluate(prediction_train, Y_train)))
    print("test准确率: {}".format(evaluate(prediction_test, Y_test)))

    costs_draw(costs, learning_rate=learning_rate)

    # 预测新图像
    new_image = "images/my_image.jpg"
    image_reshape = read_image(new_image, show_image=True)
    new_image_prediction = predict(parameters, image_reshape)
    print("新图像预测值 y = {}".format(int(np.squeeze(new_image_prediction))))