{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<a href=\"https://colab.research.google.com/github/lakshyauttrani/learning/blob/main/NN_logistic_classifier.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>","metadata":{"id":"view-in-github"}},{"cell_type":"code","source":"import os\nimport zipfile\nimport shutil\nfrom tqdm import tqdm","metadata":{"id":"sJEBnGc1rbEs","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extrair os ZIPs\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\", \"r\") as unzip:\n    unzip.extractall(\".\")\n\n    \nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\", \"r\") as unzip:\n    unzip.extractall(\".\")","metadata":{"id":"q73xb4iYrQJP","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(\"my_train/dogs\", exist_ok=True)\nos.makedirs(\"my_train/cats\", exist_ok=True)","metadata":{"id":"AFFB_BDvtKmg","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root = \"train\"\nimgs = os.walk(root).__next__()[2]\nfolders = {\n    \"cat\": \"my_train/cats\",\n    \"dog\": \"my_train/dogs\"\n}\n\nfor img in tqdm(imgs):\n    label = img.split(\".\")[0]\n    \n    old_path = os.path.join(root, img)\n    new_path = os.path.join(folders[label], img)\n    \n    shutil.move(old_path, new_path)","metadata":{"id":"id8BOS2HuL6c","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2\nimport os\nimport pickle\nfrom sklearn.model_selection import train_test_split\nimport sklearn.linear_model\nimport numpy as np\n# from dnn_utils_v2 import sigmoid, sigmoid_backward, relu, relu_backward","metadata":{"id":"_ODbx7qJyOCz","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = ['cats', 'dogs']\nimg_size = 128\ndef get_data(data_dir):\n    data = []\n    counter = 0\n    for label in labels:\n      path = os.path.join(data_dir, label)\n      class_num = labels.index(label)\n      for img in os.listdir(path):\n          try:\n              img_arr = cv2.imread(os.path.join(path, img))[...,::-1] #convert BGR to RGB format\n              resized_arr = cv2.resize(img_arr, (img_size, img_size)) # Reshaping images to preferred size\n              data.append([resized_arr, class_num])\n              counter+= 1\n              # print(counter) # uncomment this if you want to know how many images have been loaded till now.\n              if counter >4000: #since the dataset is huge i just made it so it takes first 3000 entries from each category\n                counter=0\n                break;\n          except Exception as e:\n              print(e)\n    return np.array(data)","metadata":{"id":"Oj3bnGq9yQvA","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_test_data(data_dir):\n  data = []\n  path = os.path.join(data_dir)\n  counter = 0\n  for img in os.listdir(path):\n      try:\n          img_arr = cv2.imread(os.path.join(path, img))[...,::-1] #convert BGR to RGB format\n          resized_arr = cv2.resize(img_arr, (img_size, img_size)) # Reshaping images to preferred size\n          data.append([resized_arr])\n          counter+= 1\n          print(counter)\n          if counter >300:\n            break;\n      except Exception as e:\n          print(e)\n  return np.array(data)","metadata":{"id":"XDtNzAIDqB5m","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Now we can easily fetch our train and validation data.\ntrain = get_data('./my_train')\n# # test = get_test_data('/content/drive/MyDrive/Datasets/CatsVsDogs/test1')","metadata":{"id":"9QpagD8Wygns","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (5,5))\nplt.imshow(train[2500][0])\nplt.title(labels[train[0][1]])","metadata":{"id":"TyPoUxL3zONi","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (5,5))\nplt.imshow(train[-3001][0])\nplt.title(labels[train[-1][1]])","metadata":{"id":"ndf81fwhSld1","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Write to a pickled dataset.","metadata":{"id":"gc6tT-Cj7waC"}},{"cell_type":"code","source":"# this one holds the pickled data with image size as 224\n# os.chdir('/content/drive/MyDrive/Datasets/CatsVsDogs')\nwith open ('train.pkl','wb') as f:\n  pickle.dump(train, f)\n\n# pickling a new dataset with 64*64 image size due to unavailability of RAM\n# os.chdir('./')\n# with open ('train2.pkl','wb') as f:\n#   pickle.dump(train, f)\n\n# with open ('val.pkl','wb') as f:#\n#   pickle.dump(val, f)","metadata":{"id":"tG5ElBu8WAyx","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Read from the pickled dataset.","metadata":{"id":"CYwlpxGS7z3j"}},{"cell_type":"code","source":"os.chdir('./')\nwith open ('train.pkl','rb') as f:\n  train=pickle.load(f)\n  # print(train.shape)","metadata":{"id":"xt4bNh_tdny0","trusted":true},"execution_count":null,"outputs":[]},{"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","metadata":{"id":"XOngfQXn0JSe","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def relu(Z):\n    \"\"\"\n    Implement the RELU function.\n    Arguments:\n    Z -- Output of the linear layer, of any shape\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","metadata":{"id":"znvQPqWn0qwh","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def relu_backward(dA, cache):\n    \"\"\"\n    Implement the backward propagation for a single RELU unit.\n    Arguments:\n    dA -- post-activation gradient, of any shape\n    cache -- 'Z' where we store for computing backward propagation efficiently\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","metadata":{"id":"Xytd8eXo2y1u","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sigmoid_backward(dA, cache):\n    \"\"\"\n    Implement the backward propagation for a single SIGMOID unit.\n    Arguments:\n    dA -- post-activation gradient, of any shape\n    cache -- 'Z' where we store for computing backward propagation efficiently\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","metadata":{"id":"E2USDbK021mb","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef initialize_parameters(n_x, n_h, n_y):\n    \"\"\"\n    Argument:\n    n_x -- size of the input layer\n    n_h -- size of the hidden layer\n    n_y -- size of the output layer\n    \n    Returns:\n    parameters -- python dictionary containing your parameters:\n                    W1 -- weight matrix of shape (n_h, n_x)\n                    b1 -- bias vector of shape (n_h, 1)\n                    W2 -- weight matrix of shape (n_y, n_h)\n                    b2 -- bias vector of shape (n_y, 1)\n    \"\"\"\n    \n    np.random.seed(1)\n    \n    W1 = np.random.randn(n_h, n_x)*0.01\n    b1 = np.zeros((n_h, 1))\n    W2 = np.random.randn(n_y, n_h)*0.01\n    b2 = np.zeros((n_y, 1))\n    \n    assert(W1.shape == (n_h, n_x))\n    assert(b1.shape == (n_h, 1))\n    assert(W2.shape == (n_y, n_h))\n    assert(b2.shape == (n_y, 1))\n    \n    parameters = {\"W1\": W1,\n                  \"b1\": b1,\n                  \"W2\": W2,\n                  \"b2\": b2}\n    \n    return parameters    ","metadata":{"id":"xSmmv-u5p4ZW","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"parameters = initialize_parameters(3,2,1)\nprint(\"W1 = \" + str(parameters[\"W1\"]))\nprint(\"b1 = \" + str(parameters[\"b1\"]))\nprint(\"W2 = \" + str(parameters[\"W2\"]))\nprint(\"b2 = \" + str(parameters[\"b2\"]))","metadata":{"id":"OAc0OWROp-5O","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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        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))\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","metadata":{"id":"6yvsz9EsqUT7","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"parameters = initialize_parameters_deep([5,4,3])\nprint(\"W1 = \" + str(parameters[\"W1\"]))\nprint(\"b1 = \" + str(parameters[\"b1\"]))\nprint(\"W2 = \" + str(parameters[\"W2\"]))\nprint(\"b2 = \" + str(parameters[\"b2\"]))","metadata":{"id":"HF6eZbZCqZof","trusted":true},"execution_count":null,"outputs":[]},{"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 tuple containing \"A\", \"W\" and \"b\" ; stored for computing the backward pass efficiently\n    \"\"\"\n    \n    Z = np.dot(W, A)+b\n\n    assert(Z.shape == (W.shape[0], A.shape[1]))\n    cache = (A, W, b)\n    \n    return Z, cache","metadata":{"id":"U2s3ZwuIq4L4","trusted":true},"execution_count":null,"outputs":[]},{"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 tuple 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        Z, linear_cache = linear_forward(A_prev, W, b)\n        A, activation_cache = sigmoid(Z)\n    \n    elif activation == \"relu\":\n        # Inputs: \"A_prev, W, b\". Outputs: \"A, activation_cache\".\n        Z, linear_cache = linear_forward(A_prev, W, b)\n        A, activation_cache = relu(Z)\n    \n    assert (A.shape == (W.shape[0], A_prev.shape[1]))\n    cache = (linear_cache, activation_cache)\n\n    return A, cache","metadata":{"id":"kNWTqx8prRYu","trusted":true},"execution_count":null,"outputs":[]},{"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_activation_forward() (there are L-1 of them, indexed from 0 to 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)], \"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)], \"sigmoid\")\n    caches.append(cache)\n    \n    assert(AL.shape == (1,X.shape[1]))\n            \n    return AL, caches","metadata":{"id":"Zx52BmKOrZIl","trusted":true},"execution_count":null,"outputs":[]},{"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    cost = (-1/m)*np.sum(Y*np.log(AL)+(1-Y)*np.log(1-AL))\n    \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","metadata":{"id":"TFWDF2acryOx","trusted":true},"execution_count":null,"outputs":[]},{"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","metadata":{"id":"mX2FGkULuyqg","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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","metadata":{"id":"GKEoO2y5u48J","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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\" (it's caches[l], for l in range(L-1) i.e l = 0...L-2)\n                the cache of linear_activation_forward() with \"sigmoid\" (it's caches[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) - np.divide(1 - Y, 1 - AL))\n    \n    # Lth layer (SIGMOID -> LINEAR) gradients. Inputs: \"dAL, current_cache\". Outputs: \"grads[\"dAL-1\"], grads[\"dWL\"], grads[\"dbL\"]\n    current_cache = caches[L-1]\n    grads[\"dA\" + str(L-1)], grads[\"dW\" + str(L)], grads[\"db\" + str(L)] = linear_activation_backward(dAL, current_cache, \"sigmoid\")\n    \n    # Loop from l=L-2 to l=0\n    for l in reversed(range(L-1)):\n        # lth layer: (RELU -> LINEAR) gradients.\n        # Inputs: \"grads[\"dA\" + str(l + 1)], current_cache\". Outputs: \"grads[\"dA\" + str(l)] , grads[\"dW\" + str(l + 1)] , grads[\"db\" + str(l + 1)] \n        current_cache = caches[l]\n        dA_prev_temp, dW_temp, db_temp = linear_activation_backward(grads[\"dA\"+str(l+1)], current_cache, \"relu\")\n        grads[\"dA\" + str(l)] = dA_prev_temp\n        grads[\"dW\" + str(l + 1)] = dW_temp\n        grads[\"db\" + str(l + 1)] = db_temp\n\n    return grads","metadata":{"id":"HSbVGItKu-Va","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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    return parameters","metadata":{"id":"xbkT1JpRvG2x","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=[]\ny_train=[]\nfor feature, label in train:\n  x_train.append(feature)\n  y_train.append(label)\n\nx_train = np.array(x_train)\ny_train = np.array(y_train).reshape(len(y_train),1)","metadata":{"id":"y8eQb92VwGIs","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split into train test sets\ntrain_x_orig, test_x_orig, train_y, test_y = train_test_split(x_train, y_train, test_size=0.33)\ntrain_y, test_y = train_y.T, test_y.T\nprint(train_x_orig.shape, test_x_orig.shape, train_y.shape, test_y.shape)","metadata":{"id":"qUjyBJhxvcY7","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(unique, counts) = np.unique(test_y, return_counts=True)\nfrequencies = np.asarray((unique, counts)).T\nfrequencies","metadata":{"id":"F0EJuaWqbOaS","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Explore your dataset \nm_train = train_x_orig.shape[0]\nnum_px = train_x_orig.shape[1]\nm_test = test_x_orig.shape[0]\n\nprint (\"Number of training examples: \" + str(m_train))\nprint (\"Number of testing examples: \" + str(m_test))\nprint (\"Each image is of size: (\" + str(num_px) + \", \" + str(num_px) + \", 3)\")\nprint (\"train_x_orig shape: \" + str(train_x_orig.shape))\nprint (\"train_y shape: \" + str(train_y.shape))\nprint (\"test_x_orig shape: \" + str(test_x_orig.shape))\nprint (\"test_y shape: \" + str(test_y.shape))","metadata":{"id":"WNbJBJV7vjU1","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape the training and test examples \ntrain_x_flatten = train_x_orig.reshape(train_x_orig.shape[0], -1).T   # The \"-1\" makes reshape flatten the remaining dimensions\ntest_x_flatten = test_x_orig.reshape(test_x_orig.shape[0], -1).T\n\n# Standardize data to have feature values between 0 and 1.\ntrain_x = train_x_flatten/255.\ntest_x = test_x_flatten/255.\n\nprint (\"train_x's shape: \" + str(train_x.shape))\nprint (\"test_x's shape: \" + str(test_x.shape))\n","metadata":{"id":"_n0hwCT3yXk5","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### CONSTANTS DEFINING THE MODEL ####\nn_x = 49152     # num_px * num_px * 3\nn_h = 7\nn_y = 1\nlayers_dims = (n_x, n_h, n_y)","metadata":{"id":"21YHL4oCymrC","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef two_layer_model(X, Y, layers_dims, learning_rate = 0.0075, num_iterations = 3000, print_cost=False):\n    \"\"\"\n    Implements a two-layer neural network: LINEAR->RELU->LINEAR->SIGMOID.\n    \n    Arguments:\n    X -- input data, of shape (n_x, number of examples)\n    Y -- true \"label\" vector (containing 1 if cat, 0 if non-cat), of shape (1, number of examples)\n    layers_dims -- dimensions of the layers (n_x, n_h, n_y)\n    num_iterations -- number of iterations of the optimization loop\n    learning_rate -- learning rate of the gradient descent update rule\n    print_cost -- If set to True, this will print the cost every 100 iterations \n    \n    Returns:\n    parameters -- a dictionary containing W1, W2, b1, and b2\n    \"\"\"\n    \n    np.random.seed(1)\n    grads = {}\n    costs = []                              # to keep track of the cost\n    m = X.shape[1]                           # number of examples\n    (n_x, n_h, n_y) = layers_dims\n    \n    # Initialize parameters dictionary, by calling one of the functions you'd previously implemented\n    parameters = initialize_parameters(n_x, n_h, n_y)\n    \n    # Get W1, b1, W2 and b2 from the dictionary parameters.\n    W1 = parameters[\"W1\"]\n    b1 = parameters[\"b1\"]\n    W2 = parameters[\"W2\"]\n    b2 = parameters[\"b2\"]\n    \n    # Loop (gradient descent)\n\n    for i in range(0, num_iterations):\n\n        # Forward propagation: LINEAR -> RELU -> LINEAR -> SIGMOID. Inputs: \"X, W1, b1, W2, b2\". Output: \"A1, cache1, A2, cache2\".\n        A1, cache1 = linear_activation_forward(X, W1, b1, \"relu\")\n        A2, cache2 = linear_activation_forward(A1, W2, b2, \"sigmoid\")\n        \n        # Compute cost\n        cost = compute_cost(A2, Y)\n        \n        # Initializing backward propagation\n        dA2 = - (np.divide(Y, A2) - np.divide(1 - Y, 1 - A2))\n        \n        # Backward propagation. Inputs: \"dA2, cache2, cache1\". Outputs: \"dA1, dW2, db2; also dA0 (not used), dW1, db1\".\n        dA1, dW2, db2 = linear_activation_backward(dA2, cache2, \"sigmoid\")\n        dA0, dW1, db1 = linear_activation_backward(dA1, cache1, \"relu\")\n        \n        # Set grads['dWl'] to dW1, grads['db1'] to db1, grads['dW2'] to dW2, grads['db2'] to db2\n        grads['dW1'] = dW1\n        grads['db1'] = db1\n        grads['dW2'] = dW2\n        grads['db2'] = db2\n        \n        # Update parameters.\n        parameters = update_parameters(parameters, grads, learning_rate)\n\n        # Retrieve W1, b1, W2, b2 from parameters\n        W1 = parameters[\"W1\"]\n        b1 = parameters[\"b1\"]\n        W2 = parameters[\"W2\"]\n        b2 = parameters[\"b2\"]\n        \n        # Print the cost every 100 training example\n        if print_cost and i % 100 == 0:\n            print(\"Cost after iteration {}: {}\".format(i, np.squeeze(cost)))\n        if print_cost and i % 100 == 0:\n            costs.append(cost)\n       \n    # plot the cost\n\n    plt.plot(np.squeeze(costs))\n    plt.ylabel('cost')\n    plt.xlabel('iterations (per hundreds)')\n    plt.title(\"Learning rate =\" + str(learning_rate))\n    plt.show()\n    \n    return parameters","metadata":{"id":"KLSKnrSVy179","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"parameters = two_layer_model(train_x, train_y, layers_dims = (n_x, n_h, n_y), num_iterations = 2500, print_cost=True)","metadata":{"id":"Np9t9LXGzDxn","trusted":true},"execution_count":null,"outputs":[]},{"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    p = np.zeros((1,m))\n    \n    # Forward propagation\n    probas, caches = L_model_forward(X, parameters)\n\n    \n    # convert probas to 0/1 predictions\n    for i in range(0, probas.shape[1]):\n        if probas[0,i] > 0.5:\n            p[0,i] = 1\n        else:\n            p[0,i] = 0\n    \n    #print results\n    #print (\"predictions: \" + str(p))\n    #print (\"true labels: \" + str(y))\n    print(\"Accuracy: \"  + str(np.sum((p == y)/m)))\n        \n    return p","metadata":{"id":"ZTVt_Uxz9IR9","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndef print_mislabeled_images(classes, X, y, p):\n    \"\"\"\n    Plots images where predictions and truth were different.\n    X -- dataset\n    y -- true labels\n    p -- predictions\n    \"\"\"\n    a = p + y\n    mislabeled_indices = np.asarray(np.where(a == 1))\n    plt.rcParams['figure.figsize'] = (40.0, 40.0) # set default size of plots\n    num_images = len(mislabeled_indices[0])\n    for i in range(num_images):\n        index = mislabeled_indices[1][i]\n        \n        plt.subplot(2, num_images, i + 1)\n        plt.imshow(X[:,index].reshape(64,64,3), interpolation='nearest')\n        plt.axis('off')\n        plt.title(\"Prediction: \" + classes[int(p[0,index])].decode(\"utf-8\") + \" \\n Class: \" + classes[y[0,index]].decode(\"utf-8\"))","metadata":{"id":"pQ3G0D7u9Led","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_train = predict(train_x, train_y, parameters)","metadata":{"id":"uj9PLPhm5Dsq","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_test = predict(test_x, test_y, parameters)","metadata":{"id":"askJXiIo5H_M","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"layers_dims = [49152, 20, 7, 5, 1] #  4-layer model","metadata":{"id":"V2w6dpcE5Ntd","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def L_layer_model(X, Y, layers_dims, learning_rate = 0.0075, num_iterations = 3000, 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 (num_px * num_px * 3, number of examples)\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. (≈ 1 line of code)\n    parameters = initialize_parameters_deep(layers_dims)\n    \n    # Loop (gradient descent)\n    for i in range(0, num_iterations):\n\n        # Forward propagation: [LINEAR -> RELU]*(L-1) -> LINEAR -> SIGMOID.\n        AL, caches = L_model_forward(X, parameters)\n        \n        # Compute cost.\n        cost = compute_cost(AL, Y)\n    \n        # Backward propagation.\n        grads = L_model_backward(AL, Y, caches)\n \n        # Update parameters.\n        parameters = update_parameters(parameters, grads, learning_rate)\n                \n        # Print the cost every 100 training example\n        if print_cost and i % 100 == 0:\n            print (\"Cost after iteration %i: %f\" %(i, cost))\n        if print_cost and i % 100 == 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 hundreds)')\n    plt.title(\"Learning rate =\" + str(learning_rate))\n    plt.show()\n    \n    return parameters","metadata":{"id":"ZvwqPT8r5XsW","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"parameters = L_layer_model(train_x, train_y, layers_dims, num_iterations = 2500, print_cost = True)","metadata":{"id":"9nLaESYI5aqt","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_train = predict(train_x, train_y, parameters)","metadata":{"id":"mOmskv-o5dML","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_test = predict(test_x, test_y, parameters)","metadata":{"id":"OUEkOEGZ5fGj","trusted":true},"execution_count":null,"outputs":[]}]}