In [2]:
# import packages
import numpy as np
import matplotlib.pyplot as plt
from reg_utils import sigmoid, relu, plot_decision_boundary, initialize_parameters, load_2D_dataset, predict_dec
from reg_utils import compute_cost, predict, forward_propagation, backward_propagation, update_parameters
import sklearn
import sklearn.datasets
import scipy.io

%matplotlib inline
plt.rcParams['figure.figsize'] = (7.0, 4.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

In [3]:
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


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/scipy/io/matlab/mio.py in _open_file(file_like, appendmat)
     30     try:
---> 31         return open(file_like, 'rb'), True
     32     except IOError:

FileNotFoundError: [Errno 2] No such file or directory: 'datasets/data.mat'

During handling of the above exception, another exception occurred:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-3-a9a84d38b990> in <module>
----> 1 train_X, train_Y, test_X, test_Y = load_2D_dataset()

~/Desktop/Neural Net Forecasting/reg_utils.py in load_2D_dataset()
    322 
    323 def load_2D_dataset():
--> 324     data = scipy.io.loadmat('datasets/data.mat')
    325     train_X = data['X'].T
    326     train_Y = data['y'].T

/anaconda3/lib/python3.6/site-packages/scipy/io/matlab/mio.py in loadmat(file_name, mdict, appendmat, **kwargs)
    205     """
    206     variable_names = kwargs.pop('variable_names', None)
--> 207     MR, file_opened = mat_reader_factory(file_name, appendmat, **kwargs)
    208     matfile_dict = MR.get_variables(variable_names)
    209     if mdict is not None:

/anaconda3/lib/python3.6/site-packages/scipy/io/matlab/mio.py in mat_reader_factory(file_name, appendmat, **kwargs)
     60 
     61     """
---> 62     byte_stream, file_opened = _open_file(file_name, appendmat)
     63     mjv, mnv = get_matfile_version(byte_stream)
     64     if mjv == 0:

/anaconda3/lib/python3.6/site-packages/scipy/io/matlab/mio.py in _open_file(file_like, appendmat)
     35             if appendmat and not file_like.endswith('.mat'):
     36                 file_like += '.mat'
---> 37             return open(file_like, 'rb'), True
     38         else:
     39             raise IOError('Reader needs file name or open file-like object')

FileNotFoundError: [Errno 2] No such file or directory: 'datasets/data.mat'

In [ ]: