Files
CVML-MachineLearning/course/4 Deep Learning Intro.ipynb
Sem van der Hoeven a5418d7b02 [ADD] opdr 5.2
2021-06-07 15:43:41 +02:00

565 lines
14 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Deep Learning Intro"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Shallow and Deep Networks"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.datasets import make_moons\n",
"\n",
"X, y = make_moons(n_samples=1000, noise=0.1, random_state=0)\n",
"plt.plot(X[y==0, 0], X[y==0, 1], 'ob', alpha=0.5)\n",
"plt.plot(X[y==1, 0], X[y==1, 1], 'xr', alpha=0.5)\n",
"plt.legend(['0', '1'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X_train, X_test, y_train, y_test = train_test_split(X, y,\n",
" test_size=0.3,\n",
" random_state=42)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.models import Sequential\n",
"from tensorflow.keras.layers import Dense\n",
"from tensorflow.keras.optimizers import SGD, Adam"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Shallow Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = Sequential()\n",
"model.add(Dense(1, input_shape=(2,), activation='sigmoid'))\n",
"model.compile(Adam(learning_rate=0.05), 'binary_crossentropy', metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.fit(X_train, y_train, epochs=200, verbose=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"results = model.evaluate(X_test, y_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"The Accuracy score on the Train set is:\\t{:0.3f}\".format(results[1]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def plot_decision_boundary(model, X, y):\n",
" amin, bmin = X.min(axis=0) - 0.1\n",
" amax, bmax = X.max(axis=0) + 0.1\n",
" hticks = np.linspace(amin, amax, 101)\n",
" vticks = np.linspace(bmin, bmax, 101)\n",
" \n",
" aa, bb = np.meshgrid(hticks, vticks)\n",
" ab = np.c_[aa.ravel(), bb.ravel()]\n",
" \n",
" c = model.predict(ab)\n",
" cc = c.reshape(aa.shape)\n",
"\n",
" plt.figure(figsize=(12, 8))\n",
" plt.contourf(aa, bb, cc, cmap='bwr', alpha=0.2)\n",
" plt.plot(X[y==0, 0], X[y==0, 1], 'ob', alpha=0.5)\n",
" plt.plot(X[y==1, 0], X[y==1, 1], 'xr', alpha=0.5)\n",
" plt.legend(['0', '1'])\n",
" \n",
"plot_decision_boundary(model, X, y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Deep model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = Sequential()\n",
"model.add(Dense(4, input_shape=(2,), activation='tanh'))\n",
"model.add(Dense(2, activation='tanh'))\n",
"model.add(Dense(1, activation='sigmoid'))\n",
"model.compile(Adam(learning_rate=0.05), 'binary_crossentropy', metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.fit(X_train, y_train, epochs=100, verbose=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.evaluate(X_test, y_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.metrics import accuracy_score, confusion_matrix"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y_train_pred = model.predict_classes(X_train)\n",
"y_test_pred = model.predict_classes(X_test)\n",
"\n",
"print(\"The Accuracy score on the Train set is:\\t{:0.3f}\".format(accuracy_score(y_train, y_train_pred)))\n",
"print(\"The Accuracy score on the Test set is:\\t{:0.3f}\".format(accuracy_score(y_test, y_test_pred)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plot_decision_boundary(model, X, y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Multiclass classification\n",
"\n",
"### The Iris dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('../data/iris.csv')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"import seaborn as sns\n",
"sns.pairplot(df, hue=\"species\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X = df.drop('species', axis=1)\n",
"X.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"target_names = df['species'].unique()\n",
"target_names"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"target_dict = {n:i for i, n in enumerate(target_names)}\n",
"target_dict"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y= df['species'].map(target_dict)\n",
"y.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow.keras.utils import to_categorical"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y_cat = to_categorical(y)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y_cat[:10]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X_train, X_test, y_train, y_test = train_test_split(X.values, y_cat,\n",
" test_size=0.2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = Sequential()\n",
"model.add(Dense(3, input_shape=(4,), activation='softmax'))\n",
"model.compile(Adam(learning_rate=0.1),\n",
" loss='categorical_crossentropy',\n",
" metrics=['accuracy'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.fit(X_train, y_train, epochs=20, validation_split=0.1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y_pred = model.predict(X_test)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y_pred[:5]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y_test_class = np.argmax(y_test, axis=1)\n",
"y_pred_class = np.argmax(y_pred, axis=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.metrics import classification_report"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(classification_report(y_test_class, y_pred_class))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"confusion_matrix(y_test_class, y_pred_class)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 1"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"The [Pima Indians dataset](https://archive.ics.uci.edu/ml/datasets/diabetes) is a very famous dataset distributed by UCI and originally collected from the National Institute of Diabetes and Digestive and Kidney Diseases. It contains data from clinical exams for women age 21 and above of Pima indian origins. The objective is to predict based on diagnostic measurements whether a patient has diabetes.\n",
"\n",
"It has the following features:\n",
"\n",
"- Pregnancies: Number of times pregnant\n",
"- Glucose: Plasma glucose concentration a 2 hours in an oral glucose tolerance test\n",
"- BloodPressure: Diastolic blood pressure (mm Hg)\n",
"- SkinThickness: Triceps skin fold thickness (mm)\n",
"- Insulin: 2-Hour serum insulin (mu U/ml)\n",
"- BMI: Body mass index (weight in kg/(height in m)^2)\n",
"- DiabetesPedigreeFunction: Diabetes pedigree function\n",
"- Age: Age (years)\n",
"\n",
"The last colum is the outcome, and it is a binary variable.\n",
"\n",
"In this first exercise we will explore it through the following steps:\n",
"\n",
"1. Load the ..data/diabetes.csv dataset, use pandas to explore the range of each feature\n",
"- For each feature draw a histogram. Bonus points if you draw all the histograms in the same figure.\n",
"- Explore correlations of features with the outcome column. You can do this in several ways, for example using the `sns.pairplot` we used above or drawing a heatmap of the correlations.\n",
"- Do features need standardization? If so what stardardization technique will you use? MinMax? Standard?\n",
"- Prepare your final `X` and `y` variables to be used by a ML model. Make sure you define your target variable well. Will you need dummy columns?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 2"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"Build a fully connected NN model that predicts diabetes. Follow these steps:\n",
"\n",
"1. Split your data in a train/test with a test size of 20% and a `random_state = 22`\n",
"- define a sequential model with at least one inner layer. You will have to make choices for the following things:\n",
" - what is the size of the input?\n",
" - how many nodes will you use in each layer?\n",
" - what is the size of the output?\n",
" - what activation functions will you use in the inner layers?\n",
" - what activation function will you use at output?\n",
" - what loss function will you use?\n",
" - what optimizer will you use?\n",
"- fit your model on the training set, using a validation_split of 0.1\n",
"- test your trained model on the test data from the train/test split\n",
"- check the accuracy score, the confusion matrix and the classification report"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 3\n",
"Compare your work with the results presented in [this notebook](https://www.kaggle.com/futurist/d/uciml/pima-indians-diabetes-database/pima-data-visualisation-and-machine-learning). Are your Neural Network results better or worse than the results obtained by traditional Machine Learning techniques?\n",
"\n",
"- Try training a Support Vector Machine or a Random Forest model on the exact same train/test split. Is the performance better or worse?\n",
"- Try restricting your features to only 4 features like in the suggested notebook. How does model performance change?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 4\n",
"\n",
"[Tensorflow playground](http://playground.tensorflow.org/) is a web based neural network demo. It is really useful to develop an intuition about what happens when you change architecture, activation function or other parameters. Try playing with it for a few minutes. You don't need do understand the meaning of every knob and button in the page, just get a sense for what happens if you change something. In the next chapter we'll explore these things in more detail.\n"
]
},
{
"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.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}