def model(X,Y,learning_rate = 0.3,num_iterations = 30000,print_cost = True,is_plot = True,lambd = 0,keep_prob = 1):
    '''
    实现一个三层的神经网络:linear->relu->linear->relu->linear->sigmoid
    :param X: 输入的数据,维度为(2,要训练/测试的数量)
    :param Y: 标签,[0(蓝色)|1(红色)],维度为(1,对应的是输入的数据的标签)
    :param learing_rate: 学习速率
    :param num_iterations: 迭代次数
    :param print_cost: 是否打印成本值
    :param lambd: 正则化的超参数,实数
    :param keep_prob: 随机删除节点的概率
    :return: 
    parameters-学习后的参数
    '''
    grads = {}
    costs = []
    m = X.shape[1]
    layers_dims = [X.shape[0],20,3,1]

    #初始化参数
    parameters = reg_utils.initialize_parameters(layers_dims)

    #开始学习
    for i in range(0,num_iterations):
        #前向传播
        ##是否删除随机节点
        if keep_prob == 1:
            ###不删除随机节点
            a3,cache = reg_utils.forward_propagation(X,parameters)
        elif keep_prob<1:
            ###随即删除节点
            a3 , cache = forward_propagation_with_dropout(X,parameters,keep_prob)
        else:
            print('keep_prob参数错误,程序退出.')
            exit

        #计算成本
        ##是否使用二范数
        if lambd == 0:
            ###不使用L2正则化
            cost = reg_utils.compute_cost(a3,Y)
        else:
            ###使用L2正则化
            cost = compute_cost_with_regularization(a3,Y,parameters,lambd)

        ##反向传播
        ##可以同时使用L2正则化和随机删除节点,但本次实验不同时使用
        assert(lambd == 0 or keep_prob == 1)

        ##两个参数的使用情况
        if(lambd == 0 and keep_prob == 1):
            ###不适用L2正则化和不使用随即删除节点
            grads = reg_utils.backward_propagation(X,Y,cache)
        elif lambd!=0:
            ###使用L2正则化,不使用随即删除节点
            grads = backward_propagation_with_regularization(X,Y,cache,lambd)
        elif keep_prob<1:
            ###使用随即删除节点,不使用L2正则化
            grads = backward_propagation_with_dropout(X,Y,cache,keep_prob)

        #更新参数
        parameters = reg_utils.update_parameters(parameters,grads,learning_rate)

        #记录并打印成本
        if i%1000 == 0:
            ##记录成本
            costs.append(cost)
            if(print_cost and i%10000==0):
                #打印成本
                print('第'+str(i)+'次迭代,成本值为:'+str(cost))

    #是否绘制成本曲线图
    if is_plot:
        plt.plot(costs)
        plt.ylabel('cost')
        plt.xlabel('iterations(x1,000)')
        plt.title('Learning rate = '+str(learning_rate))
        plt.show()

    #返回学习后的参数
    return parameters
