Esempio n. 1
0
 def test_L_model_forward_t1(self):
     X = np.random.randn(3, 6)
     params = forward.initialize_parameters([3, 5, 7, 8, 2])
     use_batchnorm = False
     forward_result = forward.L_model_forward(X, params, use_batchnorm)[0]
     self.assertTrue(forward_result.shape == (2, 6))
     np.testing.assert_allclose(np.sum(forward_result, axis=0), np.ones(6))
def L_layer_model(X,
                  Y,
                  layers_dims,
                  learning_rate=0.0075,
                  num_iterations=3001,
                  print_cost=False):
    lambd = 1
    costs = []
    parameters = initialize.initialize_parameters_deep(layers_dims)

    for i in range(0, num_iterations):
        AL, caches = forward.L_model_forward(X, parameters)

        costVal = cost.compute_cost(AL, Y, parameters, lambd)

        grads = backward.L_model_backward(AL, Y, caches, lambd)

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

        if print_cost and i % 100 == 0:
            print("Cost after iteration %i: %f" % (i, costVal))
        if print_cost and i % 100 == 0:
            costs.append(costVal)

    plt.plot(np.squeeze(costs))
    plt.ylabel('cost')
    plt.xlabel('iterations (per hundreds)')
    plt.title("Learning rate =" + str(learning_rate))
    plt.show()

    return parameters
def predict(X, y, parameters, dataset):
    m = X.shape[1]
    n = len(parameters) // 2
    p = np.zeros((1,m), dtype = np.int)
    
    probas, caches = forward.L_model_forward(X, parameters)

    for i in range(0, probas.shape[1]):
        if probas[0,i] > 0.5:
            p[0,i] = 1
        else:
            p[0,i] = 0

    print(dataset + " accuracy: "  + str(np.mean((p[0,:] == y[0,:]))))
        
    return p
Esempio n. 4
0
def predict(X, Y, parameters):
    """
    The function receives an input data and the true labels and
    calculates the accuracy of the trained neural network on the data.

    :param X: the input data, a numpy array of shape (height*width, number_of_examples)
    :param Y: the “real” labels of the data, a vector of shape (num_of_classes, number of examples)
    :param parameters: a python dictionary containing the DNN architecture’s parameters
    :return accuracy: the accuracy measure of the neural net on the provided data
                    (i.e. the percentage of the samples for which the correct
                     label receives the hugest confidence score).
                     Using the softmax function to normalize the output values
    """
    m = X.shape[1]
    prediction, caches = forward.L_model_forward(X,
                                                 parameters,
                                                 use_batchnorm=BATCHNORM_USAGE)
    prediction_arg_max = np.argmax(prediction, axis=0)
    label_arg_max = np.argmax(Y, axis=0)
    correct_predictions = np.sum(prediction_arg_max == label_arg_max)
    accuracy = correct_predictions / m
    return accuracy
Esempio n. 5
0
def forward_test():
    np.random.seed(1)
    A = np.random.randn(3, 2)
    W = np.random.randn(1, 3)
    b = np.random.randn(1, 1)

    Z, linear_cache = forward.linear_forward(A, W, b)
    print("Z = " + str(Z))

    np.random.seed(2)
    A_prev = np.random.randn(3, 2)
    W = np.random.randn(1, 3)
    b = np.random.randn(1, 1)

    A, linear_activation_cache = forward.linear_activation_forward(
        A_prev, W, b, activation="sigmoid")
    print("With sigmoid: A = " + str(A))

    A, linear_activation_cache = forward.linear_activation_forward(
        A_prev, W, b, activation="relu")
    print("With ReLU: A = " + str(A))

    np.random.seed(6)
    X = np.random.randn(5, 4)
    W1 = np.random.randn(4, 5)
    b1 = np.random.randn(4, 1)
    W2 = np.random.randn(3, 4)
    b2 = np.random.randn(3, 1)
    W3 = np.random.randn(1, 3)
    b3 = np.random.randn(1, 1)

    parameters = {"W1": W1, "b1": b1, "W2": W2, "b2": b2, "W3": W3, "b3": b3}

    AL, caches = forward.L_model_forward(X, parameters)
    print("AL = " + str(AL))
    print("Length of caches list = " + str(len(caches)))
Esempio n. 6
0
def L_layer_model(X, Y, layers_dims, learning_rate, num_iterations,
                  batch_size):
    """
    Implements a L-layer neural network.
    All layers but the last  have the ReLU activation function,
    and the final layer apply the softmax activation function
    :param X: the input data, a numpy array of shape (height*width , number_of_examples)
    :param Y: the “real” labels of the data, a vector of shape (num_of_classes, number of examples)
    :param layers_dims: a list containing the dimensions of each layer, including the input
    :param learning_rate: the learning rate used to update the parameters (the “alpha”)
    :param num_iterations:
    :param batch_size: the number of examples in a single training batch
    :return parameters: the parameters learnt by the system during the training
                        (the same parameters that were updated in the update_parameters function).
    :return costs: the values of the cost function.
                    One value is to be saved after each 100 training iterations (e.g. 3000 iterations -> 30 values).

    """
    stop_rate = 0.001

    train_x, train_y, validation_x, validation_y = generate_validation_data(
        X, Y)
    del X, Y

    combined_data = np.concatenate([train_x, train_y], axis=0)
    m = train_x.shape[1]

    num_batches = m // batch_size + (1 if m % batch_size else 0)

    params = forward.initialize_parameters(layers_dims)
    costs = []

    steps_cnt = 0
    while len(costs) < 2 or np.abs(costs[-2] - costs[-1]) > stop_rate:
        if not steps_cnt % num_batches:
            np.random.shuffle(combined_data.T)
            batches = np.array_split(combined_data,
                                     indices_or_sections=num_batches,
                                     axis=1)

        X_batch = batches[steps_cnt % num_batches][0:train_x.shape[0], :]
        Y_batch = batches[steps_cnt % num_batches][train_x.shape[0]:, :]

        prediction, caches = forward.L_model_forward(
            X_batch, params, use_batchnorm=BATCHNORM_USAGE)
        grads = backward.L_model_backward(prediction, Y_batch, caches)
        params = backward.update_parameters(params, grads, learning_rate)

        steps_cnt += 1

        if not steps_cnt % num_iterations:
            prediction, _ = forward.L_model_forward(
                validation_x, params, use_batchnorm=BATCHNORM_USAGE)
            cost = forward.compute_cost(prediction, validation_y)
            costs.append(cost)
            print(f'\tStep number: {steps_cnt} - Cost {cost:.3f}')

    steps_str = '' if not steps_cnt % num_batches else f' and {steps_cnt % num_batches} steps'
    print(f'\nRan over {steps_cnt // num_batches} epochs' + steps_str)

    print(
        f"\nTrain Accuracy: {predict(X=train_x, Y=train_y, parameters=params)*100:.2f}%"
    )
    print(
        f"Validation Accuracy: {predict(X=validation_x, Y=validation_y, parameters=params) * 100:.2f}%"
    )

    return params, costs