{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport sys\nimport cv2\nimport random\nfrom tqdm import tqdm\nfrom matplotlib import pyplot as plt\nfrom sklearn.model_selection import train_test_split\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"80189d8b-4464-4e26-bd7e-5c97811ede06","_uuid":"c15e567bd602ec7b98bae6eed0732bb4a77b29a9"},"cell_type":"markdown","source":"**Data Loading & Preprocessing**"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# import data\ncsv_train = pd.read_csv('../input/labels.csv')\ncsv_test = pd.read_csv('../input/sample_submission.csv')","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"03943252-b4d8-47ae-b861-2f3dc435b211","_uuid":"9924a01d270683e413cc88acd7ed8542a3147f16","trusted":true},"cell_type":"code","source":"# read training CSV\ncsv_train.head(10)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"e563c4d0-7617-4bdd-a26b-e7d95122e475","_uuid":"38cebbf6e06b3c1b28169bfc7e0b6feb5a042973","trusted":true},"cell_type":"code","source":"# read test csv\ncsv_test.head(10)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"2023ad18-54a2-46a0-a829-b4a83d050d95","_uuid":"c38812d36db72779944a03df3b38d844d5eec33f","trusted":true},"cell_type":"code","source":"# Generate Labels\ntargets_series = pd.Series(csv_train['breed'])\n#print(targets_series)\none_hot = pd.get_dummies(targets_series, sparse = True)\nlabels = np.asarray(one_hot)\nn_check = random.randint(0, len(labels)-1)\nprint(csv_train['breed'][n_check], 'is encoded as', ''.join((str(i) for i in labels[n_check])))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"ddf0c7c8-2afe-41f7-8960-9b1502c86d88","_uuid":"59a155f81a255e4848441ca2495a26757408f38c","trusted":true},"cell_type":"code","source":"im_size = 100\nx_train = []\ny_train = []\nx_test = []","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"4a97de63-a110-4dd2-a636-b64ab2b586b6","_uuid":"6d352457cedbcfe37a22b5b3ae34bb44e07ef37e","trusted":true},"cell_type":"code","source":"for i, (f, breed) in enumerate(tqdm(csv_train.values)):\n    img = cv2.imread('../input/train/{}.jpg'.format(f))\n    x_train.append(cv2.resize(img, (im_size, im_size)))\n    y_train.append(labels[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"60bf1724f3323e47888cde52cf3bfbd7b9530760"},"cell_type":"code","source":" y_train","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"c9ed96ca-2019-4b7c-a9a0-7298e999e8c6","_uuid":"736c0d75babe95cf13389db7aff874863d93d52e","trusted":true},"cell_type":"code","source":"# check train\nn_check = random.randint(0, len(y_train)-1)\nprint('label:', ''.join((str(i) for i in y_train[n_check])))\nplt.imshow(x_train[n_check], cmap = 'gray', interpolation = 'bicubic')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"52a7da65-c507-40b0-b33c-035474b4b9da","_uuid":"5806c686b3ba739b9fbf50a87c60d208a5ce137e","trusted":true},"cell_type":"code","source":"for f in tqdm(csv_test['id'].values):\n    img = cv2.imread('../input/test/{}.jpg'.format(f))\n    x_test.append(cv2.resize(img, (im_size, im_size)))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"0048c008-150d-470f-a4ad-6245b4995573","_uuid":"5ddb49c1e9d4f89aeea9783b2d44747a0d2316e5"},"cell_type":"markdown","source":"import pickle\nwith open('../output/train.txt', \"wb\") as fp:\n    pickle.dump(x_train, fp)  #Pickling\n    \nwith open(\"../output/test.txt\", \"wb\") as fp:\n    pickle.dump(x_test, fp)   #Pickling\n"},{"metadata":{"_cell_guid":"acfc5bbf-f518-4ac6-a04f-16e8fceb9a13","_uuid":"65e3a55361681305001f962ba0bad3e41bc4281d","trusted":true},"cell_type":"code","source":"# build np array and normalise them\nx_train = np.array(x_train) /255.\ny_train = np.array(y_train, np.uint8)\nx_test  = np.array(x_test) /255.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"d523c782-8b02-4838-ae21-30ce282c4638","_uuid":"e5801818ed4ca258d6e0fc360bd7fa70a3c29d62","trusted":true},"cell_type":"code","source":"print(\"x_train shape:\", x_train.shape)\nprint(\"y_train shape:\", y_train.shape)\nprint(\"x_test shape:\", x_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"24375621-a88c-40b7-9f6d-aec11af152c1","_uuid":"fdada902c2e5f24add1fbe0cabe0f2fc4762898d","trusted":true},"cell_type":"code","source":"X_train, X_valid, Y_train, Y_valid = train_test_split(x_train, y_train,\n                                                      test_size=0.3, random_state=42,\n                                                      stratify=y_train)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"7cb17550-dbb7-4bbd-888f-69ef1ead4f3a","_uuid":"0e220e79694700a650b649fe93538f8a1c52ce35","trusted":true},"cell_type":"code","source":"# Reshape the training and test examples \ntrain_x = X_train.reshape(X_train.shape[0], -1).T   # The \"-1\" makes reshape flatten the remaining dimensions\nvalid_x = X_valid.reshape(X_valid.shape[0], -1).T\ntest_x = x_test.reshape(x_test.shape[0], -1).T\n\n# Standardize data to have feature values between 0 and 1.\n#train_x = train_x_flatten/255.\n#valid_x = valid_x_flatten/255.\n#test_x = test_x_flatten/255.\ntrain_Y = Y_train.T\nvalid_Y= Y_valid.T\n\nprint (\"train_x 's shape: \" + str(train_x .shape))\nprint (\"train_Y's shape: \" + str(train_Y .shape))\nprint (\"valid_x's shape: \" + str(valid_x.shape))\nprint (\"valid_Y's shape: \" + str(valid_Y.shape))\nprint (\"test_x's shape: \" + str(test_x.shape))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"bf2a6bc8-b1ed-43c3-a2c9-dd9c9d7a0b7f","_uuid":"3b85e4a46abbaafc8e27c141d786b228d39097a5","trusted":true},"cell_type":"code","source":"X_train=[]\nX_valid=[] \nY_train=[] \nY_valid=[]\nx_train=[]\ny_train=[]\nx_test=[]","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"2fb4ef40-b326-4087-b0e0-78b68fd2ff0a","_uuid":"a81ac78a9155def2787d954b6003fe678b62dbbc"},"cell_type":"markdown","source":"**Helper Function**"},{"metadata":{"_cell_guid":"2be5de87-3bab-4c0d-aa3c-ac3727e6bbf6","_uuid":"5e82f1e412922e512f5bbb62aefa7a4594b08345","trusted":true},"cell_type":"code","source":"def sigmoid(Z):\n    \"\"\"\n    Implements the sigmoid activation in numpy\n    \n    Arguments:\n    Z -- numpy array of any shape\n    \n    Returns:\n    A -- output of sigmoid(z), same shape as Z\n    cache -- returns Z as well, useful during backpropagation\n    \"\"\"\n    \n    A = 1/(1+np.exp(-Z))\n    cache = Z\n    \n    return A, cache\n\ndef relu(Z):\n    \"\"\"\n    Implement the RELU function.\n\n    Arguments:\n    Z -- Output of the linear layer, of any shape\n\n    Returns:\n    A -- Post-activation parameter, of the same shape as Z\n    cache -- a python dictionary containing \"A\" ; stored for computing the backward pass efficiently\n    \"\"\"\n    \n    A = np.maximum(0,Z)\n    \n    assert(A.shape == Z.shape)\n    \n    cache = Z \n    return A, cache\n\n\ndef relu_backward(dA, cache):\n    \"\"\"\n    Implement the backward propagation for a single RELU unit.\n\n    Arguments:\n    dA -- post-activation gradient, of any shape\n    cache -- 'Z' where we store for computing backward propagation efficiently\n\n    Returns:\n    dZ -- Gradient of the cost with respect to Z\n    \"\"\"\n    \n    Z = cache\n    dZ = np.array(dA, copy=True) # just converting dz to a correct object.\n    \n    # When z <= 0, you should set dz to 0 as well. \n    dZ[Z <= 0] = 0\n    \n    assert (dZ.shape == Z.shape)\n    \n    return dZ\n\ndef sigmoid_backward(dA, cache):\n    \"\"\"\n    Implement the backward propagation for a single SIGMOID unit.\n\n    Arguments:\n    dA -- post-activation gradient, of any shape\n    cache -- 'Z' where we store for computing backward propagation efficiently\n\n    Returns:\n    dZ -- Gradient of the cost with respect to Z\n    \"\"\"\n    \n    Z = cache\n    \n    s = 1/(1+np.exp(-Z))\n    dZ = dA * s * (1-s)\n    \n    assert (dZ.shape == Z.shape)\n    \n    return dZ\ndef softmax(Z):\n    \"\"\"Calculates the softmax for each row of the input x.\n\n    Your code should work for a row vector and also for matrices of shape (n, m).\n\n    Argument:\n    x -- A numpy matrix of shape (n,m)\n\n    Returns:\n    s -- A numpy matrix equal to the softmax of x, of shape (n,m)\n    \"\"\"\n    \n    ### START CODE HERE ### (≈ 3 lines of code)\n    # Apply exp() element-wise to x. Use np.exp(...).\n    mx = np.amax(Z,axis=0,keepdims = True)\n    Z_exp = np.exp(Z-mx)\n\n    # Create a vector x_sum that sums each column of x_exp. Use np.sum(..., axis = 0, keepdims = True).\n    Z_sum = np.sum(Z_exp,axis=0,keepdims=True)\n    \n    # Compute softmax(x) by dividing x_exp by x_sum. It should automatically use numpy broadcasting.\n    A = Z_exp/Z_sum\n    cache = Z\n\n    ### END CODE HERE ###\n    \n    return A, cache\n\ndef initialize_parameters_deep(layer_dims):\n    \"\"\"\n    Arguments:\n    layer_dims -- python array (list) containing the dimensions of each layer in our network\n    \n    Returns:\n    parameters -- python dictionary containing your parameters \"W1\", \"b1\", ..., \"WL\", \"bL\":\n                    Wl -- weight matrix of shape (layer_dims[l], layer_dims[l-1])\n                    bl -- bias vector of shape (layer_dims[l], 1)\n    \"\"\"\n    \n    #np.random.seed(3)\n    parameters = {}\n    L = len(layer_dims)            # number of layers in the network\n\n    for l in range(1, L):\n        ### START CODE HERE ### (≈ 2 lines of code)\n        parameters['W' + str(l)] = np.random.randn(layer_dims[l],layer_dims[l-1])*0.01\n        parameters['b' + str(l)] = np.zeros((layer_dims[l], 1))*0.01\n        ### END CODE HERE ###\n        \n        assert(parameters['W' + str(l)].shape == (layer_dims[l], layer_dims[l-1]))\n        assert(parameters['b' + str(l)].shape == (layer_dims[l], 1))\n\n        \n    return parameters\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"2094e0a0-52f3-4694-b78f-56022432a8d0","_uuid":"a121bfba41e67441c5646f43bd2c984e22ebe337","trusted":true},"cell_type":"code","source":"def linear_forward(A, W, b):\n    \"\"\"\n    Implement the linear part of a layer's forward propagation.\n\n    Arguments:\n    A -- activations from previous layer (or input data): (size of previous layer, number of examples)\n    W -- weights matrix: numpy array of shape (size of current layer, size of previous layer)\n    b -- bias vector, numpy array of shape (size of the current layer, 1)\n\n    Returns:\n    Z -- the input of the activation function, also called pre-activation parameter \n    cache -- a python dictionary containing \"A\", \"W\" and \"b\" ; stored for computing the backward pass efficiently\n    \"\"\"\n    \n    ### START CODE HERE ### (≈ 1 line of code)\n    Z = np.dot(W,A)+b\n    ### END CODE HERE ###\n    \n    assert(Z.shape == (W.shape[0], A.shape[1]))\n    cache = (A, W, b)\n    \n    return Z, cache","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"23d74594-a1bf-490a-9898-28935e2f9a12","_uuid":"db43e3ea505825033d5191eafd46ebdefd3eb071","trusted":true},"cell_type":"code","source":"def linear_activation_forward(A_prev, W, b, activation):\n    \"\"\"\n    Implement the forward propagation for the LINEAR->ACTIVATION layer\n\n    Arguments:\n    A_prev -- activations from previous layer (or input data): (size of previous layer, number of examples)\n    W -- weights matrix: numpy array of shape (size of current layer, size of previous layer)\n    b -- bias vector, numpy array of shape (size of the current layer, 1)\n    activation -- the activation to be used in this layer, stored as a text string: \"sigmoid\" or \"relu\"\n\n    Returns:\n    A -- the output of the activation function, also called the post-activation value \n    cache -- a python dictionary containing \"linear_cache\" and \"activation_cache\";\n             stored for computing the backward pass efficiently\n    \"\"\"\n    \n    if activation == \"sigmoid\":\n        # Inputs: \"A_prev, W, b\". Outputs: \"A, activation_cache\".\n        ### START CODE HERE ### (≈ 2 lines of code)\n        Z, linear_cache = linear_forward(A_prev, W, b)\n        A, activation_cache = sigmoid(Z)\n        ### END CODE HERE ###\n    \n    elif activation == \"relu\":\n        # Inputs: \"A_prev, W, b\". Outputs: \"A, activation_cache\".\n        ### START CODE HERE ### (≈ 2 lines of code)\n        Z, linear_cache = linear_forward(A_prev, W, b)\n        A, activation_cache = relu(Z)\n        ### END CODE HERE ###\n        \n    elif activation == \"softmax\":\n        # Inputs: \"A_prev, W, b\". Outputs: \"A, activation_cache\".\n        ### START CODE HERE ### (≈ 2 lines of code)\n        Z, linear_cache = linear_forward(A_prev, W, b)\n        A, activation_cache = softmax(Z)\n        ### END CODE HERE ###\n    \n    assert (A.shape == (W.shape[0], A_prev.shape[1]))\n    cache = (linear_cache, activation_cache)\n\n    return A, cache","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"c4b6a7b0-478f-496e-a36c-a0977fe9b34a","_uuid":"fdc0ac98273aaf529fdf4917242177fec16e520b","trusted":true},"cell_type":"code","source":"def L_model_forward(X, parameters):\n    \"\"\"\n    Implement forward propagation for the [LINEAR->RELU]*(L-1)->LINEAR->SIGMOID computation\n    \n    Arguments:\n    X -- data, numpy array of shape (input size, number of examples)\n    parameters -- output of initialize_parameters_deep()\n    \n    Returns:\n    AL -- last post-activation value\n    caches -- list of caches containing:\n                every cache of linear_relu_forward() (there are L-1 of them, indexed from 0 to L-2)\n                the cache of linear_sigmoid_forward() (there is one, indexed L-1)\n    \"\"\"\n\n    caches = []\n    A = X\n    L = len(parameters) // 2                  # number of layers in the neural network\n    \n    # Implement [LINEAR -> RELU]*(L-1). Add \"cache\" to the \"caches\" list.\n    for l in range(1, L):\n        A_prev = A \n        A, cache = linear_activation_forward(A_prev, parameters['W' + str(l)], parameters['b' + str(l)], activation = \"relu\")\n        caches.append(cache)\n    \n    # Implement LINEAR -> SIGMOID. Add \"cache\" to the \"caches\" list.\n    AL, cache = linear_activation_forward(A, parameters['W' + str(L)], parameters['b' + str(L)], activation = \"softmax\")\n    caches.append(cache)\n    \n    assert(AL.shape == (120,X.shape[1]))\n            \n    return AL, caches","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"09e367b7-72c1-48ae-84bd-aec28de096bf","_uuid":"160246183dd489fb9eab86a397668a73301ecb19","trusted":true},"cell_type":"code","source":"def compute_cost(AL, Y):\n    \"\"\"\n    Implement the cost function defined by equation (7).\n\n    Arguments:\n    AL -- probability vector corresponding to your label predictions, shape (1, number of examples)\n    Y -- true \"label\" vector (for example: containing 0 if non-cat, 1 if cat), shape (1, number of examples)\n\n    Returns:\n    cost -- cross-entropy cost\n    \"\"\"\n    \n    m = Y.shape[1]\n\n    # Compute loss from aL and y.\n    \n    cost= (1./m) * np.sum(-np.sum(Y*np.log(AL),axis=0,keepdims=True),axis=1,keepdims=True)\n    cost = np.squeeze(cost)      # To make sure your cost's shape is what we expect (e.g. this turns [[17]] into 17).\n    assert(cost.shape == ())\n    \n    return cost","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"54aeedb1-e10b-40ff-adae-35ca12067e16","_uuid":"507b8832dde4cbe9a6c33aa93151c094f51961f1","trusted":true},"cell_type":"code","source":"def linear_backward(dZ, cache):\n    \"\"\"\n    Implement the linear portion of backward propagation for a single layer (layer l)\n\n    Arguments:\n    dZ -- Gradient of the cost with respect to the linear output (of current layer l)\n    cache -- tuple of values (A_prev, W, b) coming from the forward propagation in the current layer\n\n    Returns:\n    dA_prev -- Gradient of the cost with respect to the activation (of the previous layer l-1), same shape as A_prev\n    dW -- Gradient of the cost with respect to W (current layer l), same shape as W\n    db -- Gradient of the cost with respect to b (current layer l), same shape as b\n    \"\"\"\n    A_prev, W, b = cache\n    m = A_prev.shape[1]\n\n    dW = 1./m * np.dot(dZ,A_prev.T)\n    db = 1./m * np.sum(dZ, axis = 1, keepdims = True)\n    dA_prev = np.dot(W.T,dZ)\n    \n    assert (dA_prev.shape == A_prev.shape)\n    assert (dW.shape == W.shape)\n    assert (db.shape == b.shape)\n    \n    return dA_prev, dW, db\n\ndef linear_activation_backward(dA, cache, activation):\n    \"\"\"\n    Implement the backward propagation for the LINEAR->ACTIVATION layer.\n    \n    Arguments:\n    dA -- post-activation gradient for current layer l \n    cache -- tuple of values (linear_cache, activation_cache) we store for computing backward propagation efficiently\n    activation -- the activation to be used in this layer, stored as a text string: \"sigmoid\" or \"relu\"\n    \n    Returns:\n    dA_prev -- Gradient of the cost with respect to the activation (of the previous layer l-1), same shape as A_prev\n    dW -- Gradient of the cost with respect to W (current layer l), same shape as W\n    db -- Gradient of the cost with respect to b (current layer l), same shape as b\n    \"\"\"\n    linear_cache, activation_cache = cache\n    \n    if activation == \"relu\":\n        dZ = relu_backward(dA, activation_cache)\n        dA_prev, dW, db = linear_backward(dZ, linear_cache)\n        \n    elif activation == \"sigmoid\":\n        dZ = sigmoid_backward(dA, activation_cache)\n        dA_prev, dW, db = linear_backward(dZ, linear_cache)\n    \n    return dA_prev, dW, db\n\ndef L_model_backward(AL, Y, caches):\n    \"\"\"\n    Implement the backward propagation for the [LINEAR->RELU] * (L-1) -> LINEAR -> SIGMOID group\n    \n    Arguments:\n    AL -- probability vector, output of the forward propagation (L_model_forward())\n    Y -- true \"label\" vector (containing 0 if non-cat, 1 if cat)\n    caches -- list of caches containing:\n                every cache of linear_activation_forward() with \"relu\" (there are (L-1) or them, indexes from 0 to L-2)\n                the cache of linear_activation_forward() with \"sigmoid\" (there is one, index L-1)\n    \n    Returns:\n    grads -- A dictionary with the gradients\n             grads[\"dA\" + str(l)] = ... \n             grads[\"dW\" + str(l)] = ...\n             grads[\"db\" + str(l)] = ... \n    \"\"\"\n    grads = {}\n    L = len(caches) # the number of layers\n    m = AL.shape[1]\n    Y = Y.reshape(AL.shape) # after this line, Y is the same shape as AL\n    \n    # Initializing the backpropagation\n    dAL = - (np.divide(Y, AL)) \n    \n    # Lth layer (softmax -> LINEAR) gradients. Inputs: \"AL, Y, caches\". Outputs: \"grads[\"dAL\"], grads[\"dWL\"], grads[\"dbL\"]\n    current_cache = caches[L-1]\n    linear_cache1, activation_cache1 = current_cache\n    dZ = AL-Y     # AL-Y\n    grads[\"dA\" + str(L)], grads[\"dW\" + str(L)], grads[\"db\" + str(L)] = linear_backward(dZ, linear_cache1)\n    \n    for l in reversed(range(L-1)):\n        # lth layer: (RELU -> LINEAR) gradients.\n        current_cache = caches[l]\n        dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads[\"dA\" + str(l + 2)], current_cache, activation = \"relu\")\n        grads[\"dA\" + str(l + 1)] = dA_prev_temp\n        grads[\"dW\" + str(l + 1)] = dW_temp\n        grads[\"db\" + str(l + 1)] = db_temp\n\n    return grads\n\ndef update_parameters(parameters, grads, learning_rate):\n    \"\"\"\n    Update parameters using gradient descent\n    \n    Arguments:\n    parameters -- python dictionary containing your parameters \n    grads -- python dictionary containing your gradients, output of L_model_backward\n    \n    Returns:\n    parameters -- python dictionary containing your updated parameters \n                  parameters[\"W\" + str(l)] = ... \n                  parameters[\"b\" + str(l)] = ...\n    \"\"\"\n    \n    L = len(parameters) // 2 # number of layers in the neural network\n\n    # Update rule for each parameter. Use a for loop.\n    for l in range(L):\n        parameters[\"W\" + str(l+1)] = parameters[\"W\" + str(l+1)] - learning_rate * grads[\"dW\" + str(l+1)]\n        parameters[\"b\" + str(l+1)] = parameters[\"b\" + str(l+1)] - learning_rate * grads[\"db\" + str(l+1)]\n        \n    return parameters","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"f4a4bb88-eb5b-4195-9ae9-b9d1197ca0fa","_uuid":"3b5931e643ee9b6062d6b67d3fc577bbc5a648ad","trusted":true},"cell_type":"code","source":"def L_layer_model(X, Y, layers_dims, learning_rate = 0.1, num_iterations = 1000, print_cost=False):#lr was 0.009\n    \"\"\"\n    Implements a L-layer neural network: [LINEAR->RELU]*(L-1)->LINEAR->SIGMOID.\n    \n    Arguments:\n    X -- data, numpy array of shape (number of examples, num_px * num_px * 3)\n    Y -- true \"label\" vector (containing 0 if cat, 1 if non-cat), of shape (1, number of examples)\n    layers_dims -- list containing the input size and each layer size, of length (number of layers + 1).\n    learning_rate -- learning rate of the gradient descent update rule\n    num_iterations -- number of iterations of the optimization loop\n    print_cost -- if True, it prints the cost every 100 steps\n    \n    Returns:\n    parameters -- parameters learnt by the model. They can then be used to predict.\n    \"\"\"\n\n    #np.random.seed(1)\n    costs = []                         # keep track of cost\n    \n    # Parameters initialization.\n    ### START CODE HERE ###\n    parameters = initialize_parameters_deep(layers_dims)\n    ### END CODE HERE ###\n    \n    # Loop (gradient descent)\n    for i in range(0, num_iterations):\n\n        # Forward propagation: [LINEAR -> RELU]*(L-1) -> LINEAR -> SIGMOID.\n        ### START CODE HERE ### (≈ 1 line of code)\n        AL, caches = L_model_forward(X, parameters)\n        ### END CODE HERE ###\n        \n        # Compute cost.\n        ### START CODE HERE ### (≈ 1 line of code)\n        cost = compute_cost(AL, Y)\n        ### END CODE HERE ###\n    \n        # Backward propagation.\n        ### START CODE HERE ### (≈ 1 line of code)\n        grads = L_model_backward(AL, Y, caches)\n        ### END CODE HERE ###\n \n        # Update parameters.\n        ### START CODE HERE ### (≈ 1 line of code)\n        parameters = update_parameters(parameters, grads, learning_rate)\n        ### END CODE HERE ###\n                \n        # Print the cost every 100 training example\n        if print_cost and i % 2 == 0:\n            print (\"Cost after iteration %i: %f\" %(i, cost))\n        if print_cost and i % 2 == 0:\n            costs.append(cost)\n            \n    # plot the cost\n    plt.plot(np.squeeze(costs))\n    plt.ylabel('cost')\n    plt.xlabel('iterations (per tens)')\n    plt.title(\"Learning rate =\" + str(learning_rate))\n    plt.show()\n    \n    return parameters","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"abf08607-1882-415f-b5a9-7041c85ed20b","_uuid":"ffa6931f2d83b807e83e57182c435e208e8f836c","trusted":true},"cell_type":"code","source":"def predict(X, y, parameters):\n    \"\"\"\n    This function is used to predict the results of a  L-layer neural network.\n    \n    Arguments:\n    X -- data set of examples you would like to label\n    parameters -- parameters of the trained model\n    \n    Returns:\n    p -- predictions for the given dataset X\n    \"\"\"\n    \n    m = X.shape[1]\n    n = len(parameters) // 2 # number of layers in the neural network\n    \n    # Forward propagation\n    probas, caches = L_model_forward(X, parameters)\n\n    pred=probas.T\n    p=(probas == probas.max(axis=0))\n       \n    print(\"Accuracy: \"  + str((np.sum(np.sum(p * y,axis=0,keepdims=True),axis=1,keepdims=True))/m))\n        \n    return pred","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"8b1abb76-bbd3-4e1f-8c89-d2135a788dca","_uuid":"4a54d35b99180fc607c93f58aca70362e4aa4571","trusted":true},"cell_type":"code","source":"def predictT(X , parameters):\n    \"\"\"\n    This function is used to predict the results of a  L-layer neural network.\n    \n    Arguments:\n    X -- data set of examples you would like to label\n    parameters -- parameters of the trained model\n    \n    Returns:\n    p -- predictions for the given dataset X\n    \"\"\"\n    \n    m = X.shape[1]\n    n = len(parameters) // 2 # number of layers in the neural network\n    \n    # Forward propagation\n    probas, caches = L_model_forward(X, parameters)\n\n    pred=probas.T\n        \n    return pred","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"d4b80ee6-0059-424b-9c74-feb3ebdbd3ba","_uuid":"b37ca67caa0b0113a17234f729b4fc4e72753ca4","trusted":true},"cell_type":"code","source":"layers_dims = [30000,250 ,200,200,120] #5-layer model #first index is number of pixel (input layer)\nparameters = L_layer_model(train_x, train_Y, layers_dims, num_iterations = 1500, print_cost = True)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"cbaf258a-36d9-4791-910f-f9802f6ab0e5","_uuid":"ed759615cb1557ead521e6c8e727258134076158","trusted":false},"cell_type":"code","source":"pred_train = predict(train_x, train_Y, parameters)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"3774833b-6e2d-4dcf-b998-283bacd97329","_uuid":"cbba303c045138312e3a0b9038bbba57edff23b3","trusted":false},"cell_type":"code","source":"pred_test = predict(valid_x, valid_Y, parameters)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"f1800ee2-d720-4177-8337-6cad65e7590c","_uuid":"b772c7c5b50411564ade06aa453e19ee2602f27c","trusted":false},"cell_type":"code","source":"predictions =predictT(test_x , parameters)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"2f28ee1a-1fd5-4d3f-ae1c-ce095ba685fc","_uuid":"cb23085ee68489af7c715e1014f05a95c56097d4","trusted":false},"cell_type":"code","source":"# save prediction to csv\nclasses = csv_test.columns.values[1:]\nframe = pd.DataFrame(predictions, index=csv_test['id'].tolist(), columns=classes)\nframe.to_csv(\"predicted_test2.csv\", index_label='id')","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_cell_guid":"5bd4aeb0-b3f0-43de-9a4e-a87c17b03b4f","_uuid":"2d22e564d00d33375bb70f6cc5c416e6ca67ef58","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}