예제 #2
0
def model(X, Y, learning_rate = 0.3, num_iterations = 30000, print_cost = True, lambd = 0, keep_prob = 1):
    """
    Implements a three-layer neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SIGMOID.
    
    Arguments:
    X -- input data, of shape (input size, number of examples)
    Y -- true "label" vector (1 for blue dot / 0 for red dot), of shape (output size, number of examples)
    learning_rate -- learning rate of the optimization
    num_iterations -- number of iterations of the optimization loop
    print_cost -- If True, print the cost every 10000 iterations
    lambd -- regularization hyperparameter, scalar
    keep_prob - probability of keeping a neuron active during drop-out, scalar.
    
    Returns:
    parameters -- parameters learned by the model. They can then be used to predict.
    """
        
    grads = {}
    costs = []                            # to keep track of the cost
    m = X.shape[1]                        # number of examples
    layers_dims = [X.shape[0], 20, 3, 1]
    
    # Initialize parameters dictionary.
    parameters = initialize_parameters(layers_dims)

    # Loop (gradient descent)

    for i in range(0, num_iterations):

        # Forward propagation: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID.
        if keep_prob == 1:
            a3, cache = forward_propagation(X, parameters)
        elif keep_prob < 1:
            a3, cache = forward_propagation_with_dropout(X, parameters, keep_prob)
        
        # Cost function
        if lambd == 0:
            cost = compute_cost(a3, Y)
        else:
            cost = compute_cost_with_regularization(a3, Y, parameters, lambd)
            
        # Backward propagation.
        assert(lambd==0 or keep_prob==1)    # it is possible to use both L2 regularization and dropout, 
                                            # but this assignment will only explore one at a time
        if lambd == 0 and keep_prob == 1:
            grads = backward_propagation(X, Y, cache)
        elif lambd != 0:
            grads = backward_propagation_with_regularization(X, Y, cache, lambd)
        elif keep_prob < 1:
            grads = backward_propagation_with_dropout(X, Y, cache, keep_prob)
        
        # Update parameters.
        parameters = update_parameters(parameters, grads, learning_rate)
        
        # Print the loss every 10000 iterations
        if print_cost and i % 10000 == 0:
            print("Cost after iteration {}: {}".format(i, cost))
        if print_cost and i % 1000 == 0:
            costs.append(cost)
    
    # plot the cost
    plt.plot(costs)
    plt.ylabel('cost')
    plt.xlabel('iterations (x1,000)')
    plt.title("Learning rate =" + str(learning_rate))
    plt.show()
    
    return parameters
예제 #3
0
def model(X,Y,learning_rate=0.3,num_iterations=30000,print_cost=True,is_plot=True,lambd=0,keep_prob=1):
    """
    实现一个三层的神经网络:Linear -> Relu -> Linear -> Relu -> Linear -> Sigmoid
    
    参数:
        X - 输入的数据,维度为(2.要训练/测试的数量)
        Y - 标签,【0(蓝色)|1(红色)】,维度为(1,对应的是输入数据的标签)
        learning_rate - 学习速率
        num_iterations - 迭代的次数
        print_cost - 是否打印成本值,每10000次打印一次,但是每1000次记录一个成本值
        is_plot - 是否绘制梯度下降的曲线图
        lambd - 正则化的超参数,实数
        keep_prob - 随机删除节点的概率
    返回:
        parameters - 学习后的参数
    """
    
    grads = {}
    costs = []
    m = X.shape[1]
    layers_dims = [X.shape[0],20,3,1]
    
    #初始化参数
    parameters = reg_utils.initialize_parameters(layers_dims)
    
    #开始学习
    for i in range(0,num_iterations):
        #前向传播
        ##时候随机删除节点
        if keep_prob == 1:
            ##不随机删除节点
            a3,cache = reg_utils.forward_propagation(X,parameters)
        elif keep_prob < 1:
            ##随机删除节点
            a3,cache = forward_propagation_with_dropout(X,parameters,keep_prob)
        else:
            print("keep_prob参数错误!程序退出")
            exit
        
        ###计算成本
        ##是否使用二范数
        if lambd == 0:
            ##不使用L2正则化
            cost = reg_utils.compute_cost(a3,Y)
        else:
            ###使用L2正则化
            cost = compute_cost_with_regularization(a3,Y,parameters,lambd)
            
        ##反向传播
        ##可以同时使用L2正则化和随机删除节点,但本次实验不同时使用
        
        assert(lambd == 0 or keep_prob == 1)
        
        ##两个参数的使用情况
        if (lambd == 0 and keep_prob == 1):
            #不使用L2正则化和不使用随机删除节点
            grads = reg_utils.backward_propagation(X,Y,cache)
        elif lambd != 0:
            ####使用正则化,不使用随机删除节点
            grads = backward_propagation_with_regularization(X,Y,cache,lambd)
        elif keep_prob < 1:
            ###使用随机删除节点,不使用L2正则化
            grads = backward_propagation_with_dropout(X,Y,cache,keep_prob)
        
        ###更新参数
        parameters = reg_utils.update_parameters(parameters,grads,learning_rate)
        
        ##记录并打印成本
        if i % 1000 == 0:
            ##记录成本
            costs.append(cost)
            if(print_cost and i % 10000 == 0):
                #打印成本
                print("第" + str(i) + "次迭代,成本值为:" + str(cost))
    
    
    #是否绘制成本曲线图
    if is_plot:
        plt.plot(costs)
        plt.ylabel('cost')
        plt.xlabel('iterations(x1,10000)')
        plt.title('Learning rate = '+ str(learning_rate))
        
        
    #返回学习后的参数
    return parameters
