-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cat classification using 1 sigmoid hidden layer
- Loading branch information
1 parent
ddcfb06
commit d1b9e9e
Showing
2 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,286 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Cat classification with sigmoid function" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<b><i>Importing python libraries</i></b>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 26, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import h5py\n", | ||
"import scipy\n", | ||
"from PIL import Image\n", | ||
"from scipy import ndimage\n", | ||
"from random import shuffle\n", | ||
"from tqdm import tqdm\n", | ||
"import os\n", | ||
"import cv2\n", | ||
"from sklearn.metrics import accuracy_score" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 28, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"training_data = np.load('C:/Users/Rohit/Music/machine_learning_projects/cat_vs_noncat/cat_vs_noncat_10000.npy')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<b><i>Preprocessing step:</i></b>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 29, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"labels = training_data[0, 1]\n", | ||
"for i in range(1, training_data.shape[0]):\n", | ||
"\tlabels = np.concatenate((labels, training_data[i, 1]), axis = 0)\n", | ||
"labels = labels.reshape(labels.shape[0], 1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 30, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"train_data = training_data[0, 0]\n", | ||
"for i in range(1, training_data.shape[0]):\n", | ||
"\ttrain_data = np.concatenate((train_data, training_data[i, 0]), axis = 1)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 31, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"train_data = train_data/255" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 32, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"(3072, 19000)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"X = train_data[:,:19000]\n", | ||
"Y = labels[:19000,:]\n", | ||
"X_test = train_data[:, 19000:]\n", | ||
"Y_test = labels[19000:, :]\n", | ||
"print(X.shape)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<b><i>Initialization of parameters. </i></b>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 33, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"w = np.zeros((train_data.shape[0], 1))\n", | ||
"b = 0" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 34, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def sigmoid(z):\n", | ||
"\n", | ||
"\ts = 1/(1 + np.exp(-z.astype(float)))\n", | ||
"\n", | ||
"\treturn s" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<b><i>Gradient calculation using Forward and Backward Propagation</i></b>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 35, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def propagate(w, b, X, Y):\n", | ||
"\tz = np.dot(X.T, w) + b\n", | ||
"\ta = sigmoid(z)\n", | ||
"\tdz = a - Y\n", | ||
"\tdw = np.dot(X, dz)/X.shape[1]\n", | ||
"\tdb = np.sum(dz)/X.shape[1]\n", | ||
"\t#print(db)\n", | ||
"\tgrads = {\"dw\": dw,\n", | ||
"\t\t\t\"db\": db}\n", | ||
"\treturn grads" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<b><i>Optimization function</i></b>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 36, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def optimize(w, b, X, Y, num_iterations, learning_rate):\n", | ||
"\n", | ||
"\tfor i in range(num_iterations):\n", | ||
"\n", | ||
"\t\tgrads = propagate(w, b, X, Y)\n", | ||
"\t\tdw = grads[\"dw\"]\n", | ||
"\t\tdb = grads[\"db\"]\n", | ||
"\t\tw = w - learning_rate*dw\n", | ||
"\t\tb = b - learning_rate*db\n", | ||
"\tparams = {\"w\": w,\n", | ||
"\t\t\t\t\"b\": b}\n", | ||
"\tgrads = {\"dw\": dw,\n", | ||
"\t\t\t\t\"db\":db}\n", | ||
"\treturn params, grads" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 37, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"params, grads = optimize(w, b, X, Y, num_iterations= 100, learning_rate = 0.009)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<b><i>Prediction on test data</i></b>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 38, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def predict(w, b, X, Y):\n", | ||
"\tz = np.dot(X.T, w) + b\n", | ||
"\ta = sigmoid(z)\n", | ||
"\tfor i in range(a.shape[0]):\n", | ||
"\t\tif a[i, 0] < 0.5:\n", | ||
"\t\t\ta[i, 0] = np.array([0])\n", | ||
"\t\telse:\n", | ||
"\t\t\ta[i, 0] = np.array([1])\n", | ||
"\treturn a\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 39, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"w = params[\"w\"]\n", | ||
"b = params[\"b\"]\n", | ||
"pred_Y = predict(w, b, X_test, Y_test)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 40, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Accuracy: 0.631\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(\"Accuracy: \",accuracy_score(Y_test, pred_Y))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<h3 style = \"color:red\">Note: </h3><b><i>This neural network model uses only one layer i.e. output layer with one sigmoid unit just to predict 0/1. This model is trained on just 1800 train images and tested on 200 test images.</i><b>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.5.0" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Binary file not shown.