This commit is contained in:
Morten Hjorth-Jensen
2023-11-08 08:44:50 +01:00
parent a97f111286
commit 3ecd9f78ea
3 changed files with 2172 additions and 453 deletions
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
+89 -225
View File
@@ -3,9 +3,7 @@
{
"cell_type": "markdown",
"id": "31a10e2b",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"<!-- HTML file automatically generated from DocOnce source (https://github.com/doconce/doconce/)\n",
"doconce format html week45.do.txt --no_mako -->\n",
@@ -15,9 +13,7 @@
{
"cell_type": "markdown",
"id": "019daa83",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"# Week 45, Recurrent Neural Networks\n",
"**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n",
@@ -28,9 +24,7 @@
{
"cell_type": "markdown",
"id": "6ecf1c2b",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Plan for week 45\n",
"\n",
@@ -68,9 +62,7 @@
{
"cell_type": "markdown",
"id": "d80a4db8",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Material for the lab sessions, additional ways to present classification results and other practicalities"
]
@@ -78,9 +70,7 @@
{
"cell_type": "markdown",
"id": "1ded5c92",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Searching for Optimal Regularization Parameters $\\lambda$\n",
"\n",
@@ -97,10 +87,7 @@
"cell_type": "code",
"execution_count": 1,
"id": "6dea0513",
"metadata": {
"collapsed": false,
"editable": true
},
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
@@ -154,9 +141,7 @@
{
"cell_type": "markdown",
"id": "440bf579",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"Here we have performed a rather data greedy calculation as function of the regularization parameter $\\lambda$. There is no resampling here. The latter can easily be added by employing the function **RidgeCV** instead of just calling the **Ridge** function. For **RidgeCV** we need to pass the array of $\\lambda$ values.\n",
"By inspecting the figure we can in turn determine which is the optimal regularization parameter.\n",
@@ -166,9 +151,7 @@
{
"cell_type": "markdown",
"id": "683b1da2",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Grid Search\n",
"\n",
@@ -181,10 +164,7 @@
"cell_type": "code",
"execution_count": 2,
"id": "38be79e0",
"metadata": {
"collapsed": false,
"editable": true
},
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
@@ -234,9 +214,7 @@
{
"cell_type": "markdown",
"id": "081fbe5c",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"By default the grid search function includes cross validation with\n",
"five folds. The [Scikit-Learn\n",
@@ -249,9 +227,7 @@
{
"cell_type": "markdown",
"id": "a9798c07",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Randomized Grid Search\n",
"\n",
@@ -269,10 +245,7 @@
"cell_type": "code",
"execution_count": 3,
"id": "0a7e4e2e",
"metadata": {
"collapsed": false,
"editable": true
},
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
@@ -323,9 +296,7 @@
{
"cell_type": "markdown",
"id": "dcd07cb8",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Wisconsin Cancer Data\n",
"\n",
@@ -338,10 +309,7 @@
"cell_type": "code",
"execution_count": 4,
"id": "65061d95",
"metadata": {
"collapsed": false,
"editable": true
},
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
@@ -365,9 +333,7 @@
{
"cell_type": "markdown",
"id": "50492453",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Using the correlation matrix\n",
"\n",
@@ -379,10 +345,7 @@
"cell_type": "code",
"execution_count": 5,
"id": "08a2ab17",
"metadata": {
"collapsed": false,
"editable": true
},
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
@@ -424,9 +387,7 @@
{
"cell_type": "markdown",
"id": "5408b7dd",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Discussing the correlation data\n",
"\n",
@@ -449,10 +410,7 @@
"cell_type": "code",
"execution_count": 6,
"id": "76c3d259",
"metadata": {
"collapsed": false,
"editable": true
},
"metadata": {},
"outputs": [],
"source": [
"cancerpd = pd.DataFrame(cancer.data, columns=cancer.feature_names)"
@@ -461,9 +419,7 @@
{
"cell_type": "markdown",
"id": "83903231",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"and then"
]
@@ -472,10 +428,7 @@
"cell_type": "code",
"execution_count": 7,
"id": "58ccd6c3",
"metadata": {
"collapsed": false,
"editable": true
},
"metadata": {},
"outputs": [],
"source": [
"correlation_matrix = cancerpd.corr().round(1)"
@@ -484,9 +437,7 @@
{
"cell_type": "markdown",
"id": "adda54cb",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"Diagonalizing this matrix we can in turn say something about which\n",
"features are of relevance and which are not. This leads us to\n",
@@ -497,9 +448,7 @@
{
"cell_type": "markdown",
"id": "8799c034",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Other ways of presenting a classification problem\n",
"\n",
@@ -530,9 +479,7 @@
{
"cell_type": "markdown",
"id": "dfbe0571",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Combinations of classification results\n",
"\n",
@@ -544,9 +491,7 @@
{
"cell_type": "markdown",
"id": "65f7c63d",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"$$\n",
"{\\displaystyle \\mathrm {TPR} ={\\frac {\\mathrm {TP} }{\\mathrm {P} }}={\\frac {\\mathrm {TP} }{\\mathrm {TP} +\\mathrm {FN} }}=1-\\mathrm {FNR} }\n",
@@ -556,9 +501,7 @@
{
"cell_type": "markdown",
"id": "2e674d10",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"The $TPR$ defines how many correct positive results occur among all positive samples available during the test\n",
"\n",
@@ -568,9 +511,7 @@
{
"cell_type": "markdown",
"id": "65577837",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"$$\n",
"{\\displaystyle \\mathrm {FNR} ={\\frac {\\mathrm {FN} }{\\mathrm {P} }}={\\frac {\\mathrm {FN} }{\\mathrm {FN} +\\mathrm {TP} }} }\n",
@@ -580,9 +521,7 @@
{
"cell_type": "markdown",
"id": "e5e8802d",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"**Specificity, selectivity or true negative rate $TNR$. It is the probability of a negative test result, conditioned on the individual truly being negative.**"
]
@@ -590,9 +529,7 @@
{
"cell_type": "markdown",
"id": "07c61591",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"$$\n",
"{\\displaystyle \\mathrm {TNR} ={\\frac {\\mathrm {TN} }{\\mathrm {N} }}={\\frac {\\mathrm {TN} }{\\mathrm {TN} +\\mathrm {FP} }}=1-\\mathrm {FPR} }\n",
@@ -602,9 +539,7 @@
{
"cell_type": "markdown",
"id": "b3f61bb3",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"with the fall-out false positive rate"
]
@@ -612,9 +547,7 @@
{
"cell_type": "markdown",
"id": "6544105e",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"$$\n",
"{\\displaystyle \\mathrm {FPR} ={\\frac {\\mathrm {FP} }{\\mathrm {N} }}={\\frac {\\mathrm {FP} }{\\mathrm {FP} +\\mathrm {TN} }}=1-\\mathrm {TNR} }\n",
@@ -624,9 +557,7 @@
{
"cell_type": "markdown",
"id": "0b571e85",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"The $FPR$ defines how many incorrect positive results occur among\n",
"all negative samples available during the test."
@@ -635,9 +566,7 @@
{
"cell_type": "markdown",
"id": "17d6872a",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Positive and negative prediction values\n",
"\n",
@@ -654,9 +583,7 @@
{
"cell_type": "markdown",
"id": "122f7e6c",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"$$\n",
"{\\displaystyle \\mathrm {PPV} ={\\frac {\\mathrm {TP} }{\\mathrm {TP} +\\mathrm {FP} }}=1-\\mathrm {FDR} }\n",
@@ -666,9 +593,7 @@
{
"cell_type": "markdown",
"id": "f44a5e78",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"**Negative predictive value $NPV$.**"
]
@@ -676,9 +601,7 @@
{
"cell_type": "markdown",
"id": "86fa9670",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"$$\n",
"{\\displaystyle \\mathrm {NPV} ={\\frac {\\mathrm {TN} }{\\mathrm {TN} +\\mathrm {FN} }}=1-\\mathrm {FOR} }\n",
@@ -688,9 +611,7 @@
{
"cell_type": "markdown",
"id": "a30e358d",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Other quantities\n",
"\n",
@@ -700,9 +621,7 @@
{
"cell_type": "markdown",
"id": "3f0de6de",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"$$\n",
"{\\displaystyle \\mathrm {FDR} ={\\frac {\\mathrm {FP} }{\\mathrm {FP} +\\mathrm {TP} }}=1-\\mathrm {PPV} }\n",
@@ -712,9 +631,7 @@
{
"cell_type": "markdown",
"id": "0b4cc93f",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"**False omission rate $FOR$.**"
]
@@ -722,9 +639,7 @@
{
"cell_type": "markdown",
"id": "4866ec0a",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"$$\n",
"{\\displaystyle \\mathrm {FOR} ={\\frac {\\mathrm {FN} }{\\mathrm {FN} +\\mathrm {TN} }}=1-\\mathrm {NPV} }\n",
@@ -734,9 +649,7 @@
{
"cell_type": "markdown",
"id": "9b1c8c45",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## $F_1$ score\n",
"\n",
@@ -762,9 +675,7 @@
{
"cell_type": "markdown",
"id": "0e651a1f",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"$$\n",
"{\\displaystyle \\mathrm {F} _{1}=2\\times {\\frac {\\mathrm {PPV} \\times \\mathrm {TPR} }{\\mathrm {PPV} +\\mathrm {TPR} }}={\\frac {2\\mathrm {TP} }{2\\mathrm {TP} +\\mathrm {FP} +\\mathrm {FN} }}}\n",
@@ -774,9 +685,7 @@
{
"cell_type": "markdown",
"id": "d83efe41",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## ROC curve\n",
"\n",
@@ -799,9 +708,7 @@
{
"cell_type": "markdown",
"id": "d088215b",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Cumulative gain curve\n",
"\n",
@@ -817,9 +724,7 @@
{
"cell_type": "markdown",
"id": "5c0bcd1f",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Other measures in classification studies: Cancer Data again"
]
@@ -828,10 +733,7 @@
"cell_type": "code",
"execution_count": 8,
"id": "2a6f80df",
"metadata": {
"collapsed": false,
"editable": true
},
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
@@ -871,9 +773,7 @@
{
"cell_type": "markdown",
"id": "58e5b521",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Material for Lecture Thursday November 9"
]
@@ -881,9 +781,7 @@
{
"cell_type": "markdown",
"id": "4ebe5a91",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Recurrent neural networks (RNNs): Overarching view\n",
"\n",
@@ -908,9 +806,7 @@
{
"cell_type": "markdown",
"id": "182f425e",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## A simple example"
]
@@ -919,10 +815,7 @@
"cell_type": "code",
"execution_count": 9,
"id": "8b3ca785",
"metadata": {
"collapsed": false,
"editable": true
},
"metadata": {},
"outputs": [],
"source": [
"# Start importing packages\n",
@@ -992,9 +885,7 @@
{
"cell_type": "markdown",
"id": "b0c12b4c",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"### RNNs\n",
"\n",
@@ -1012,9 +903,7 @@
{
"cell_type": "markdown",
"id": "d81020f6",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Basic layout\n",
"\n",
@@ -1028,9 +917,7 @@
{
"cell_type": "markdown",
"id": "2d7453c9",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"### We need to specify the initial activity state of all the hidden and output units\n",
"\n",
@@ -1050,9 +937,7 @@
{
"cell_type": "markdown",
"id": "fc4a082e",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"### We can specify inputs in several ways\n",
"\n",
@@ -1068,9 +953,7 @@
{
"cell_type": "markdown",
"id": "c1106ad9",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"### We can specify targets in several ways\n",
"\n",
@@ -1114,9 +997,7 @@
{
"cell_type": "markdown",
"id": "b139ef7b",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"### Backpropagation through time\n",
"\n",
@@ -1135,9 +1016,7 @@
{
"cell_type": "markdown",
"id": "70c95078",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"### The backward pass is linear\n",
"\n",
@@ -1196,9 +1075,7 @@
{
"cell_type": "markdown",
"id": "2ca24031",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## The problem of exploding or vanishing gradients\n",
"* What happens to the magnitude of the gradients as we backpropagate through many layers?\n",
@@ -1221,9 +1098,7 @@
{
"cell_type": "markdown",
"id": "93648979",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Four effective ways to learn an RNN\n",
"1. Long Short Term Memory Make the RNN out of little modules that are designed to remember values for a long time.\n",
@@ -1240,9 +1115,7 @@
{
"cell_type": "markdown",
"id": "b8806dcd",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"### Long Short Term Memory (LSTM)\n",
"\n",
@@ -1264,9 +1137,7 @@
{
"cell_type": "markdown",
"id": "b0edad40",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"### Implementing a memory cell in a neural network\n",
"\n",
@@ -1346,9 +1217,7 @@
{
"cell_type": "markdown",
"id": "58fd18f9",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## An extrapolation example\n",
"\n",
@@ -1362,10 +1231,7 @@
"cell_type": "code",
"execution_count": 10,
"id": "a604caa5",
"metadata": {
"collapsed": false,
"editable": true
},
"metadata": {},
"outputs": [],
"source": [
"\n",
@@ -1401,9 +1267,7 @@
{
"cell_type": "markdown",
"id": "545336c9",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Formatting the Data\n",
"\n",
@@ -1445,10 +1309,7 @@
"cell_type": "code",
"execution_count": 11,
"id": "3ef3ed13",
"metadata": {
"collapsed": false,
"editable": true
},
"metadata": {},
"outputs": [],
"source": [
"# FORMAT_DATA\n",
@@ -1528,9 +1389,7 @@
{
"cell_type": "markdown",
"id": "8a8b29b8",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Predicting New Points With A Trained Recurrent Neural Network"
]
@@ -1539,10 +1398,7 @@
"cell_type": "code",
"execution_count": 12,
"id": "d840beea",
"metadata": {
"collapsed": false,
"editable": true
},
"metadata": {},
"outputs": [],
"source": [
"def test_rnn (x1, y_test, plot_min, plot_max):\n",
@@ -1641,9 +1497,7 @@
{
"cell_type": "markdown",
"id": "2e5b339f",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Other Things to Try\n",
"\n",
@@ -1663,10 +1517,7 @@
"cell_type": "code",
"execution_count": 13,
"id": "370e799d",
"metadata": {
"collapsed": false,
"editable": true
},
"metadata": {},
"outputs": [],
"source": [
"def rnn_2layers(length_of_sequences, batch_size = None, stateful = False):\n",
@@ -1760,9 +1611,7 @@
{
"cell_type": "markdown",
"id": "d8f62fc4",
"metadata": {
"editable": true
},
"metadata": {},
"source": [
"## Other Types of Recurrent Neural Networks\n",
"\n",
@@ -1786,10 +1635,7 @@
"cell_type": "code",
"execution_count": 14,
"id": "4ea36ea9",
"metadata": {
"collapsed": false,
"editable": true
},
"metadata": {},
"outputs": [],
"source": [
"def lstm_2layers(length_of_sequences, batch_size = None, stateful = False):\n",
@@ -1985,7 +1831,25 @@
]
}
],
"metadata": {},
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}