def model(X, Y, learning_rate = 0.3, num_iterations = 3000, print_cost = True, lambd = 0, keep_prob = 1):
    """
    Implements a three-layer neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SIGMOID.
    
    Arguments:
    X -- input data, of shape (input size, number of examples)
    Y -- true "label" vector (1 for blue dot / 0 for red dot), of shape (output size, number of examples)
    learning_rate -- learning rate of the optimization
    num_iterations -- number of iterations of the optimization loop
    print_cost -- If True, print the cost every 10000 iterations
    lambd -- regularization hyperparameter, scalar
    keep_prob - probability of keeping a neuron active during drop-out, scalar.
    
    Returns:
    parameters -- parameters learned by the model. They can then be used to predict.
    """
        
    grads = {}
    costs = []                            # to keep track of the cost
    m = X.shape[1]                        # number of examples
    layers_dims = [X.shape[0], 20, 3, 1]
    
    # Initialize parameters dictionary.
    parameters = initialize_parameters(layers_dims)

    # Loop (gradient descent)

    for i in range(0, num_iterations):

        # Forward propagation: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID.
        if keep_prob == 1:
            a3, cache = forward_propagation(X, parameters)
        elif keep_prob < 1:
            a3, cache = forward_propagation_with_dropout(X, parameters, keep_prob)
        
        # Cost function
        if lambd == 0:
            cost = compute_cost(a3, Y)
        else:
            cost = compute_cost_with_regularization(a3, Y, parameters, lambd)
            
        # Backward propagation.
        assert(lambd == 0 or keep_prob == 1)    # it is possible to use both L2 regularization and dropout, 
                                            # but this assignment will only explore one at a time
        if lambd == 0 and keep_prob == 1:
            grads = backward_propagation(X, Y, cache)
        elif lambd != 0:
            grads = backward_propagation_with_regularization(X, Y, cache, lambd)
        elif keep_prob < 1:
            grads = backward_propagation_with_dropout(X, Y, cache, keep_prob)
        
        # Update parameters.
        parameters = update_parameters(parameters, grads, learning_rate)
        
        # Print the loss every 10000 iterations
        if print_cost and i % 10000 == 0:
            print("Cost after iteration {}: {}".format(i, cost))
        if print_cost and i % 1000 == 0:
            costs.append(cost)
    
    # plot the cost
    plt.plot(costs)
    plt.ylabel('cost')
    plt.xlabel('iterations (x1,000)')
    plt.title("Learning rate =" + str(learning_rate))
    plt.show()
    
    return parameters
