Project - Building Cat vs Non-Cat Image Classifier using NumPy and ANN

27 / 32

Cat vs Non-cat Classifier - Defining some utility functions - Model

The function model is the holistic function, which uses all the other helper functions to train the algorithm.

We are essentially creating models with different learning rates and pick the one which yields reasonable train and validation accuracies. We also parallelly take care that the model doesn't overfit, by making note of the epochs that yield reasonable results.

Finally, the best parameters are returned in the form of a dictionary containing the corresponding values.

INSTRUCTIONS

Copy-paste the following code for the model function.

Call the initialize_weights function and optimize function at the appropriate places inside the model function.

def  model(X_train, Y_train, X_val, Y_val, num_iterations=2000, learning_rate=[0.5]):
    prev_train_acc=0
    prev_val_acc=0

    #  Initialize weights and bias 
    w, b = << your code comes here >>(X_train.shape[0])

    best_values = {
        'final w':w,
        'final b':b,
        'Train accuracy':prev_train_acc,
        'Validation accuracy': prev_val_acc,
     }


    for lr in learning_rate:
        print(("-"*30 + "learning_rate:{}"+"-"*30).format(lr))

        # Initialize weights and bias
        w, b = << your code comes here >>(X_train.shape[0])


        # Optimization
        lr_optimal_values = << your code comes here >>(w, b, X_train, Y_train, X_val, Y_val, num_iterations, lr)
        if lr_optimal_values['Validation accuracy']>prev_val_acc:
            prev_val_acc = lr_optimal_values['Validation accuracy']
            prev_train_acc = lr_optimal_values['Train accuracy']
            final_lr = lr
            final_w = lr_optimal_values['final w']
            final_b = lr_optimal_values['final b']
            final_epoch = lr_optimal_values['epoch']
            final_Y_prediction_val = lr_optimal_values['Y_prediction_val']
            final_Y_prediction_train = lr_optimal_values['Y_prediction_train']

    best_values['Train accuracy'] = prev_train_acc
    best_values['Validation accuracy'] = prev_val_acc
    best_values['final_lr'] = final_lr
    best_values['final w'] = final_w
    best_values['final b'] = final_b
    best_values['epoch'] = final_epoch
    best_values['Y_prediction_val'] = final_Y_prediction_val
    best_values['Y_prediction_train'] = final_Y_prediction_train

    return best_values
Get Hint See Answer


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...