Example #1
0
def compute_cost_with_regularization(A3, Y, parameters, lambd):
    """
    Implement the cost function with L2 regularization. See formula (2) above.
    
    Arguments:
    A3 -- post-activation, output of forward propagation, of shape (output size, number of examples)
    Y -- "true" labels vector, of shape (output size, number of examples)
    parameters -- python dictionary containing parameters of the model
    
    Returns:
    cost - value of the regularized loss function (formula (2))
    """
    m = Y.shape[1]
    W1 = parameters["W1"]
    W2 = parameters["W2"]
    W3 = parameters["W3"]
    
    cross_entropy_cost = compute_cost(A3, Y) # This gives you the cross-entropy part of the cost
    
    ### START CODE HERE ### (approx. 1 line)
    L2_regularization_cost = 1.0 / m * lambd / 2 * (np.sum(np.square(W1)) + np.sum(np.square(W2)) + np.sum(np.square(W3)))
    ### END CODER HERE ###
    
    cost = cross_entropy_cost + L2_regularization_cost
    
    return cost
Example #2
0
def model(X,
          Y,
          learning_rate=0.3,
          num_iterations=30000,
          print_cost=True,
          lambd=0,
          keep_prob=1):
    grads = {}
    costs = []
    m = X.shape[1]
    layers_dims = [X.shape[0], 20, 3, 1]
    parameters = initialize_parameters(layers_dims)
    for i in range(0, num_iterations):
        if keep_prob == 1:
            a3, cache = forward_propagation(X, parameters)
        elif keep_prob < 1:
            a3, cache = forward_propagation_with_dropout_test_case(
                X, parameters, keep_prob)
        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:
            backward_propagation_with_dropout(X, Y, cache, keep_prob)
        parameters = update_parameters(parameters, grads, learning_rate)
        if print_cost and i % 10000 == 0:
            print("Cost after iteration {}: {}".format(i, cost))
        if print_cost and i % 1000 == 0:
            costs.append(cost)
    plt.plot(costs)
    plt.ylabel('cost')
    plt.xlabel('iterations (x1,000)')
    plt.title("Learning rate =" + str(learning_rate))
    plt.show()

    return parameters
Example #3
0
def compute_cost_with_regularization(A3,Y,parameters,lambd):
    """
        L2 正则化成本计算
    参数:
        A3 -正向传播的输出结果,维度(输出节点数量,训练/测试数量)
        Y  -标签向量    维度(输出节点数量,训练/测试数量)
        parameters -模型学习后的参数字典
        lambd -正则化超参数
    返回:
        cost -正则化损失的值
    """
    m=Y.shape[1]
    W1=parameters["W1"]
    W2=parameters["W2"]
    W3=parameters["W3"]

    cross_entropy_cost=reg_utils.compute_cost(A3,Y)
    L2_regularization_cost=lambd*(np.sum(np.square(W1))+np.sum(np.square(W2))+np.sum(np.square(W3)))/(2*m)

    cost=cross_entropy_cost+L2_regularization_cost
    return cost
def compute_cost_with_regularization(A3, Y, parameters, lambd):
    """
    Implement the cost function with L2 regularization. 
    
    Arguments:
    A3 -- post-activation, output of forward propagation, of shape (output size, number of examples)
    Y -- "true" labels vector, of shape (output size, number of examples)
    parameters -- python dictionary containing parameters of the model
    
    Returns:
    cost - value of the regularized loss function 
    """
    m = Y.shape[1]
    cost = compute_cost(A3, Y)  # without Regularization term
    L = len(parameters) // 2
    W = 0
    for i in range(L):
        W += np.sum(parameters["W" + str(i + 1)]**2)

    cost += (lambd * W) / (2 * m)

    return cost