def model(X,
          Y,
          learning_rate=0.3,
          num_iterations=30000,
          print_cost=True,
          lambd=0,
          keep_prob=1):
    """
    implements a three layer neural network:
        linear -> relu -> linear -> relu -> linear -> sigmoid
        
    arguments:
        X -- input data, shape(input size,number of examples)
        Y -- true label vector, shape(output size,number of examples)
        learning_rate -- learn rate of the optimization
        num_iterations -- number of iterations of the optimization loop
        print_cost -- if true, print the cost every 10000 iterations
        lambd -- regularization hhyperparameter
        keep_prob -- probability of keeping a neuron activa during drop-out
        
    returns:
        parameters -- parameters learned by the model, they can then be used to predict
    """
    grads = {}
    costs = []
    m = X.shape[1]
    layers_dims = [X.shape[0], 20, 3, 1]

    #initialize parameters dictionary
    parameters = initialize_parameters(layers_dims)

    #loop
    for i in range(0, num_iterations):
        #forward
        if keep_prob == 1:
            a3, cache = forward_propagation(X, parameters)
        elif keep_prob < 1:
            a3, cache = forward_propagation_with_dropout(
                X, parameters, keep_prob)

        #cost function
        if lambd == 0:
            cost = compute_cost(a3, Y)
        else:
            cost = compute_cost_with_regularization(a3, Y, parameters, lambd)

        #
        assert (lambd == 0 or keep_prob == 1)

        if lambd == 0 and keep_prob == 1:
            grads = backward_propagation(X, Y, cache)
        elif lambd != 0:
            grads = backward_propagation_with_regularization(
                X, Y, cache, lambd)
        elif keep_prob < 1:
            grads = backward_propagation_with_dropout(X, Y, cache, keep_prob)

        #update parameters
        parameters = update_parameters(parameters, grads, learning_rate)

        #print the loss every 1000 iterations
        if print_cost and i % 10000 == 0:
            print("Cost after iteration {}: {}".format(i, cost))
        if print_cost and i % 100 == 0:
            costs.append(cost)

    #plot the cost
    plt.figure(2)
    plt.plot(costs)
    plt.ylabel('cost')
    plt.xlabel('iterations (x1,000)')
    plt.title('learning rate = ' + str(learning_rate))
    plt.show()

    return parameters
예제 #6
0
def model(X,
          Y,
          learning_rate=0.3,
          num_iterations=30000,
          print_cost=True,
          lambd=0,
          keep_prob=1):
    """
    实现一个三层神经网络模型: LINEAR->RELU->LINEAR->RELU->LINEAR->SIGMOID.
    可以:-- 不使用正则化
          -- 使用 L2 正则化
          -- 使用 dropout 正则化

    :param X: 输入数据, of shape (input size, number of examples)
    :param Y: 正确的标签向量 (1 for blue dot / 0 for red dot), of shape (output size, number of examples)
    :param learning_rate: 优化的学习率
    :param num_iterations: 优化循环的迭代次数
    :param print_cost: If True, print the cost every 10000 iterations
    :param lambd: 正则化的超参数 λ
    :param keep_prob: drop-out 正则化中随机保留节点的概率
    
    Returns:
    parameters -- 神经网络模型学习得到的参数,可以用来预测
    """

    grads = {}
    costs = []  # to keep track of the cost
    m = X.shape[1]  # number of examples
    layers_dims = [X.shape[0], 20, 3, 1]

    # 初始化参数字典
    parameters = initialize_parameters(layers_dims)

    # 学习循环(梯度下降)
    for i in range(0, num_iterations):

        # 前向传播: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID.
        # 不使用 dropout 正则化
        if keep_prob == 1:
            a3, cache = forward_propagation(X, parameters)
        # 使用 dropout 正则化
        elif keep_prob < 1:
            a3, cache = forward_propagation_with_dropout(
                X, parameters, keep_prob)

        # 计算 cost
        # 不使用 L2 正则化
        if lambd == 0:
            cost = compute_cost(a3, Y)
        # 使用 L2 正则化
        else:
            cost = compute_cost_with_regularization(a3, Y, parameters, lambd)

        # 后向传播
        assert (lambd == 0 or keep_prob == 1
                )  # 可以同时使用 L2 正则化和 dropout, 但是本程序只使用其中一个或不使用正则化
        # 不使用正则化
        if lambd == 0 and keep_prob == 1:
            grads = backward_propagation(X, Y, cache)
        # 使用 L2 正则化
        elif lambd != 0:
            grads = backward_propagation_with_regularization(
                X, Y, cache, lambd)
        # 使用 dropout 正则化
        elif keep_prob < 1:
            grads = backward_propagation_with_dropout(X, Y, cache, keep_prob)

        # 更新参数
        parameters = update_parameters(parameters, grads, learning_rate)

        # Print the loss every 10000 iterations
        if print_cost and i % 10000 == 0:
            print("Cost after iteration {}: {}".format(i, cost))
        if print_cost and i % 1000 == 0:
            costs.append(cost)

    # 绘制 cost 变化曲线图
    plt.plot(costs)
    plt.ylabel('cost')
    plt.xlabel('iterations (x1,000)')
    plt.title("Learning rate =" + str(learning_rate))
    plt.show()

    return parameters
