├── BackpropagationBatch.py ├── BackpropagationStochastic.py ├── Keras-Classification └── keras-classification.py ├── LICENSE ├── Logistic-Regression-Numpy ├── README.md ├── images.zip └── log-regression.py ├── Notes ├── ConvNets (Pt 1).pdf ├── ConvNets (Pt 2).pdf ├── ConvNets (Pt 3).pdf ├── ConvNets (Pt 4).pdf ├── Hyperparameter Tuning (Pt 1).pdf ├── Hyperparameter Tuning (Pt 2).pdf ├── Neural Netowkrs and Deep Learning.pdf ├── README.md ├── Sequence Models (Pt 1).pdf ├── Sequence Models (Pt 2).pdf ├── Structuring ML Projects (Pt 1).pdf └── Structuring ML Projects (Pt 2).pdf └── README.md /BackpropagationBatch.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | S1 = 5 # neurons 5 | R = 1 # dimensions 6 | p = np.linspace(-2, 2, 100) # p between (-2,2) 7 | alpha = 0.02 # learning rate 8 | 9 | 10 | #Initialize weights and biases 11 | np.random.seed(5) 12 | w1 = np.random.uniform(-0.5, 0.5,(S1, R)) 13 | b1 = np.random.uniform(-0.5, 0.5,(S1, 1)) 14 | w2 = np.random.uniform(-0.5, 0.5,(1, S1)) 15 | b2 = np.random.uniform(-0.5, 0.5,(1, 1)) 16 | 17 | 18 | # Define approximation function 19 | def g(n): 20 | app = np.exp(-np.abs(n)) * np.sin(np.pi * n) 21 | return app 22 | 23 | 24 | # Define activation function 25 | def logsig(n): 26 | a = 1 / (1 + np.exp(-n)) 27 | return a 28 | 29 | 30 | # Define target function 31 | t = g(p) 32 | 33 | 34 | # Define forward propagation function 35 | def forward_propagation(p, w1, b1, w2, b2): 36 | n1 = np.dot(w1, p) + b1 37 | a1 = logsig(n1) 38 | 39 | n2 = np.dot(w2, a1) + b2 40 | a2 = n2 41 | return a1, n1, a2, n2 42 | 43 | 44 | # Define backpropagation to calculate sensitivities 45 | def backward(a1, w2, e): 46 | s2 = (-2 * 1) * e 47 | s1 = np.dot(np.diag(((1 - a1) * a1).flatten()), w2.T) * s2 48 | return s1, s2 49 | 50 | 51 | def NNF_Batch(p, w1, b1, w2, b2, alpha, t, epochs = 35000): 52 | MSE_batch = np.zeros(epochs) 53 | for j in range(epochs): 54 | e = np.zeros(len(p)) 55 | s1_sum = 0 56 | s2_sum = 0 57 | r2 = 0 58 | r1 = 0 59 | for i in range(len(p)): 60 | a1, n1, a2, n2 = forward_propagation(p[i], w1, b1, w2, b2) 61 | e[i] = t[i] - a2.reshape(-1).item() 62 | s1, s2 = backward(a1, w2, e[i]) 63 | s1_sum += s1 64 | s2_sum += s2 65 | r2 += np.dot(s2, a1.T) 66 | r1 += np.dot(s1, p[i]) 67 | w2 = w2 - alpha * r2 68 | w1 = w1 - alpha * r1 69 | b2 = b2 - alpha * s2_sum 70 | b1 = b1 - alpha * s1_sum 71 | MSE_batch[j] = np.dot(e.T, e) 72 | print(MSE_batch[j]) 73 | return e, MSE_batch, w1, w2, b2, b1 74 | 75 | 76 | e, MSE_batch, w1, w2, b2, b1 = NNF_Batch(p, w1, b1, w2, b2, alpha, t) 77 | 78 | 79 | #Plot original vs. network 80 | q = np.zeros((len(p))) 81 | for i in range(len(p)): 82 | _, _, q[i], _ = forward_propagation(p[i], w1, b1, w2, b2) 83 | 84 | plt.figure(1) 85 | plt.plot(p, t, label = 'Original function', color = 'green') 86 | plt.plot(p, q, label = 'Network function', color = 'blue') 87 | plt.legend() 88 | plt.show() 89 | 90 | 91 | #Plot squared error for epochs 92 | plt.figure(2) 93 | plt.plot(range(len(MSE_batch)), MSE_batch) 94 | plt.show() 95 | -------------------------------------------------------------------------------- /BackpropagationStochastic.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | S1 = 5 # neurons 5 | R = 1 # dimensions 6 | p = np.linspace(-2, 2, 100) # p between (-2,2) 7 | alpha = 0.02 # learning rate 8 | 9 | #Randomly initialize Weights and Biases 10 | np.random.seed(4) 11 | w1 = np.random.uniform(-0.5, 0.5, (S1, R)) 12 | b1 = np.random.uniform(-0.5, 0.5, (S1, 1)) 13 | w2 = np.random.uniform(-0.5, 0.5, (1, S1)) 14 | b2 = np.random.uniform(-0.5, 0.5, (1, 1)) 15 | 16 | 17 | # Define approximation function 18 | def g(n): 19 | app = np.exp(-np.abs(n)) * np.sin(np.pi * n) 20 | return app 21 | 22 | 23 | # Define activation function 24 | def logsig(n): 25 | a = 1 / (1 + np.exp(-n)) 26 | return a 27 | 28 | 29 | # Define target function 30 | t = g(p) 31 | 32 | 33 | # Define forward propagation function 34 | def forward_propagation(p, w1, b1, w2, b2): 35 | n1 = np.dot(w1, p) + b1 36 | a1 = logsig(n1) 37 | 38 | n2 = np.dot(w2, a1) + b2 39 | a2 = n2 40 | return a1, n1, a2, n2 41 | 42 | 43 | # Define backpropagation to calculate sensitivities 44 | def backward(a1, w2, e): 45 | s2 = (-2 * 1) * e 46 | s1 = np.dot(np.diag(((1 - a1) * a1).flatten()), w2.T) * s2 47 | return s1, s2 48 | 49 | 50 | # Update weights and biases 51 | def update(s1, s2, w1, w2, b1, b2, alpha, a1, p): 52 | w2_new = w2 - alpha * np.dot(s2, a1.T) 53 | w1_new = w1 - alpha * np.dot(s1, p) 54 | b2_new = b2 - alpha * s2 55 | b1_new = b1 - alpha * s1 56 | return w2_new, w1_new, b2_new, b1_new 57 | 58 | 59 | # Define neural network function 60 | def NNF(w1, b1, w2, b2, alpha, t, epochs = 15000): 61 | MSE = np.zeros(epochs) 62 | 63 | for i in range(epochs): 64 | e = np.zeros(len(p)) 65 | 66 | for j in range(len(p)): 67 | a1, n1, a2, n2 = forward_propagation(p[j], w1, b1, w2, b2) 68 | e[j] = t[j] - a2.reshape(-1).item() 69 | s1, s2 = backward(a1, w2, e[j]) 70 | w2, w1, b2, b1 = update(s1, s2, w1, w2, b1, b2, alpha, a1, p[j]) 71 | MSE[i] = np.dot(e.T, e) 72 | 73 | print(MSE[i]) 74 | return e, MSE, w1, w2, b2, b1 75 | 76 | 77 | # Call NNF 78 | e, MSE, w1, w2, b2, b1 = NNF(w1, b1, w2, b2, alpha, t) 79 | 80 | #Plot 81 | q = np.zeros((len(p))) 82 | for i in range(len(p)): 83 | _, _, q[i], _ = forward_propagation(p[i], w1, b1, w2, b2) 84 | 85 | plt.figure(1) 86 | plt.plot(p, t, label='Original function', color = 'green') 87 | plt.plot(p, q, label='Network function', color = 'blue') 88 | plt.legend() 89 | plt.show() 90 | 91 | #Plot squared error for epochs 92 | plt.figure(2) 93 | plt.loglog(range(len(MSE)), MSE) 94 | plt.show() 95 | -------------------------------------------------------------------------------- /Keras-Classification/keras-classification.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2, collections 3 | import os.path 4 | import zipfile 5 | from sklearn import preprocessing 6 | from sklearn.model_selection import train_test_split 7 | from keras.layers import Input, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D 8 | from keras.layers import AveragePooling2D, MaxPooling2D 9 | from keras.models import Model 10 | from keras.utils import * 11 | import keras.backend as K 12 | K.set_image_data_format('channels_last') 13 | import matplotlib.pyplot as plt 14 | 15 | 16 | cwd = os.getcwd() 17 | print('Current working directory:', cwd) 18 | print('') 19 | parent = os.path.abspath(os.path.join(cwd, os.pardir)) 20 | print('Parent directory is:', parent) 21 | 22 | #Extract zip into folder 23 | with zipfile.ZipFile(parent + '/Logistic-Regression-Numpy/images.zip', 'r') as zip_ref: 24 | zip_ref.extractall(cwd) 25 | 26 | # Define new path to work on the images 27 | dir = cwd + '/images/' 28 | 29 | #::------------------------------------- 30 | # Data-pre processing 31 | #::------------------------------------- 32 | 33 | # Read images 34 | print("Starting images and label pre-processing...") 35 | 36 | label_data = [] 37 | images_data = [] 38 | for subdir, dirs, files in os.walk(dir): 39 | for file in files: 40 | image = os.path.join(subdir, file) 41 | img = cv2.imread(image) 42 | # handle non-readable images 43 | if img is None: 44 | pass 45 | else: 46 | images_data.append(img) 47 | # read directory and append labels 48 | label = (subdir.split('images/')[1]) 49 | label_data.append(label) 50 | 51 | print("Images and labels successfully pre-processed!") 52 | 53 | # look at labels and images shape 54 | labels_data = np.array(label_data) 55 | print("Labels shape:", labels_data.shape) 56 | images_data = np.array(images_data) 57 | print("Images shape:", images_data.shape) 58 | 59 | # one-hot encoding: Convert text-based labels to int 60 | le = preprocessing.LabelEncoder() 61 | le.fit(labels_data) 62 | 63 | # confirm we have 2 unique classes 64 | print('Unique classes:',le.classes_) 65 | integer_labels = le.transform(labels_data) 66 | 67 | # count images 68 | image_count = len(list(images_data)) 69 | print("Number of images:", image_count) 70 | print("") 71 | 72 | # count number of images in every category 73 | print("Number of images in each class:") 74 | print(collections.Counter(labels_data).keys()) 75 | print(collections.Counter(labels_data).values()) 76 | 77 | 78 | # split train/test 79 | x_train, x_test, y_train, y_test = train_test_split(images_data, integer_labels, random_state = 42, test_size = 0.2, stratify = integer_labels) 80 | 81 | # Example of a picture 82 | index = 507 83 | plt.imshow(x_train[index]) 84 | plt.show() 85 | 86 | #::------------------------------------- 87 | # Shape checking 88 | #::------------------------------------- 89 | 90 | # Normalize image vectors 91 | X_train = x_train/255. 92 | X_test = x_test/255. 93 | 94 | # Reshape 95 | Y_train = y_train.T 96 | Y_test = y_test.T 97 | 98 | print ("number of training examples = " + str(X_train.shape[0])) 99 | print ("number of test examples = " + str(X_test.shape[0])) 100 | print ("X_train shape: " + str(X_train.shape)) 101 | print ("Y_train shape: " + str(Y_train.shape)) 102 | print ("X_test shape: " + str(X_test.shape)) 103 | print ("Y_test shape: " + str(Y_test.shape)) 104 | 105 | #::------------------------------------- 106 | # Build keras model 107 | #::------------------------------------- 108 | 109 | def kerasModel(input_shape): 110 | """ 111 | Implementation of the kerasModel. 112 | 113 | Arguments: 114 | input_shape -- shape of the images of the dataset 115 | """ 116 | 117 | # Define the input placeholder as a tensor with shape input_shape. 118 | X_input = Input(input_shape) 119 | 120 | # Zero-Padding: pads the border of X_input with zeroes 121 | X = ZeroPadding2D((3, 3))(X_input) 122 | 123 | # CONV -> BN -> RELU Block applied to X 124 | X = Conv2D(32, (7, 7), strides=(1, 1), name='conv0')(X) 125 | X = BatchNormalization(axis=3, name='bn0')(X) 126 | X = Activation('relu')(X) 127 | 128 | # MAXPOOL 129 | X = MaxPooling2D((2, 2), name='max_pool0')(X) 130 | 131 | X = Conv2D(32, (3, 3), strides=(1, 1), name='conv1')(X) 132 | X = BatchNormalization(axis=3, name='bn1')(X) 133 | X = Activation('relu')(X) 134 | 135 | # MAXPOOL 136 | X = MaxPooling2D((2, 2), name='max_pool1')(X) 137 | 138 | # FLATTEN X (convert it to a vector) + FULLYCONNECTED 139 | X = Flatten()(X) 140 | X = Dense(1, activation='sigmoid', name='fc')(X) 141 | 142 | # Create model. This creates your Keras model instance 143 | model = Model(inputs=X_input, outputs=X, name='kerasModel') 144 | 145 | return model 146 | 147 | #::------------------------------------- 148 | # Create / Compile / Train / Evaluate 149 | #::------------------------------------- 150 | 151 | # Step 1: create the model 152 | kerasModel = kerasModel(X_train.shape[1:]) 153 | 154 | # Step 2: compile the model 155 | kerasModel.compile('adam', 'binary_crossentropy', metrics=['accuracy']) 156 | 157 | # Step 3: train the model 158 | kerasModel.fit(x = X_train, y = Y_train, batch_size = 32, epochs = 40) 159 | 160 | # Step 4: evaluate the model 161 | preds = kerasModel.evaluate(x = X_test, y = Y_test) 162 | print() 163 | print ("Loss = " + str(preds[0])) 164 | print ("Test Accuracy = " + str(preds[1])) 165 | 166 | 167 | # Print the details of the layers in a table with the sizes of its inputs/outputs 168 | kerasModel.summary() 169 | 170 | # Plot the graph in a nice layout 171 | plot_model(kerasModel, to_file='kerasModel.png') 172 | # SVG(model_to_dot(kerasModel).create(prog='dot', format='svg')) 173 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Guillermina 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Logistic-Regression-Numpy/README.md: -------------------------------------------------------------------------------- 1 | ## Logistic Regression with Numpy 2 | 3 | This folder contains two files: 4 | - log-regression.py: Algortihm for the logistic regression model. The model was built using Numpy and contains helper functions for parameters initialization, minimizing cost function, and make predictions (on the test set) 5 | - images.zip: These are the images used in this project. Dowloaded from Kaggle at [https://www.kaggle.com/yo7oyo/lake-plane-binaryclass](https://www.kaggle.com/yo7oyo/lake-plane-binaryclass) 6 | -------------------------------------------------------------------------------- /Logistic-Regression-Numpy/images.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glosophy/Deep-Learning/5e534477ced5372ab822ac687d0d66b895b9f6cd/Logistic-Regression-Numpy/images.zip -------------------------------------------------------------------------------- /Logistic-Regression-Numpy/log-regression.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from sklearn.model_selection import train_test_split 4 | import cv2, os, pathlib 5 | from sklearn import preprocessing 6 | import collections 7 | 8 | 9 | cwd = os.getcwd() 10 | dir = cwd + '/images/' 11 | data_dir = pathlib.Path(dir) 12 | 13 | print(cwd) 14 | print(data_dir) 15 | 16 | # Read images 17 | print("Starting images and label pre-processing...") 18 | 19 | label_data = [] 20 | images_data = [] 21 | for subdir, dirs, files in os.walk(dir): 22 | for file in files: 23 | image = os.path.join(subdir, file) 24 | img = cv2.imread(image) 25 | # handle non-readable images 26 | if img is None: 27 | pass 28 | else: 29 | images_data.append(img) 30 | # read directory and append labels 31 | label = (subdir.split('images/')[1]) 32 | label_data.append(label) 33 | 34 | print("Images and labels successfully pre-processed!") 35 | 36 | # look at labels and images shape 37 | labels_data = np.array(label_data) 38 | print("Labels shape:", labels_data.shape) 39 | images_data = np.array(images_data) 40 | print("Images shape:", images_data.shape) 41 | 42 | # one-hot encoding: Convert text-based labels to int 43 | le = preprocessing.LabelEncoder() 44 | le.fit(labels_data) 45 | 46 | # confirm we have 2 unique classes 47 | print('Unique classes:',le.classes_) 48 | integer_labels = le.transform(labels_data) 49 | 50 | # count images 51 | image_count = len(list(images_data)) 52 | print("Number of images:", image_count) 53 | print("") 54 | 55 | # count number of images in every category 56 | print("Number of images in each class:") 57 | print(collections.Counter(labels_data).keys()) 58 | print(collections.Counter(labels_data).values()) 59 | 60 | 61 | # split train/test 62 | x_train, x_test, y_train, y_test = train_test_split(images_data, integer_labels, random_state = 42, test_size = 0.2, stratify = integer_labels) 63 | 64 | # Example of a picture 65 | index = 507 66 | plt.imshow(x_train[index]) 67 | plt.show() 68 | 69 | if y_train[index] == 1: 70 | print ("y = " + str(y_train[index]) + ", it's a lake picture.") 71 | else: 72 | print ("y = " + str(y_train[index]) + ", it's an airplane picture.") 73 | 74 | m_train = len(x_train) 75 | m_test = len(x_test) 76 | num_px = x_train.shape[1] 77 | 78 | print ("Number of training examples: m_train = " + str(m_train)) 79 | print ("Number of testing examples: m_test = " + str(m_test)) 80 | print ("Height/Width of each image: num_px = " + str(num_px)) 81 | print ("Each image is of size: (" + str(num_px) + ", " + str(num_px) + ", 3)") 82 | print ("x_train shape: " + str(x_train.shape)) 83 | print ("y_train shape: " + str(y_train.shape)) 84 | print ("x_test shape: " + str(x_test.shape)) 85 | print ("y_test shape: " + str(y_test.shape)) 86 | 87 | # Reshape the training and test examples 88 | x_train_flatten = x_train.reshape(x_train.shape[0], -1).T 89 | x_test_flatten = x_test.reshape(x_test.shape[0], -1).T 90 | 91 | print ("x_train_flatten shape: " + str(x_train_flatten.shape)) 92 | print ("y_train shape: " + str(y_train.shape)) 93 | print ("x_test_flatten shape: " + str(x_test_flatten.shape)) 94 | print ("y_test shape: " + str(y_test.shape)) 95 | 96 | # standarize images 97 | X_train = x_train_flatten/255. 98 | X_test = x_test_flatten/255. 99 | 100 | 101 | # HELPER FUNCTION: sigmoid 102 | def sigmoid(z): 103 | """ 104 | Compute the sigmoid of z 105 | 106 | Arguments: 107 | z -- A scalar or numpy array of any size. 108 | 109 | Returns: 110 | s -- sigmoid(z) 111 | """ 112 | 113 | s = 1 / (1 + np.exp(-z)) 114 | 115 | return s 116 | 117 | 118 | # INITIALIZE PARAMETERS 119 | def initialize_with_zeros(dim): 120 | """ 121 | This function creates a vector of zeros of shape (dim, 1) for w and initializes b to 0. 122 | 123 | Argument: 124 | dim -- size of the w vector we want (or number of parameters in this case) 125 | 126 | Returns: 127 | w -- initialized vector of shape (dim, 1) 128 | b -- initialized scalar (corresponds to the bias) 129 | """ 130 | 131 | s = (dim, 1) 132 | w = np.zeros(s) 133 | b = 0 134 | 135 | assert (w.shape == (dim, 1)) 136 | assert (isinstance(b, float) or isinstance(b, int)) 137 | 138 | return w, b 139 | 140 | 141 | # FORWARD AND BACKWARD PROPAGATION FUNCTION 142 | def propagate(w, b, X, Y): 143 | """ 144 | Arguments: 145 | w -- weights, a numpy array of size (num_px * num_px * 3, 1) 146 | b -- bias, a scalar 147 | X -- data of size (num_px * num_px * 3, number of examples) 148 | Y -- true "label" vector (containing 0 if non-cat, 1 if cat) of size (1, number of examples) 149 | 150 | Returns: 151 | cost -- negative log-likelihood cost for logistic regression 152 | dw -- gradient of the loss with respect to w, thus same shape as w 153 | db -- gradient of the loss with respect to b, thus same shape as b 154 | """ 155 | 156 | m = X.shape[1] 157 | 158 | # FORWARD PROPAGATION (FROM X TO COST) 159 | A = sigmoid(np.dot(w.T, X) + b) # compute activation 160 | 161 | cost = (-1 / m) * (np.sum((Y * (np.log(A))) + ((1 - Y) * (np.log((1 - A)+0.00000000001))))) 162 | 163 | 164 | # BACKWARD PROPAGATION (TO FIND GRAD) 165 | dw = (1 / m) * (np.dot(X, ((A - Y).T))) 166 | db = (np.sum(A - Y)) * (1 / m) 167 | 168 | assert (dw.shape == w.shape) 169 | assert (db.dtype == float) 170 | cost = np.squeeze(cost) 171 | assert (cost.shape == ()) 172 | 173 | grads = {"dw": dw, 174 | "db": db} 175 | 176 | return grads, cost 177 | 178 | 179 | # OPTIMIZATION FUNCTION 180 | def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost=False): 181 | """ 182 | This function optimizes w and b by running a gradient descent algorithm 183 | 184 | Arguments: 185 | w -- weights, a numpy array of size (num_px * num_px * 3, 1) 186 | b -- bias, a scalar 187 | X -- data of shape (num_px * num_px * 3, number of examples) 188 | Y -- true "label" vector (containing 0 if lake, 1 if airplane), of shape (1, number of examples) 189 | num_iterations -- number of iterations of the optimization loop 190 | learning_rate -- learning rate of the gradient descent update rule 191 | print_cost -- True to print the loss every 100 steps 192 | 193 | Returns: 194 | params -- dictionary containing the weights w and bias b 195 | grads -- dictionary containing the gradients of the weights and bias with respect to the cost function 196 | costs -- list of all the costs computed during the optimization, this will be used to plot the learning curve. 197 | """ 198 | 199 | costs = [] 200 | 201 | for i in range(num_iterations): 202 | 203 | # Cost and gradient calculation 204 | grads, cost = propagate(w, b, X, Y) 205 | 206 | # Retrieve derivatives from grads 207 | dw = grads["dw"] 208 | db = grads["db"] 209 | 210 | # update rule 211 | w = w - (learning_rate * dw) 212 | b = b - (learning_rate * db) 213 | 214 | # Record the costs 215 | if i % 100 == 0: 216 | costs.append(cost) 217 | 218 | # Print the cost every 100 training iterations 219 | if print_cost and i % 100 == 0: 220 | print("Cost after iteration %i: %f" % (i, cost)) 221 | 222 | params = {"w": w, 223 | "b": b} 224 | 225 | grads = {"dw": dw, 226 | "db": db} 227 | 228 | return params, grads, costs 229 | 230 | 231 | # GRADED FUNCTION: predict 232 | def predict(w, b, X): 233 | ''' 234 | Predict whether the label is 0 or 1 using learned logistic regression parameters (w, b) 235 | 236 | Arguments: 237 | w -- weights, a numpy array of size (num_px * num_px * 3, 1) 238 | b -- bias, a scalar 239 | X -- data of size (num_px * num_px * 3, number of examples) 240 | 241 | Returns: 242 | Y_prediction -- a numpy array (vector) containing all predictions (0/1) for the examples in X 243 | ''' 244 | 245 | m = X.shape[1] 246 | Y_prediction = np.zeros((1, m)) 247 | w = w.reshape(X.shape[0], 1) 248 | 249 | # Compute vector "A" predicting the probabilities of an airplane being present in the picture 250 | A = sigmoid(np.dot(w.T, X) + b) 251 | 252 | for i in range(A.shape[1]): 253 | 254 | # Convert probabilities A[0,i] to actual predictions p[0,i] 255 | if A[0, i] <= 0.5: 256 | Y_prediction[0, i] += 0 257 | else: 258 | Y_prediction[0, i] += 1 259 | 260 | assert (Y_prediction.shape == (1, m)) 261 | 262 | return Y_prediction 263 | 264 | 265 | # MODEL FUNCTION: 266 | def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost= True): 267 | """ 268 | Builds the logistic regression model by calling the function you've implemented previously 269 | 270 | Arguments: 271 | X_train -- training set represented by a numpy array of shape (num_px * num_px * 3, m_train) 272 | Y_train -- training labels represented by a numpy array (vector) of shape (1, m_train) 273 | X_test -- test set represented by a numpy array of shape (num_px * num_px * 3, m_test) 274 | Y_test -- test labels represented by a numpy array (vector) of shape (1, m_test) 275 | num_iterations -- hyperparameter representing the number of iterations to optimize the parameters 276 | learning_rate -- hyperparameter representing the learning rate used in the update rule of optimize() 277 | print_cost -- Set to true to print the cost every 100 iterations 278 | 279 | Returns: 280 | d -- dictionary containing information about the model. 281 | """ 282 | 283 | # initialize parameters with zeros 284 | w, b = initialize_with_zeros(X_train.shape[0]) 285 | 286 | # Gradient descent 287 | parameters, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost=True) 288 | 289 | # Retrieve parameters w and b from dictionary "parameters" 290 | w = parameters["w"] 291 | b = parameters["b"] 292 | 293 | # Predict test/train set examples 294 | Y_prediction_test = predict(w, b, X_test) 295 | Y_prediction_train = predict(w, b, X_train) 296 | 297 | # Print train/test Errors 298 | print("train accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100)) 299 | print("test accuracy: {} %".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100)) 300 | 301 | d = {"costs": costs, 302 | "Y_prediction_test": Y_prediction_test, 303 | "Y_prediction_train": Y_prediction_train, 304 | "w": w, 305 | "b": b, 306 | "learning_rate": learning_rate, 307 | "num_iterations": num_iterations} 308 | 309 | return d 310 | 311 | 312 | d = model(X_train, y_train, X_test, y_test, num_iterations = 2000, learning_rate = 0.005, print_cost = True) 313 | 314 | 315 | # Plot learning curve (with costs) 316 | costs1 = np.squeeze(d['costs']) 317 | plt.plot(costs1) 318 | plt.ylabel('cost') 319 | plt.xlabel('iterations (per hundreds)') 320 | plt.title("Learning rate =" + str(d["learning_rate"])) 321 | plt.show() 322 | 323 | 324 | 325 | learning_rates = [0.01, 0.001, 0.0001] 326 | models = {} 327 | for i in learning_rates: 328 | print ("learning rate is: " + str(i)) 329 | models[str(i)] = model(X_train, y_train, X_test, y_test, num_iterations = 1500, learning_rate = i, print_cost = False) 330 | print ('\n' + "-------------------------------------------------------" + '\n') 331 | 332 | for i in learning_rates: 333 | plt.plot(np.squeeze(models[str(i)]["costs"]), label= str(models[str(i)]["learning_rate"])) 334 | 335 | plt.ylabel('cost') 336 | 337 | plt.xlabel('iterations (hundreds)') 338 | 339 | legend = plt.legend(loc='upper center', shadow=True) 340 | frame = legend.get_frame() 341 | frame.set_facecolor('0.90') 342 | plt.show() 343 | -------------------------------------------------------------------------------- /Notes/ConvNets (Pt 1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glosophy/Deep-Learning/5e534477ced5372ab822ac687d0d66b895b9f6cd/Notes/ConvNets (Pt 1).pdf -------------------------------------------------------------------------------- /Notes/ConvNets (Pt 2).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glosophy/Deep-Learning/5e534477ced5372ab822ac687d0d66b895b9f6cd/Notes/ConvNets (Pt 2).pdf -------------------------------------------------------------------------------- /Notes/ConvNets (Pt 3).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glosophy/Deep-Learning/5e534477ced5372ab822ac687d0d66b895b9f6cd/Notes/ConvNets (Pt 3).pdf -------------------------------------------------------------------------------- /Notes/ConvNets (Pt 4).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glosophy/Deep-Learning/5e534477ced5372ab822ac687d0d66b895b9f6cd/Notes/ConvNets (Pt 4).pdf -------------------------------------------------------------------------------- /Notes/Hyperparameter Tuning (Pt 1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glosophy/Deep-Learning/5e534477ced5372ab822ac687d0d66b895b9f6cd/Notes/Hyperparameter Tuning (Pt 1).pdf -------------------------------------------------------------------------------- /Notes/Hyperparameter Tuning (Pt 2).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glosophy/Deep-Learning/5e534477ced5372ab822ac687d0d66b895b9f6cd/Notes/Hyperparameter Tuning (Pt 2).pdf -------------------------------------------------------------------------------- /Notes/Neural Netowkrs and Deep Learning.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glosophy/Deep-Learning/5e534477ced5372ab822ac687d0d66b895b9f6cd/Notes/Neural Netowkrs and Deep Learning.pdf -------------------------------------------------------------------------------- /Notes/README.md: -------------------------------------------------------------------------------- 1 | ### Deep Learning Specialization Notes 2 | 3 | These are some notes from the deeplearning.ai's [Deep Learning Specialization](https://www.coursera.org/specializations/deep-learning) in Cousera. 4 | 5 | Reading order: 6 | 1. Neural Networks and Deep Learning 7 | 2. Hyperparamenter Tuning 8 | 3. Structuring Machine Learning Projects 9 | 4. Conv Nets 10 | 5. Sequence Models 11 | 12 | You can find the [complete pdf here](https://drive.google.com/file/d/1VC8LpK9yys-9T9pZQe_MLRBZMOdZlx1J/view?usp=sharing) 13 | -------------------------------------------------------------------------------- /Notes/Sequence Models (Pt 1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glosophy/Deep-Learning/5e534477ced5372ab822ac687d0d66b895b9f6cd/Notes/Sequence Models (Pt 1).pdf -------------------------------------------------------------------------------- /Notes/Sequence Models (Pt 2).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glosophy/Deep-Learning/5e534477ced5372ab822ac687d0d66b895b9f6cd/Notes/Sequence Models (Pt 2).pdf -------------------------------------------------------------------------------- /Notes/Structuring ML Projects (Pt 1).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glosophy/Deep-Learning/5e534477ced5372ab822ac687d0d66b895b9f6cd/Notes/Structuring ML Projects (Pt 1).pdf -------------------------------------------------------------------------------- /Notes/Structuring ML Projects (Pt 2).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/glosophy/Deep-Learning/5e534477ced5372ab822ac687d0d66b895b9f6cd/Notes/Structuring ML Projects (Pt 2).pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deep Learning 2 | This repo contains some useful scripts for deep learning. 3 | --------------------------------------------------------------------------------