From aa2daa373fafff79862fe04bfa51628c42bbe093 Mon Sep 17 00:00:00 2001 From: Somnath Saha Date: Sat, 13 Jan 2018 09:16:37 +0530 Subject: [PATCH] Add files via upload --- Breast_cancer_1.ipynb | 516 +++++++++++++++++++ Breast_cancer_2.ipynb | 1107 +++++++++++++++++++++++++++++++++++++++++ Breast_cancer_3.ipynb | 541 ++++++++++++++++++++ 3 files changed, 2164 insertions(+) create mode 100644 Breast_cancer_1.ipynb create mode 100644 Breast_cancer_2.ipynb create mode 100644 Breast_cancer_3.ipynb diff --git a/Breast_cancer_1.ipynb b/Breast_cancer_1.ipynb new file mode 100644 index 0000000..90f1202 --- /dev/null +++ b/Breast_cancer_1.ipynb @@ -0,0 +1,516 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 244, + "metadata": {}, + "outputs": [], + "source": [ + "import sklearn\n", + "import warnings" + ] + }, + { + "cell_type": "code", + "execution_count": 245, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.datasets import load_breast_cancer" + ] + }, + { + "cell_type": "code", + "execution_count": 246, + "metadata": {}, + "outputs": [], + "source": [ + "data = load_breast_cancer()" + ] + }, + { + "cell_type": "code", + "execution_count": 247, + "metadata": {}, + "outputs": [], + "source": [ + "feature_names = data['feature_names']\n", + "features = data['data']\n", + "label_names = data['target_names']\n", + "labels = data['target']" + ] + }, + { + "cell_type": "code", + "execution_count": 248, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['mean radius' 'mean texture' 'mean perimeter' 'mean area'\n", + " 'mean smoothness' 'mean compactness' 'mean concavity'\n", + " 'mean concave points' 'mean symmetry' 'mean fractal dimension'\n", + " 'radius error' 'texture error' 'perimeter error' 'area error'\n", + " 'smoothness error' 'compactness error' 'concavity error'\n", + " 'concave points error' 'symmetry error' 'fractal dimension error'\n", + " 'worst radius' 'worst texture' 'worst perimeter' 'worst area'\n", + " 'worst smoothness' 'worst compactness' 'worst concavity'\n", + " 'worst concave points' 'worst symmetry' 'worst fractal dimension']\n" + ] + } + ], + "source": [ + "print(feature_names)" + ] + }, + { + "cell_type": "code", + "execution_count": 249, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[ 1.79900000e+01 1.03800000e+01 1.22800000e+02 1.00100000e+03\n", + " 1.18400000e-01 2.77600000e-01 3.00100000e-01 1.47100000e-01\n", + " 2.41900000e-01 7.87100000e-02 1.09500000e+00 9.05300000e-01\n", + " 8.58900000e+00 1.53400000e+02 6.39900000e-03 4.90400000e-02\n", + " 5.37300000e-02 1.58700000e-02 3.00300000e-02 6.19300000e-03\n", + " 2.53800000e+01 1.73300000e+01 1.84600000e+02 2.01900000e+03\n", + " 1.62200000e-01 6.65600000e-01 7.11900000e-01 2.65400000e-01\n", + " 4.60100000e-01 1.18900000e-01]\n" + ] + } + ], + "source": [ + "print(features[0])" + ] + }, + { + "cell_type": "code", + "execution_count": 250, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['malignant' 'benign']\n" + ] + } + ], + "source": [ + "print(label_names)" + ] + }, + { + "cell_type": "code", + "execution_count": 251, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", + " 1 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 0 0 1 0 0 1 1 1 1 0 1 0 0 1 1 1 1 0 1 0 0\n", + " 1 0 1 0 0 1 1 1 0 0 1 0 0 0 1 1 1 0 1 1 0 0 1 1 1 0 0 1 1 1 1 0 1 1 0 1 1\n", + " 1 1 1 1 1 1 0 0 0 1 0 0 1 1 1 0 0 1 0 1 0 0 1 0 0 1 1 0 1 1 0 1 1 1 1 0 1\n", + " 1 1 1 1 1 1 1 1 0 1 1 1 1 0 0 1 0 1 1 0 0 1 1 0 0 1 1 1 1 0 1 1 0 0 0 1 0\n", + " 1 0 1 1 1 0 1 1 0 0 1 0 0 0 0 1 0 0 0 1 0 1 0 1 1 0 1 0 0 0 0 1 1 0 0 1 1\n", + " 1 0 1 1 1 1 1 0 0 1 1 0 1 1 0 0 1 0 1 1 1 1 0 1 1 1 1 1 0 1 0 0 0 0 0 0 0\n", + " 0 0 0 0 0 0 0 1 1 1 1 1 1 0 1 0 1 1 0 1 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1\n", + " 1 0 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 1 1 1 0 0 0 1 1\n", + " 1 1 0 1 0 1 0 1 1 1 0 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0\n", + " 0 1 0 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1\n", + " 1 0 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 0 1 1 1 1 1 0 1 1\n", + " 0 1 0 1 1 0 1 0 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1\n", + " 1 1 1 1 1 1 0 1 0 1 1 0 1 1 1 1 1 0 0 1 0 1 0 1 1 1 1 1 0 1 1 0 1 0 1 0 0\n", + " 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", + " 1 1 1 1 1 1 1 0 0 0 0 0 0 1]\n" + ] + } + ], + "source": [ + "print(labels)" + ] + }, + { + "cell_type": "code", + "execution_count": 252, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.model_selection import train_test_split" + ] + }, + { + "cell_type": "code", + "execution_count": 253, + "metadata": {}, + "outputs": [], + "source": [ + "train, test, train_labels, test_labels = train_test_split(features, labels, test_size=0.80, random_state=42)" + ] + }, + { + "cell_type": "code", + "execution_count": 254, + "metadata": {}, + "outputs": [], + "source": [ + "train_set_x = train.T\n", + "train_set_y = train_labels.T\n", + "test_set_x = test.T\n", + "test_set_y = test_labels.T" + ] + }, + { + "cell_type": "code", + "execution_count": 255, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 256, + "metadata": {}, + "outputs": [], + "source": [ + "def sigmoid(z):\n", + " s = 1/(1+np.exp(-z))\n", + " return s" + ] + }, + { + "cell_type": "code", + "execution_count": 257, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "sigmoid([0, 2]) = [ 0.5 0.88079708]\n" + ] + } + ], + "source": [ + "print (\"sigmoid([0, 2]) = \" + str(sigmoid(np.array([0,2]))))" + ] + }, + { + "cell_type": "code", + "execution_count": 258, + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_with_zeros(dim):\n", + " w = np.zeros((dim,1),dtype=float)\n", + " b = 0\n", + " assert(w.shape == (dim, 1))\n", + " assert(isinstance(b, float) or isinstance(b, int))\n", + " \n", + " return w, b" + ] + }, + { + "cell_type": "code", + "execution_count": 259, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "w = [[ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]]\n", + "b = 0\n" + ] + } + ], + "source": [ + "dim = 30\n", + "w, b = initialize_with_zeros(dim)\n", + "print (\"w = \" + str(w))\n", + "print (\"b = \" + str(b))" + ] + }, + { + "cell_type": "code", + "execution_count": 260, + "metadata": {}, + "outputs": [], + "source": [ + "def propagate(w, b, X, Y):\n", + " m = X.shape[1]\n", + " A = 1 / (1 + np.exp(-(np.dot(np.transpose(w), X) + b)))\n", + " cost = -((np.sum(Y*np.log(A) + (1 - Y) * np.log(1 - A)))/m)\n", + " \n", + " dw = (np.dot(X,np.transpose(A-Y)))/m\n", + " db = np.sum(A-Y)/m\n", + " \n", + " assert(dw.shape == w.shape)\n", + " assert(db.dtype == float)\n", + " cost = np.squeeze(cost)\n", + " assert(cost.shape == ())\n", + " \n", + " grads = {\"dw\": dw,\n", + " \"db\": db}\n", + " \n", + " return grads, cost" + ] + }, + { + "cell_type": "code", + "execution_count": 261, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dw = [[ 0.99845601]\n", + " [ 2.39507239]]\n", + "db = 0.00145557813678\n", + "cost = 5.80154531939\n" + ] + } + ], + "source": [ + "w, b, X, Y = np.array([[1.],[2.]]), 2., np.array([[1.,2.,-1.],[3.,4.,-3.2]]), np.array([[1,0,1]])\n", + "grads, cost = propagate(w, b, X, Y)\n", + "print (\"dw = \" + str(grads[\"dw\"]))\n", + "print (\"db = \" + str(grads[\"db\"]))\n", + "print (\"cost = \" + str(cost))" + ] + }, + { + "cell_type": "code", + "execution_count": 262, + "metadata": {}, + "outputs": [], + "source": [ + "def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost = False):\n", + " costs = []\n", + " \n", + " for i in range(num_iterations):\n", + " \n", + " \n", + " grads, cost = propagate(w,b,X,Y)\n", + " \n", + " dw = grads[\"dw\"]\n", + " db = grads[\"db\"]\n", + " \n", + " w = w-learning_rate*dw\n", + " b = b-learning_rate*db\n", + " \n", + " if i % 100 == 0:\n", + " costs.append(cost)\n", + " \n", + " if print_cost and i % 100 == 0:\n", + " print (\"Cost after iteration %i: %f\" %(i, cost))\n", + " \n", + " params = {\"w\": w,\n", + " \"b\": b}\n", + " \n", + " grads = {\"dw\": dw,\n", + " \"db\": db}\n", + " \n", + " return params, grads, costs" + ] + }, + { + "cell_type": "code", + "execution_count": 263, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "w = [[ 0.19033591]\n", + " [ 0.12259159]]\n", + "b = 1.92535983008\n", + "dw = [[ 0.67752042]\n", + " [ 1.41625495]]\n", + "db = 0.219194504541\n" + ] + } + ], + "source": [ + "params, grads, costs = optimize(w, b, X, Y, num_iterations= 100, learning_rate = 0.009, print_cost = False)\n", + "\n", + "print (\"w = \" + str(params[\"w\"]))\n", + "print (\"b = \" + str(params[\"b\"]))\n", + "print (\"dw = \" + str(grads[\"dw\"]))\n", + "print (\"db = \" + str(grads[\"db\"]))" + ] + }, + { + "cell_type": "code", + "execution_count": 264, + "metadata": {}, + "outputs": [], + "source": [ + "def predict(w, b, X):\n", + " m = X.shape[1]\n", + " Y_prediction = np.zeros((1,m))\n", + " w = w.reshape(X.shape[0], 1)\n", + " \n", + " A = 1/(1+np.exp(-(np.dot(np.transpose(w),X)+b))) \n", + " \n", + " for i in range(A.shape[1]):\n", + " \n", + " if (A[0][i] > 0.5):\n", + " Y_prediction[0][i]=1\n", + " else:\n", + " Y_prediction[0][i]=0\n", + " \n", + " assert(Y_prediction.shape == (1, m))\n", + " \n", + " return Y_prediction" + ] + }, + { + "cell_type": "code", + "execution_count": 265, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "predictions = [[ 1. 1. 0.]]\n" + ] + } + ], + "source": [ + "w = np.array([[0.1124579],[0.23106775]])\n", + "b = -0.3\n", + "X = np.array([[1.,-1.1,-3.2],[1.2,2.,0.1]])\n", + "print (\"predictions = \" + str(predict(w, b, X)))" + ] + }, + { + "cell_type": "code", + "execution_count": 266, + "metadata": {}, + "outputs": [], + "source": [ + "def model(X_train, Y_train, X_test, Y_test, num_iterations = 20, learning_rate = 0.5, print_cost = False):\n", + " w, b = np.zeros((X_train.shape[0],1), dtype=float), 0\n", + "\n", + " # Gradient descent (≈ 1 line of code)\n", + " parameters, grads, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost=False)\n", + " \n", + " # Retrieve parameters w and b from dictionary \"parameters\"\n", + " w = parameters[\"w\"]\n", + " b = parameters[\"b\"]\n", + " \n", + " # Predict test/train set examples (≈ 2 lines of code)\n", + " Y_prediction_test = predict(w, b, X_test)\n", + " Y_prediction_train = predict(w, b, X_train)\n", + "\n", + " ### END CODE HERE ###\n", + "\n", + " # Print train/test Errors\n", + " #print(\"train accuracy: {} %\".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))\n", + " print(\"test accuracy: {} %\".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))\n", + "\n", + " \n", + " d = {\"costs\": costs,\n", + " \"Y_prediction_test\": Y_prediction_test, \n", + " \"Y_prediction_train\" : Y_prediction_train, \n", + " \"w\" : w, \n", + " \"b\" : b,\n", + " \"learning_rate\" : learning_rate,\n", + " \"num_iterations\": num_iterations}\n", + " \n", + " return d" + ] + }, + { + "cell_type": "code", + "execution_count": 267, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "test accuracy: 92.7631578947 %\n" + ] + } + ], + "source": [ + "d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations = 2000, learning_rate = 0.005)\n", + "warnings.filterwarnings('ignore')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.14" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Breast_cancer_2.ipynb b/Breast_cancer_2.ipynb new file mode 100644 index 0000000..f96d99c --- /dev/null +++ b/Breast_cancer_2.ipynb @@ -0,0 +1,1107 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [], + "source": [ + "import sklearn\n", + "import numpy as np\n", + "import h5py\n", + "from dnn_utils_v2 import sigmoid, sigmoid_backward, relu, relu_backward\n", + "from sklearn.datasets import load_breast_cancer\n", + "from testCases_v2 import *" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "data = load_breast_cancer()" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "feature_names = data['feature_names']\n", + "features = data['data']\n", + "label_names = data['target_names']\n", + "labels = data['target']" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.model_selection import train_test_split" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "train, test, train_labels, test_labels = train_test_split(features, labels, test_size=0.20, random_state=42)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "train_set_x = train.T\n", + "train_set_y = train_labels.T\n", + "test_set_x = test.T\n", + "test_set_y = test_labels.T" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "train_set_x = np.asarray(train_set_x)\n", + "train_set_y = np.reshape(train_set_y,[455,1])\n", + "test_set_x = np.asarray(test_set_x)\n", + "test_set_y = np.reshape(test_set_y,[114,1])" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [], + "source": [ + "def layer_sizes(X, Y):\n", + " n_x = X.shape[0]\n", + " n_h = 4\n", + " n_y = Y.shape[0]\n", + " return (n_x, n_h, n_y)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The size of the input layer is: n_x = 30\n", + "The size of the hidden layer is: n_h = 4\n", + "The size of the output layer is: n_y = 455\n" + ] + } + ], + "source": [ + "#X_assess, Y_assess = layer_sizes_test_case()\n", + "(n_x, n_h, n_y) = layer_sizes(train_set_x,train_set_y)\n", + "print(\"The size of the input layer is: n_x = \" + str(n_x))\n", + "print(\"The size of the hidden layer is: n_h = \" + str(n_h))\n", + "print(\"The size of the output layer is: n_y = \" + str(n_y))" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_parameters(n_x, n_h, n_y):\n", + " np.random.seed(2)\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", + " parameters = {\"W1\": W1,\n", + " \"b1\": b1,\n", + " \"W2\": W2,\n", + " \"b2\": b2}\n", + " \n", + " return parameters" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "W1 = [[ -4.16757847e-03 -5.62668272e-04 -2.13619610e-02 1.64027081e-02\n", + " -1.79343559e-02 -8.41747366e-03 5.02881417e-03 -1.24528809e-02\n", + " -1.05795222e-02 -9.09007615e-03 5.51454045e-03 2.29220801e-02\n", + " 4.15393930e-04 -1.11792545e-02 5.39058321e-03 -5.96159700e-03\n", + " -1.91304965e-04 1.17500122e-02 -7.47870949e-03 9.02525097e-05\n", + " -8.78107893e-03 -1.56434170e-03 2.56570452e-03 -9.88779049e-03\n", + " -3.38821966e-03 -2.36184031e-03 -6.37655012e-03 -1.18761229e-02\n", + " -1.42121723e-02 -1.53495196e-03]\n", + " [ -2.69056960e-03 2.23136679e-02 -2.43476758e-02 1.12726505e-03\n", + " 3.70444537e-03 1.35963386e-02 5.01857207e-03 -8.44213704e-03\n", + " 9.76147160e-08 5.42352572e-03 -3.13508197e-03 7.71011738e-03\n", + " -1.86809065e-02 1.73118467e-02 1.46767801e-02 -3.35677339e-03\n", + " 6.11340780e-03 4.79705919e-04 -8.29135289e-03 8.77102184e-04\n", + " 1.00036589e-02 -3.81092518e-03 -3.75669423e-03 -7.44707629e-04\n", + " 4.33496330e-03 1.27837923e-02 -6.34679305e-03 5.08396243e-03\n", + " 2.16116006e-03 -1.85861239e-02]\n", + " [ -4.19316482e-03 -1.32328898e-03 -3.95702397e-04 3.26003433e-03\n", + " -2.04032305e-02 4.62555231e-04 -6.77675577e-03 -1.43943903e-02\n", + " 5.24296430e-03 7.35279576e-03 -6.53250268e-03 8.42456282e-03\n", + " -3.81516482e-03 6.64890091e-04 -1.09873895e-02 1.58448706e-02\n", + " -2.65944946e-02 -9.14526229e-04 6.95119605e-03 -2.03346655e-02\n", + " -1.89469265e-03 -7.72186654e-04 8.24703005e-03 1.24821292e-02\n", + " -4.03892269e-03 -1.38451867e-02 1.36723542e-02 1.21788563e-02\n", + " -4.62005348e-03 3.50888494e-03]\n", + " [ 3.81866234e-03 5.66275441e-03 2.04207979e-03 1.40669624e-02\n", + " -1.73795950e-02 1.04082395e-02 3.80471970e-03 -2.17135269e-03\n", + " 1.17353150e-02 -2.34360319e-02 1.16152149e-02 3.86078048e-03\n", + " -1.13313327e-02 4.33092555e-03 -3.04086439e-03 2.58529487e-02\n", + " 1.83533272e-02 4.40689872e-03 -7.19253841e-03 -5.83414595e-03\n", + " -3.25049628e-03 -5.60234506e-03 -9.02246068e-03 -5.90972275e-03\n", + " -2.76179492e-03 -5.16883894e-03 -6.98589950e-03 -9.28891925e-03\n", + " 2.55043824e-02 -1.47317325e-02]]\n", + "b1 = [[ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]]\n", + "W2 = [[-0.01021415 0.00432396 -0.0032358 0.00423825]\n", + " [ 0.0079918 0.01262614 0.00751965 -0.00993761]\n", + " [ 0.01109143 -0.01764918 -0.00114421 -0.00498174]\n", + " ..., \n", + " [-0.01866306 -0.03136547 0.00379628 -0.0091896 ]\n", + " [ 0.01286028 0.00788292 0.01262143 0.00823994]\n", + " [-0.00459355 0.00377281 -0.015075 -0.01203564]]\n", + "b2 = [[ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]\n", + " [ 0.]]\n" + ] + } + ], + "source": [ + "parameters = initialize_parameters(n_x, n_h, n_y)\n", + "print(\"W1 = \" + str(parameters[\"W1\"]))\n", + "print(\"b1 = \" + str(parameters[\"b1\"]))\n", + "print(\"W2 = \" + str(parameters[\"W2\"]))\n", + "print(\"b2 = \" + str(parameters[\"b2\"]))" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "metadata": {}, + "outputs": [], + "source": [ + "def forward_propagation(X, parameters):\n", + " W1 = parameters[\"W1\"]\n", + " b1 = parameters[\"b1\"]\n", + " W2 = parameters[\"W2\"]\n", + " b2 = parameters[\"b2\"]\n", + " \n", + " Z1 = np.dot(W1,X)+b1\n", + " A1 = np.tanh(Z1)\n", + " Z2 = np.dot(W2,A1)+b2\n", + " A2 = sigmoid(Z2)\n", + " A2 = np.array(A2)\n", + " \n", + " #assert(A2.shape == (1, X.shape[1]))\n", + " \n", + " cache = {\"Z1\": Z1,\n", + " \"A1\": A1,\n", + " \"Z2\": Z2,\n", + " \"A2\": A2}\n", + " \n", + " return A2, cache" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [], + "source": [ + "A2, cache = forward_propagation(train_set_x, parameters)" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3.8550913462803287, 0.26653259470817869, 0.0003619250854785507, 0.25022620939274792)\n" + ] + } + ], + "source": [ + "print(np.mean(cache['Z1']) ,np.mean(cache['A1']),np.mean(cache['Z2']),np.mean(cache['A2']))" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": {}, + "outputs": [], + "source": [ + "def compute_cost(A2, Y, parameters):\n", + " m = Y.shape[1]\n", + "\n", + " logprobs = np.dot(np.log(A2),Y)+np.dot(np.log(1-A2),(1-Y))\n", + " cost = - np.sum(logprobs)/m\n", + "\n", + " return cost" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "cost = nan\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "c:\\python27\\lib\\site-packages\\ipykernel_launcher.py:6: RuntimeWarning: invalid value encountered in log\n", + " \n" + ] + } + ], + "source": [ + "print(\"cost = \" + str(compute_cost(A2, train_set_y, parameters)))" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": {}, + "outputs": [], + "source": [ + "def backward_propagation(parameters, cache, X, Y):\n", + " m = X.shape[1]\n", + " \n", + " W1 = parameters[\"W1\"]\n", + " W2 = parameters[\"W2\"]\n", + " \n", + " A1 = cache[\"A1\"]\n", + " A2 = cache[\"A2\"]\n", + " print(type(Y))\n", + " print(Y.shape)\n", + " A2=np.reshape(A2,[455,1])\n", + " print(type(A2))\n", + " print(A2.shape)\n", + " \n", + " dZ2 = A2-Y\n", + " dW2 = np.dot(dZ2,A1.T)/m\n", + " db2 = np.sum(dZ2,axis=1,keepdims=True)/m\n", + " print(W2.shape)\n", + " print(dZ2.shape)\n", + " \n", + " #dZ1 = np.dot(W2.T,dZ2)*(1-np.power(A1,2))\n", + " dW1 = np.dot(dZ1,X.T)/m\n", + " db1 = np.sum(dZ1,axis=1,keepdims=True)/m\n", + " \n", + " grads = {\"dW1\": dW1,\n", + " \"db1\": db1,\n", + " \"dW2\": dW2,\n", + " \"db2\": db2}\n", + " \n", + " return grads" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "(455, 1)\n" + ] + }, + { + "ename": "ValueError", + "evalue": "cannot reshape array of size 414050 into shape (455,1)", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mgrads\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbackward_propagation\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparameters\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcache\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtrain_set_x\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtrain_set_y\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"dW1 = \"\u001b[0m\u001b[1;33m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgrads\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"dW1\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"db1 = \"\u001b[0m\u001b[1;33m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgrads\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"db1\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"dW2 = \"\u001b[0m\u001b[1;33m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgrads\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"dW2\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m\"db2 = \"\u001b[0m\u001b[1;33m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgrads\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"db2\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m\u001b[0m in \u001b[0;36mbackward_propagation\u001b[1;34m(parameters, cache, X, Y)\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mY\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mY\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0mA2\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mA2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m455\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 12\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mA2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 13\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mA2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mc:\\python27\\lib\\site-packages\\numpy\\core\\fromnumeric.pyc\u001b[0m in \u001b[0;36mreshape\u001b[1;34m(a, newshape, order)\u001b[0m\n\u001b[0;32m 230\u001b[0m [5, 6]])\n\u001b[0;32m 231\u001b[0m \"\"\"\n\u001b[1;32m--> 232\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0m_wrapfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'reshape'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnewshape\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0morder\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 233\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 234\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mc:\\python27\\lib\\site-packages\\numpy\\core\\fromnumeric.pyc\u001b[0m in \u001b[0;36m_wrapfunc\u001b[1;34m(obj, method, *args, **kwds)\u001b[0m\n\u001b[0;32m 55\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_wrapfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 56\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 57\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 58\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 59\u001b[0m \u001b[1;31m# An AttributeError occurs if the object does not have\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mValueError\u001b[0m: cannot reshape array of size 414050 into shape (455,1)" + ] + } + ], + "source": [ + "grads = backward_propagation(parameters, cache, train_set_x, train_set_y)\n", + "print (\"dW1 = \"+ str(grads[\"dW1\"]))\n", + "print (\"db1 = \"+ str(grads[\"db1\"]))\n", + "print (\"dW2 = \"+ str(grads[\"dW2\"]))\n", + "print (\"db2 = \"+ str(grads[\"db2\"]))" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": {}, + "outputs": [], + "source": [ + "def update_parameters(parameters, grads, learning_rate = 1.2):\n", + " \"\"\"\n", + " Updates parameters using the gradient descent update rule given above\n", + " \n", + " Arguments:\n", + " parameters -- python dictionary containing your parameters \n", + " grads -- python dictionary containing your gradients \n", + " \n", + " Returns:\n", + " parameters -- python dictionary containing your updated parameters \n", + " \"\"\"\n", + " W1 = parameters[\"W1\"]\n", + " b1 = parameters[\"b1\"]\n", + " W2 = parameters[\"W2\"]\n", + " b2 = parameters[\"b2\"]\n", + " \n", + " dW1 = grads[\"dW1\"]\n", + " db1 = grads[\"db1\"]\n", + " dW2 = grads[\"dW2\"]\n", + " db2 = grads[\"db2\"]\n", + " \n", + " W1 = W1-learning_rate*dW1\n", + " b1 = b1-learning_rate*db1\n", + " W2 = W2-learning_rate*dW2\n", + " b2 = b2-learning_rate*db2\n", + " \n", + " parameters = {\"W1\": W1,\n", + " \"b1\": b1,\n", + " \"W2\": W2,\n", + " \"b2\": b2}\n", + " \n", + " return parameters" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "parameters = update_parameters(parameters, grads)\n", + "\n", + "print(\"W1 = \" + str(parameters[\"W1\"]))\n", + "print(\"b1 = \" + str(parameters[\"b1\"]))\n", + "print(\"W2 = \" + str(parameters[\"W2\"]))\n", + "print(\"b2 = \" + str(parameters[\"b2\"]))" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": {}, + "outputs": [], + "source": [ + "def nn_model(X, Y, n_h, num_iterations = 10000, print_cost=False):\n", + " \"\"\"\n", + " Arguments:\n", + " X -- dataset of shape (2, number of examples)\n", + " Y -- labels of shape (1, number of examples)\n", + " n_h -- size of the hidden layer\n", + " num_iterations -- Number of iterations in gradient descent loop\n", + " print_cost -- if True, print the cost every 1000 iterations\n", + " \n", + " Returns:\n", + " parameters -- parameters learnt by the model. They can then be used to predict.\n", + " \"\"\"\n", + " \n", + " np.random.seed(3)\n", + " n_x = layer_sizes(X, Y)[0]\n", + " n_y = layer_sizes(X, Y)[2]\n", + " \n", + " # Initialize parameters, then retrieve W1, b1, W2, b2. Inputs: \"n_x, n_h, n_y\". Outputs = \"W1, b1, W2, b2, parameters\".\n", + " ### START CODE HERE ### (≈ 5 lines of code)\n", + " parameters = initialize_parameters(n_x,n_h,n_y)\n", + " W1 = parameters[\"W1\"]\n", + " b1 = parameters[\"b1\"]\n", + " W2 = parameters[\"W2\"]\n", + " b2 = parameters[\"b2\"]\n", + " ### END CODE HERE ###\n", + " \n", + " # Loop (gradient descent)\n", + "\n", + " for i in range(0, num_iterations):\n", + " \n", + " ### START CODE HERE ### (≈ 4 lines of code)\n", + " # Forward propagation. Inputs: \"X, parameters\". Outputs: \"A2, cache\".\n", + " A2, cache = forward_propagation(X,parameters)\n", + " \n", + " # Cost function. Inputs: \"A2, Y, parameters\". Outputs: \"cost\".\n", + " cost = compute_cost(A2,Y,parameters)\n", + " \n", + " # Backpropagation. Inputs: \"parameters, cache, X, Y\". Outputs: \"grads\".\n", + " grads = backward_propagation(parameters,cache,X,Y)\n", + " \n", + " # Gradient descent parameter update. Inputs: \"parameters, grads\". Outputs: \"parameters\".\n", + " parameters = update_parameters(parameters,grads)\n", + " \n", + " ### END CODE HERE ###\n", + " \n", + " if print_cost and i % 1000 == 0:\n", + " print (\"Cost after iteration %i: %f\" %(i, cost))\n", + "\n", + " return parameters" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "parameters = nn_model(train_set_x, train_set_y, 4, num_iterations=10000, print_cost=True)\n", + "print(\"W1 = \" + str(parameters[\"W1\"]))\n", + "print(\"b1 = \" + str(parameters[\"b1\"]))\n", + "print(\"W2 = \" + str(parameters[\"W2\"]))\n", + "print(\"b2 = \" + str(parameters[\"b2\"]))" + ] + }, + { + "cell_type": "code", + "execution_count": 119, + "metadata": {}, + "outputs": [], + "source": [ + "def predict(parameters, X):\n", + " \"\"\"\n", + " Using the learned parameters, predicts a class for each example in X\n", + " \n", + " Arguments:\n", + " parameters -- python dictionary containing your parameters \n", + " X -- input data of size (n_x, m)\n", + " \n", + " Returns\n", + " predictions -- vector of predictions of our model (red: 0 / blue: 1)\n", + " \"\"\"\n", + " \n", + " # Computes probabilities using forward propagation, and classifies to 0/1 using 0.5 as the threshold.\n", + " ### START CODE HERE ### (≈ 2 lines of code)\n", + " A2, cache = forward_propagation(X,parameters)\n", + " #print (A2.shape[1])\n", + " \"\"\"\n", + " for i in range(A2.shape[1]):\n", + " if(A2[0][i]>0.5):\n", + " predictions[0][i]=1\n", + " else:\n", + " predictions[0][i]=0\n", + " #predictions = A2\n", + " assert(predictions.shape==A2.shape)\n", + " #predictions=A2\n", + " \"\"\"\n", + " A2[A2>0.5]=1\n", + " A2[A2<0.5]=0\n", + " predictions=A2\n", + " ### END CODE HERE ###\n", + " \n", + " return predictions" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "predictions mean = 0.257695930443\n" + ] + } + ], + "source": [ + "predictions = predict(parameters, train_set_x)\n", + "print(\"predictions mean = \" + str(np.mean(predictions)))" + ] + }, + { + "cell_type": "code", + "execution_count": 122, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\n", + "(114, 1)\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "c:\\python27\\lib\\site-packages\\ipykernel_launcher.py:6: RuntimeWarning: invalid value encountered in log\n", + " \n" + ] + }, + { + "ename": "ValueError", + "evalue": "cannot reshape array of size 25992 into shape (455,1)", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mparameters\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnn_model\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtest_set_x\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtest_set_y\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mn_h\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnum_iterations\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m10000\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mprint_cost\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mTrue\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;32m\u001b[0m in \u001b[0;36mnn_model\u001b[1;34m(X, Y, n_h, num_iterations, print_cost)\u001b[0m\n\u001b[0;32m 37\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 38\u001b[0m \u001b[1;31m# Backpropagation. Inputs: \"parameters, cache, X, Y\". Outputs: \"grads\".\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 39\u001b[1;33m \u001b[0mgrads\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbackward_propagation\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparameters\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcache\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mY\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 40\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 41\u001b[0m \u001b[1;31m# Gradient descent parameter update. Inputs: \"parameters, grads\". Outputs: \"parameters\".\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32m\u001b[0m in \u001b[0;36mbackward_propagation\u001b[1;34m(parameters, cache, X, Y)\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mY\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mY\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0mA2\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mA2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m455\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 12\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mA2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 13\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mA2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mc:\\python27\\lib\\site-packages\\numpy\\core\\fromnumeric.pyc\u001b[0m in \u001b[0;36mreshape\u001b[1;34m(a, newshape, order)\u001b[0m\n\u001b[0;32m 230\u001b[0m [5, 6]])\n\u001b[0;32m 231\u001b[0m \"\"\"\n\u001b[1;32m--> 232\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0m_wrapfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'reshape'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnewshape\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0morder\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0morder\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 233\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 234\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;32mc:\\python27\\lib\\site-packages\\numpy\\core\\fromnumeric.pyc\u001b[0m in \u001b[0;36m_wrapfunc\u001b[1;34m(obj, method, *args, **kwds)\u001b[0m\n\u001b[0;32m 55\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_wrapfunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 56\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 57\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 58\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 59\u001b[0m \u001b[1;31m# An AttributeError occurs if the object does not have\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mValueError\u001b[0m: cannot reshape array of size 25992 into shape (455,1)" + ] + } + ], + "source": [ + "parameters = nn_model(test_set_x, test_set_y, n_h = 4, num_iterations = 10000, print_cost=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "shapes (114,1) and (114,455,2) not aligned: 1 (dim 1) != 455 (dim 1)", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mpredictions\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpredict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparameters\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtest_set_x\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;34m'Accuracy: %d'\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtest_set_y\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mpredictions\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mT\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mtest_set_y\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mpredictions\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mT\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtest_set_y\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msize\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;34m'%'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;31mValueError\u001b[0m: shapes (114,1) and (114,455,2) not aligned: 1 (dim 1) != 455 (dim 1)" + ] + } + ], + "source": [ + "predictions = predict(parameters, test_set_x)\n", + "print ('Accuracy: %d' % float((np.dot(test_set_y,predictions.T) + np.dot(1-test_set_y,1-predictions.T))/float(test_set_y.size)*100) + '%')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.14" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Breast_cancer_3.ipynb b/Breast_cancer_3.ipynb new file mode 100644 index 0000000..3823d45 --- /dev/null +++ b/Breast_cancer_3.ipynb @@ -0,0 +1,541 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cost after iteration 0: 30.843251\n", + "Cost after iteration 3: nan\n", + "Cost after iteration 6: nan\n", + "Cost after iteration 9: nan\n", + "Cost after iteration 12: nan\n", + "Cost after iteration 15: nan\n", + "Cost after iteration 18: nan\n", + "Cost after iteration 21: nan\n", + "Cost after iteration 24: nan\n", + "Cost after iteration 27: nan\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "c:\\python27\\lib\\site-packages\\ipykernel_launcher.py:271: RuntimeWarning: divide by zero encountered in log\n", + "c:\\python27\\lib\\site-packages\\ipykernel_launcher.py:353: RuntimeWarning: divide by zero encountered in divide\n", + "c:\\python27\\lib\\site-packages\\ipykernel_launcher.py:353: RuntimeWarning: invalid value encountered in divide\n", + "c:\\python27\\lib\\site-packages\\ipykernel_launcher.py:105: RuntimeWarning: invalid value encountered in multiply\n", + "c:\\python27\\lib\\site-packages\\ipykernel_launcher.py:60: RuntimeWarning: invalid value encountered in maximum\n", + "c:\\python27\\lib\\site-packages\\ipykernel_launcher.py:84: RuntimeWarning: invalid value encountered in less_equal\n" + ] + }, + { + "data": { + "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYwAAAEWCAYAAAB1xKBvAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4xLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvAOZPmwAAG9lJREFUeJzt3XmUXWWd7vHvQxIJc4IUGAiTNrY4\nEfQIuGy9iDSg7YCKtnNa5SK2XLvVvja2LBDQuxBnr5ertAq4GhQEbTEidnQxiAihIgkSBhkEyQ1D\nMSlRRBKe+8d+Sw7lqdSbpHadquT5rLVX7bP3++79e1NwntrD2Ue2iY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cbVq/YyvyfNG/2M8voUmuc3QfMp+leV+ZO79vVLYKeR9QMvAr7f7/8O\nMvV/ysMHYyo7CHiupMPK621o3nj/BCxy8x0Tw94v6bVlfufS7r41bPtvgG+6Oe1zt6RLgBcAvyvb\nXg4gaQnNI1uuAP4IfFXSD4AFPbY5Bxgasewc248BN0m6FXjGWo5rNC8Dng9cVQ6ANuPxB0D+qau+\nxcDflvmfAadLOgf4Tte27gF2rNhnbOASGDGVCfgftn/0hIXNtY7fj3h9IPBC23+QdDHNX/JjbXs0\n3d/hsBqYbnuVpH1o3qjfBBwFjPwK0odp3vy7jbyIOPzY8zHHNQYBZ9j+SI91j9oe3u9qyvuA7SMl\n7Qv8HbBE0jzb99H8Wz1cud/YgOUaRkwlDwFbdb3+EfBeSTOguUZQvrBmpG2AB0pYPAPYr2vdo8P9\nR7gU+PtyPWEAeAnNQ+V6UvOtiNvYvgD4Z2Bej2bXA381YtkbJG0i6Wk0XwF641qMa6TusfyE5rHa\n25dtbCtp1zV1lvQ021faPha4l8cf6f10mtN4sZHLEUZMJdcAqyQtpTn//wWa00G/KBeeh+j9VbAX\nAkdKuobmDfmKrnWnAtdI+oXtt3Yt/y7Nd5Espfmr/8O27yqB08tWwPckzaT56/4DPdpcCnxGkrr+\nwr8RuITmOsmRtv8o6auV4xrpCWORdAzwX5I2AR6leQT37Wvo/ylJe5T6f1LGDvBS4AcV+48NXG6r\njZhAkr5AcwH5x+XzDQtsn9vnskal5ju3LwH+xs1Xj8ZGLKekIibW/wI273cRa2EXmi/CSlhEjjAi\nIqJOjjAiIqJKAiMiIqokMCIiokoCIyIiqiQwIiKiyv8H0W1qtiwl/skAAAAASUVORK5CYII=\n", + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import sklearn\n", + "import numpy as np\n", + "import h5py\n", + "from dnn_utils_v2 import sigmoid, sigmoid_backward, relu, relu_backward\n", + "from sklearn.datasets import load_breast_cancer\n", + "from testCases_v2 import *\n", + "import matplotlib.pyplot as plt\n", + "\n", + "data = load_breast_cancer()\n", + "\n", + "feature_names = data['feature_names']\n", + "features = data['data']\n", + "label_names = data['target_names']\n", + "labels = data['target']\n", + "\n", + "from sklearn.model_selection import train_test_split\n", + "\n", + "train, test, train_labels, test_labels = train_test_split(features, labels, test_size=0.20, random_state=42)\n", + "\n", + "train_set_x = train.T\n", + "train_set_y = train_labels.T\n", + "test_set_x = test.T\n", + "test_set_y = test_labels.T\n", + "\n", + "import numpy as np\n", + "X_train = np.asarray(train_set_x)\n", + "Y_train = np.reshape(train_set_y,[455,1])\n", + "X_test = np.asarray(test_set_x)\n", + "Y_test = np.reshape(test_set_y,[114,1])\n", + "\n", + "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", + "\n", + "def 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", + "\n", + "def 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", + "\n", + "def 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\n", + "\n", + "def 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 \n", + "\n", + "\n", + "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(1)\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]) / np.sqrt(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\n", + "\n", + "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", + " Z = W.dot(A) + b\n", + " \n", + " assert(Z.shape == (W.shape[0], A.shape[1]))\n", + " cache = (A, W, b)\n", + " \n", + " return Z, cache\n", + "\n", + "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", + " 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\n", + "\n", + "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 = \"sigmoid\")\n", + " caches.append(cache)\n", + " \n", + " assert(AL.shape == (1,X.shape[1]))\n", + " \n", + " return AL, caches\n", + "\n", + "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.dot(Y,np.log(AL).T) - np.dot(1-Y, np.log(1-AL).T))\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\n", + "\n", + "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", + "\n", + "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\n", + "\n", + "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\" (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) - np.divide(1 - Y, 1 - AL))\n", + " \n", + " # Lth layer (SIGMOID -> LINEAR) gradients. Inputs: \"AL, Y, caches\". Outputs: \"grads[\"dAL\"], grads[\"dWL\"], grads[\"dbL\"]\n", + " current_cache = caches[L-1]\n", + " grads[\"dA\" + str(L)], grads[\"dW\" + str(L)], grads[\"db\" + str(L)] = linear_activation_backward(dAL, current_cache, activation = \"sigmoid\")\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", + "\n", + "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", + " \n", + " return parameters\n", + "\n", + "def L_layer_model(X, Y, layers_dims, learning_rate = 0.075, num_iterations = 30, 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 % 3 == 0:\n", + " print (\"Cost after iteration %i: %f\" %(i, cost))\n", + " if print_cost and i % 3 == 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\n", + "\n", + "layers_dims = [30, 20, 15, 10, 1]\n", + "parameters = L_layer_model(X_train, Y_train.T, layers_dims, num_iterations = 30, print_cost = True)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.14" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}