def model(X,
          Y,
          learning_rate=0.3,
          num_iterations=30000,
          print_cost=True,
          lambd=0,
          keep_prob=1):

    grads = {}
    costs = []  # to keep track of the cost
    m = X.shape[1]  # number of examples
    layers_dims = [X.shape[0], 20, 3, 1]

    # Initialize parameters dictionary.
    parameters = initialize_parameters(layers_dims)

    # Loop (gradient descent)

    for i in range(0, num_iterations):

        # Forward propagation: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID.
        if keep_prob == 1:
            a3, cache = forward_propagation(X, parameters)
        elif keep_prob < 1:
            a3, cache = forward_propagation_with_dropout(
                X, parameters, keep_prob)

        # Cost function
        if lambd == 0:
            cost = compute_cost(a3, Y)
        else:
            cost = compute_cost_with_regularization(a3, Y, parameters, lambd)

        # Backward propagation.
        assert (lambd == 0 or keep_prob == 1
                )  # it is possible to use both L2 regularization and dropout,
        # but this assignment will only explore one at a time
        if lambd == 0 and keep_prob == 1:
            grads = backward_propagation(X, Y, cache)
        elif lambd != 0:
            grads = backward_propagation_with_regularization(
                X, Y, cache, lambd)
        elif keep_prob < 1:
            grads = backward_propagation_with_dropout(X, Y, cache, keep_prob)

        # Update parameters.
        parameters = update_parameters(parameters, grads, learning_rate)

        # Print the loss every 10000 iterations
        if print_cost and i % 10000 == 0:
            print("Cost after iteration {}: {}".format(i, cost))
        if print_cost and i % 1000 == 0:
            costs.append(cost)

    # plot the cost
    plt.plot(costs)
    plt.ylabel('cost')
    plt.xlabel('iterations (x1,000)')
    plt.title("Learning rate =" + str(learning_rate))
    plt.show()

    return parameters