def compute_cost_with_regularization(A3,Y,parameters,lambd):
    '''
    实现L2正则化计算成本
    :param A3: 正向传播的结果,维度为(输出节点数量,训练/测试的数量)
    :param Y: 标签向量,与数据一一对应,维度为(输出节点数量,驯良/测试的数量)
    :param parameters: 包含模型学习后的参数的字典
    :param lambd: 
    :return: 
    cost - 使用公式2计算出来的正则化损失函值
    '''

    m=Y.shape[1]
    W1 = parameters['W1']
    W2 = parameters['W2']
    W3 = parameters['W3']

    cross_entropy_cost = reg_utils.compute_cost(A3,Y)

    L2_regularization_cost = lambd*(np.sum(np.square(W1))+np.sum(np.square(W2))+np.sum(np.square(W3)))/(m*2)
    cost = cross_entropy_cost + L2_regularization_cost

    return cost
def compute_cost_with_regularization(A3, Y, parameters, lambd):
    """
    Implement the cost function with L2 regularization. See formula (2) above.
    
    Arguments:
    A3 -- post-activation, output of forward propagation, of shape (output size, number of examples)
    Y -- "true" labels vector, of shape (output size, number of examples)
    parameters -- python dictionary containing parameters of the model
    
    Returns:
    cost - value of the regularized loss function (formula (2))
    """
    m = Y.shape[1]
    W1 = parameters["W1"]
    W2 = parameters["W2"]
    W3 = parameters["W3"]
    
    cross_entropy_cost = compute_cost(A3, Y) # This gives you the cross-entropy part of the cost 
    L2_regularization_cost = (np.sum(np.square(W1))+np.sum(np.square(W2))+np.sum(np.square(W3)))*lambd/2./m
    cost = cross_entropy_cost + L2_regularization_cost
    
    return cost
Example #7
0
def compute_cost_with_regularization(A3, Y, parameters, lambd):
    '''
    实现L2正则化计算成本
    :param A3: 正向传播的结果,维度为(输出节点数量,训练/测试的数量)
    :param Y: 标签向量,与数据一一对应,维度为(输出节点数量,驯良/测试的数量)
    :param parameters: 包含模型学习后的参数的字典
    :param lambd: 
    :return: 
    cost - 使用公式2计算出来的正则化损失函值
    '''

    m = Y.shape[1]
    W1 = parameters['W1']
    W2 = parameters['W2']
    W3 = parameters['W3']

    cross_entropy_cost = reg_utils.compute_cost(A3, Y)

    L2_regularization_cost = lambd * (np.sum(np.square(W1)) + np.sum(
        np.square(W2)) + np.sum(np.square(W3))) / (m * 2)
    cost = cross_entropy_cost + L2_regularization_cost

    return cost
Example #8
0
def compute_cost_with_regularization(A3, Y, parameters, lambd):
    """
    实现公式2的L2正则化计算成本

    参数:
        A3 - 正向传播的输出结果,维度为(输出节点数量,训练/测试的数量)
        Y - 标签向量,与数据一一对应,维度为(输出节点数量,训练/测试的数量)
        parameters - 包含模型学习后的参数的字典
    返回:
        cost - 使用公式2计算出来的正则化损失的值

    """
    m = Y.shape[1]
    W1 = parameters["W1"]
    W2 = parameters["W2"]
    W3 = parameters["W3"]
    cross_entropy_cost = reg_utils.compute_cost(A3, Y)
    L2_regularization_cost = lambd * (np.sum(np.square(W1)) + np.sum(
        np.square(W2)) + np.sum(np.square(W3))) / (2 * m)

    cost = cross_entropy_cost + L2_regularization_cost

    return cost
def compute_cost_with_regularization(A3, Y, parameters, lambd):
    """
    Implement the cost function with L2 regularization. See formula (2) above.
    
    Arguments:
    A3 -- post-activation, output of forward propagation, of shape (output size, number of examples)
    Y -- "true" labels vector, of shape (output size, number of examples)
    parameters -- python dictionary containing parameters of the model
    
    Returns:
    cost - value of the regularized loss function (formula (2))
    """
    m = Y.shape[1]
    W1 = parameters["W1"]
    W2 = parameters["W2"]
    W3 = parameters["W3"]
    
    # Modify the cost and observe the consequences
    cross_entropy_cost = compute_cost(A3, Y)            # This gives you the cross-entropy part of the cost
    L2_regularization_cost = lambd * (np.sum(np.square(W1)) + np.sum(np.square(W2)) + np.sum(np.square(W3))) / (2 * m) # do this for the weights as well to sum up the three terms
    
    cost = cross_entropy_cost + L2_regularization_cost
    
    return cost
