Ejemplo n.º 1
0
def test_gradient_big_values():
    test_n_x = 61
    test_n_h = 120
    test_n_y = 10
    max_value = 0.50

    grads = gradient.initialize_parameters_deep([test_n_x, test_n_h, test_n_y])
    assert __is_any_value_huge__(grads, max_value) == False
Ejemplo n.º 2
0
def test_gradient_koefs():
    ...
    test_n_x= 5
    test_n_h= 10
    test_n_y= 2
    grads= gradient.initialize_parameters_deep([test_n_x, test_n_h, test_n_y])
    assert grads is not None
    assert grads["W1"].shape == (test_n_h, test_n_x)
    assert grads["W2"].shape == (test_n_y, test_n_h)
    assert grads["b1"].shape == (test_n_h, 1)
    assert grads["b2"].shape == (test_n_y, 1)
Ejemplo n.º 3
0
def test_init_params():
    np.random.seed(2)
    n_x = 4
    n_h = 5
    n_y = 3
    parameters = gradient.initialize_parameters_deep([n_x, n_h, n_y])
    test_parameters = saved_data.get_saved_parameters()
    print('my W1', parameters['W1'])
    print('his W1', test_parameters['W1'])
    assert (np.isclose(parameters['W1'], test_parameters['W1'])).all()
    assert (np.isclose(parameters['b1'], test_parameters['b1'])).all()
    assert (np.isclose(parameters['W2'], test_parameters['W2'])).all()
    assert (np.isclose(parameters['b2'], test_parameters['b2'])).all()
Ejemplo n.º 4
0
def test_gradient_randomization():
    test_n_x = 61
    test_n_x = 61
    test_n_h = 120
    test_n_y = 10
    grads = gradient.initialize_parameters_deep([test_n_x, test_n_h, test_n_y])
    # determinant can be counted only for square matrixes
    for hidden_layer_id in ["W1", "W2"]:
        num_of_zeros = 0
        num_of_all_values = grads[hidden_layer_id].shape[0] * grads[hidden_layer_id].shape[1]
        for index, value in np.ndenumerate(grads[hidden_layer_id]):
            if value == 0:
                num_of_zeros += 1

    max_percent_of_zeros = 0.7  # we don't let gradient contain more than 70% of zeros
    assert num_of_zeros < num_of_all_values * max_percent_of_zeros
def start_nn_model_learning(X,
                            Y,
                            nn_architecture: list,
                            optimization_algorithm_name,
                            activation_function_name,
                            num_iterations,
                            learning_rate,
                            print_cost_function=None):
    """
    Arguments:
    X -- dataset of shape (2, number of examples)
    Y -- labels of shape (1, number of examples)
    n_h -- size of the hidden layer
    num_iterations -- Number of iterations in gradient descent loop
    print_cost -- if True, print the cost every 1000 iterations

    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    """
    global last_cost
    NUMBER_OF_LABELS = 10  # from 0 to 9
    INPUT_LAYER_SIZE = 400  # as 20 * 20 = 400
    nn_architecture = [INPUT_LAYER_SIZE] + nn_architecture + [NUMBER_OF_LABELS]
    # Initialize parameters
    parameters = __gradient__.initialize_parameters_deep(nn_architecture)
    ## set up optimization algorithm for better learning
    optimization_algorithm = __gradient_descent__
    if (optimization_algorithm_name == OPTIMIZATION_GRADIENT_DESCENT):
        optimization_algorithm = __gradient_descent__
    elif (optimization_algorithm_name ==
          OPTIMIZATION_STOCHASTIC_GRADIENT_DESCENT):
        raise BaseException(
            'Stochastic gradient descent is not implemented yet!')
    elif (optimization_algorithm_name == OPTIMIZATION_BATCH_GRADIENT_DESCENT):
        raise BaseException('Batch gradient descent is not implemented yet!')
    ## set up activation function
    activation_module = None
    if (activation_function_name == ACTIVATION_TANH):
        activation_module = np.tanh
    elif (activation_function_name == ACTIVATION_SIGMOID):
        activation_module = __sigmoid__
    elif (activation_function_name == ACTIVATION_RELU):
        activation_module = __relu__

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

        # Forward propagation. Inputs: "X, parameters". Outputs: "A2, cache".
        AL, caches = __propagations__.L_model_forward(X, parameters)

        # Cost function. Inputs: "A2, Y, parameters". Outputs: "cost".
        current_cost = __cost__.compute_cost(AL, Y)

        # Backpropagation. Inputs: "parameters, cache, X, Y". Outputs: "grads".
        grads = __propagations__.L_model_backward(AL, Y, caches)

        # Gradient descent parameter update. Inputs: "parameters, grads". Outputs: "parameters".
        parameters = optimization_algorithm.update_parameters(
            parameters, grads, learning_rate)

        # Print the cost every 1000 iterations
        if print_cost_function is not None and i % 10 == 0:
            print(str(current_cost))
            #print_cost_function(current_cost)

    return parameters