예제 #8
0
def model(X,
          Y,
          learning_rate=0.3,
          num_iterations=30000,
          print_cost=True,
          is_plot=True,
          lambd=0,
          keep_prob=1):
    """
    实现一个三层的神经网络:LINEAR ->RELU -> LINEAR -> RELU -> LINEAR -> SIGMOID

    参数:
        X - 输入的数据,维度为(2, 要训练/测试的数量)
        Y - 标签,【0(蓝色) | 1(红色)】,维度为(1,对应的是输入的数据的标签)
        learning_rate - 学习速率
        num_iterations - 迭代的次数
        print_cost - 是否打印成本值,每迭代10000次打印一次,但是每1000次记录一个成本值
        is_polt - 是否绘制梯度下降的曲线图
        lambd - 正则化的超参数,实数
        keep_prob - 随机保留节点的概率
    返回
        parameters - 学习后的参数
    """
    grads = {}
    costs = []
    m = X.shape[1]
    layers_dims = [X.shape[0], 20, 3, 1]

    #初始化参数
    parameters = reg_utils.initialize_parameters(
        layers_dims)  # 用的是Xavier初始化,感觉不好,应该用He初始化

    #开始学习
    for i in range(0, num_iterations):
        #前向传播
        ##是否随机删除节点
        if keep_prob == 1:
            ###不随机删除节点
            a3, cache = reg_utils.forward_propagation(X, parameters)
        elif keep_prob < 1:
            ###随机删除节点
            a3, cache = forward_propagation_with_dropout(
                X, parameters, keep_prob)
        else:
            print("keep_prob参数错误!程序退出。")
            exit

        #计算成本
        ## 是否使用二范数
        if lambd == 0:
            ###不使用L2正则化
            cost = reg_utils.compute_cost(a3, Y)
        else:
            ###使用L2正则化
            cost = compute_cost_with_regularization(a3, Y, parameters, lambd)

        #反向传播
        ##可以同时使用L2正则化和随机删除节点,但是本次实验不同时使用。
        assert (lambd == 0 or keep_prob == 1)

        ##两个参数的使用情况
        if (lambd == 0 and keep_prob == 1):
            ### 不使用L2正则化和不使用随机删除节点
            grads = reg_utils.backward_propagation(X, Y, cache)
        elif lambd != 0:
            ### 使用L2正则化,不使用随机删除节点
            grads = backward_propagation_with_regularization(
                X, Y, cache, lambd)
        elif keep_prob < 1:
            ### 使用随机删除节点,不使用L2正则化
            grads = backward_propagation_with_dropout(X, Y, cache, keep_prob)

        #更新参数
        parameters = reg_utils.update_parameters(parameters, grads,
                                                 learning_rate)

        #记录并打印成本
        if i % 1000 == 0:
            ## 记录成本
            costs.append(cost)
            if (print_cost and i % 10000 == 0):
                #打印成本
                print("第" + str(i) + "次迭代,成本值为:" + str(cost))

    #是否绘制成本曲线图
    if is_plot:
        plt.plot(costs)
        plt.ylabel('cost')
        plt.xlabel('iterations (x1,000)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

    #返回学习后的参数
    return parameters
예제 #9
0
def model(X, Y, learning_rate = 0.3, num_iterations = 30000, print_cost = True, is_plot = True, lambd = 0, keep_prob = 1):
    """
    实现一个三层的神经网络:ReLU->ReLU->Sigmoid
    :param lambd:正则化的超参数
    :param keep_prob:随机删除结点的概率
    :return:
        parameters - 学习后的参数
    """
    parameter = {}
    grads = {}
    costs = []
    m = X.shape[1]
    layers_dims = [X.shape[0], 20, 3, 1]

    # 初始化参数
    parameters = reg_utils.initialize_parameters(layers_dims)

    for l in range(num_iterations):
        # 前向传播
        # 是否随机删除结点
        if keep_prob == 1:
            # 前向传播
            a3, cache = reg_utils.forward_propagation(X, parameters)
        elif keep_prob < 1:
            a3, cache = forward_propagation_with_dropout(X, parameters, keep_prob)
        else:
            print("keep_prob参数错误!程序退出")
            exit()

        # 计算成本
        # 是否使用正则化
        if lambd == 0:
            cost = reg_utils.compute_cost(a3, Y)
        else:
            cost = compute_cost_with_regularization(a3, Y, parameters, lambd)

        if l % 1000 == 0:
            costs.append(cost)
            if print_cost:
                print(f'第{l}次迭代,成本值为:{cost}')

        # 反向传播
        # 可以同时使用L2正则化和随机删除节点,但本次实验不同时使用
        assert(lambd == 0 or keep_prob == 1)

        if lambd == 0 and keep_prob == 1:
            # 不使用L2正则化和随机删除节点
            grads = reg_utils.backward_propagation(X, Y, cache)
        elif lambd != 0:
            # 使用L2正则化,不使用dropout
            grads = backward_propagation_with_regularization(X, Y, cache, lambd)
        elif keep_prob < 1:
            # 使用dropout,不使用L2正则化
            grads = backward_propagation_with_dropout(X, Y, cache, keep_prob)

        # 更新参数
        parameters = reg_utils.update_parameters(parameters, grads, learning_rate)

    if is_plot:
        plt.plot(costs)
        plt.ylabel('cost')
        plt.xlabel('iterations (x1,000)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

    return parameters
예제 #10
0
def model(X,
          Y,
          learning_rate=0.3,
          num_iterations=30000,
          print_cost=True,
          is_plot=True,
          lambd=0,
          keep_prob=1):
    '''
    实现一个三层的神经网络:linear->relu->linear->relu->linear->sigmoid
    :param X: 输入的数据,维度为(2,要训练/测试的数量)
    :param Y: 标签,[0(蓝色)|1(红色)],维度为(1,对应的是输入的数据的标签)
    :param learing_rate: 学习速率
    :param num_iterations: 迭代次数
    :param print_cost: 是否打印成本值
    :param lambd: 正则化的超参数,实数
    :param keep_prob: 随机删除节点的概率
    :return: 
    parameters-学习后的参数
    '''
    grads = {}
    costs = []
    m = X.shape[1]
    layers_dims = [X.shape[0], 20, 3, 1]

    #初始化参数
    parameters = reg_utils.initialize_parameters(layers_dims)

    #开始学习
    for i in range(0, num_iterations):
        #前向传播
        ##是否删除随机节点
        if keep_prob == 1:
            ###不删除随机节点
            a3, cache = reg_utils.forward_propagation(X, parameters)
        elif keep_prob < 1:
            ###随即删除节点
            a3, cache = forward_propagation_with_dropout(
                X, parameters, keep_prob)
        else:
            print('keep_prob参数错误,程序退出.')
            exit

        #计算成本
        ##是否使用二范数
        if lambd == 0:
            ###不使用L2正则化
            cost = reg_utils.compute_cost(a3, Y)
        else:
            ###使用L2正则化
            cost = compute_cost_with_regularization(a3, Y, parameters, lambd)

        ##反向传播
        ##可以同时使用L2正则化和随机删除节点,但本次实验不同时使用
        assert (lambd == 0 or keep_prob == 1)

        ##两个参数的使用情况
        if (lambd == 0 and keep_prob == 1):
            ###不适用L2正则化和不使用随即删除节点
            grads = reg_utils.backward_propagation(X, Y, cache)
        elif lambd != 0:
            ###使用L2正则化,不使用随即删除节点
            grads = backward_propagation_with_regularization(
                X, Y, cache, lambd)
        elif keep_prob < 1:
            ###使用随即删除节点,不使用L2正则化
            grads = backward_propagation_with_dropout(X, Y, cache, keep_prob)

        #更新参数
        parameters = reg_utils.update_parameters(parameters, grads,
                                                 learning_rate)

        #记录并打印成本
        if i % 1000 == 0:
            ##记录成本
            costs.append(cost)
            if (print_cost and i % 10000 == 0):
                #打印成本
                print('第' + str(i) + '次迭代,成本值为:' + str(cost))

    #是否绘制成本曲线图
    if is_plot:
        plt.plot(costs)
        plt.ylabel('cost')
        plt.xlabel('iterations(x1,000)')
        plt.title('Learning rate = ' + str(learning_rate))
        plt.show()

    #返回学习后的参数
    return parameters
        if keep_prob == 1:
            a3, cache = forward_propagation(X, parameters)
        elif keep_prob < 1:
            a3, cache = forward_propagation_with_dropout(X, parameters, keep_prob)

        # Cost function
        if lambd == 0:
            cost = compute_cost(a3, Y)
        else:
            cost = compute_cost_with_regularization(a3, Y, parameters, lambd)

        # Backward propagation.
        assert(lambd==0 or keep_prob==1)    # it is possible to use both L2 regularization and dropout,
                                            # but this assignment will only explore one at a time
        if lambd == 0 and keep_prob == 1:
            grads = backward_propagation(X, Y, cache)
        elif lambd != 0:
            grads = backward_propagation_with_regularization(X, Y, cache, lambd)
        elif keep_prob < 1:
            grads = backward_propagation_with_dropout(X, Y, cache, keep_prob)

        # Update parameters.
        parameters = update_parameters(parameters, grads, learning_rate)

        # Print the loss every 10000 iterations
        if print_cost and i % 10000 == 0:
            print("Cost after iteration {}: {}".format(i, cost))
        if print_cost and i % 1000 == 0:
            costs.append(cost)

    # plot the cost