Example #10
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
def model(X,
          Y,
          learning_rate=0.3,
          num_iterations=30000,
          print_cost=True,
          lambd=0,
          keep_prob=1):

    grads = {}
    costs = []

    m = X.shape[1]

    layer_dims = [X.shape[0], 20, 3, 1]

    parameters = initialize_parameters(layer_dims)

    for i in range(num_iterations):

        if keep_prob == 1:

            A3, cache = forward_propagation(X, parameters)

        elif keep_prob < 1:

            A3, cache = forward_propagation_with_dropout(
                X, parameters, keep_prob)

        if lambd == 0:

            cost = compute_cost(A3, Y)

        else:

            cost = compute_cost_with_regulations(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)

        parameters = update_parameters(parameters, grads, learning_rate)

        if print_cost and i % 1000 == 0:

            print("Cost After iterations {} : {}".format(i, cost))

            costs.append(cost)

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

    plt.savefig('/home/guanlingh/local2server/homework6/image/h6-2.jpg')

    return parameters
Example #12
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
Example #13
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
Example #14
0
def L_layer_model(X,
                  Y,
                  layers_dims,
                  learning_rate=0.0075,
                  num_iterations=3000,
                  print_cost=False):  #lr was 0.009
    """
    Implements a L-layer neural network: [LINEAR->RELU]*(L-1)->LINEAR->SIGMOID.
    
    Arguments:
    X -- data, numpy array of shape (number of examples, num_px * num_px * 3)
    Y -- true "label" vector (containing 0 if cat, 1 if non-cat), of shape (1, number of examples)
    layers_dims -- list containing the input size and each layer size, of length (number of layers + 1).
    learning_rate -- learning rate of the gradient descent update rule
    num_iterations -- number of iterations of the optimization loop
    print_cost -- if True, it prints the cost every 100 steps
    
    Returns
    parameters -- parameters learnt by the model. They can then be used to predict.
    """

    np.random.seed(1)
    costs = []  # keep track of cost

    # Parameters initialization.
    ### START CODE HERE ###
    parameters = initialize_parameters_deep(layers_dims)
    ### END CODE HERE ###

    # Loop (gradient descent)
    for i in range(0, num_iterations):

        # Forward propagation: [LINEAR -> RELU]*(L-1) -> LINEAR -> SIGMOID.
        ### START CODE HERE ### (~ 1 line of code)
        AL, caches = L_model_forward(X, parameters)
        ### END CODE HERE ###

        # Compute cost.
        ### START CODE HERE ### (~ 1 line of code)
        cost = compute_cost(AL, Y)
        ### END CODE HERE ###

        # Backward propagation.
        ### START CODE HERE ### (~ 1 line of code)
        grads = L_model_backward(AL, Y, caches)
        ### END CODE HERE ###

        # Update parameters.
        ### START CODE HERE ### (~ 1 line of code)
        parameters = update_parameters(parameters, grads, learning_rate)
        ### END CODE HERE ###

        # Print the cost every 100 training example
        if print_cost and i % 1 == 0:
            print("Cost after iteration %i: %f" % (i, cost))
        if print_cost and i % 100 == 0:
            costs.append(cost)

    # plot the cost
    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 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
def model2(X,
           Y,
           learning_rate=0.3,
           num_iterations=30000,
           print_cost=True,
           is_plot=True,
           lambd=0,
           keep_prob=1):

    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:
            cost = reg_utils.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 = reg_utils.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)

        # 更新参数
        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
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
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
Example #19
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)

    # 开始学习
    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 = reg_utils.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 = reg_utils.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 = reg_utils.backward_propagation_with_regularization(X, Y, cache, lambd)
        elif keep_prob < 1:
            ### 使用随机删除节点,不使用L2正则化
            grads =reg_utils. 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
Example #20
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
Example #21
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
    # 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)