-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlab4.html
575 lines (487 loc) · 18.9 KB
/
lab4.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Lab 4</title>
<script src="site_libs/header-attrs-2.4/header-attrs.js"></script>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/journal.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/default.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<link href="site_libs/font-awesome-5.1.0/css/all.css" rel="stylesheet" />
<link href="site_libs/font-awesome-5.1.0/css/v4-shims.css" rel="stylesheet" />
<script src="highlight-custom.js"></script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
<link rel="stylesheet" href="website-custom.css" type="text/css" />
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 61px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 66px;
margin-top: -66px;
}
.section h2 {
padding-top: 66px;
margin-top: -66px;
}
.section h3 {
padding-top: 66px;
margin-top: -66px;
}
.section h4 {
padding-top: 66px;
margin-top: -66px;
}
.section h5 {
padding-top: 66px;
margin-top: -66px;
}
.section h6 {
padding-top: 66px;
margin-top: -66px;
}
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #ffffff;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
background: white;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
@media print {
.toc-content {
/* see https://github.com/w3c/csswg-drafts/issues/4434 */
float: right;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">EDLD 654</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="schedule.html">Slides, Readings, & Schedule</a>
</li>
<li>
<a href="assignments.html">Assignments</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="site-syllabus.html">Syllabus</a>
</li>
<li>
<a href="https://www.kaggle.com/c/edld-654-fall-2020/overview">Kaggle</a>
</li>
<li>
<a href="talapas-pkg-install.html">
<span class="fa fa-server"></span>
</a>
</li>
<li>
<a href="https://github.com/uo-datasci-specialization/c4-ml-fall-2020">
<span class="fa fa-github"></span>
</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Lab 4</h1>
<h3 class="subtitle">k-nearest neighbor</h3>
<h4 class="date">Assigned 11/4/20, Due 11/11/20</h4>
</div>
<p>The goal of this lab is to apply and tune <em>K</em>-nearest neighbor models and explore non-regular grids while using HPC via <a href="https://hpcrcf.atlassian.net/wiki/spaces/TCP/overview?homepageId=6761503">talapas</a>.</p>
<p>Please complete this project using a GitHub repo. You will complete this lab by submitting a link to your repo. In your repo, please have <code>data</code>, <code>models</code>, and <code>plots</code> folders, as well as a <code>screenshots</code> folder. I will be asking you to take screenshots of your work at multiple points.</p>
<p>When you run the models on talapas, you can decide how much of the data to use. You are free to even stick with half of 1%. The idea is to get you practice working with talapas, but <span class="math inline">\(K\)</span>NN models are just really inefficient and I’d like you to be able to work mostly interactively. As I (Daniel) worked through the lab with the full dataset, I found that the preliminary run took about 10-12 minutes, the tuning took a very long time, and the final fit was very fast. Note that if you sample, say, 50% of the data, the time it takes will not directly translate to 50% of the time for the full data, but it will obviously help.</p>
<div id="data" class="section level3">
<h3>1. Data</h3>
<ul>
<li>Connect to talapas. Note, for this lab you will also need the “kknn” and “doParallel” packages. Install them after you connect. (Type <code>R</code> and hit return to connect to R, install like normal, saving to a local library, then type <code>q()</code> to exit R).</li>
<li>Navigate to your folder in <code>edld654</code></li>
<li>Create new <code>data</code> and <code>models</code> folders</li>
<li>Move <code>train.csv</code> into the <code>data</code> folder</li>
<li>Take a screenshot of your data successfully transferred to talapas and place it in your screenshots folder. Note - if you’re working in a group, each member of the group should do this, and each member of the group should run analyses on talapas. You can have separate folders for each group member, or just name the screenshots with the group members name.</li>
</ul>
<p>If you have trouble with this step, you will not be able to complete subsequent steps in the lab, so please ask for help.</p>
</div>
<div id="a-simple-knn-model" class="section level3">
<h3>2. A simple KNN model</h3>
<p>On your local (not on talapas), create a new R script. Do the following</p>
<ul>
<li>Load the <em>tidyverse</em>, and <em>tidymodels</em></li>
<li>Add the following to prepare for parallel processing</li>
</ul>
<pre class="r"><code>all_cores <- parallel::detectCores(logical = FALSE)
library(doParallel)
cl <- makePSOCKcluster(all_cores)
registerDoParallel(cl)
foreach::getDoParWorkers()
clusterEvalQ(cl, {library(tidymodels)})</code></pre>
<ul>
<li>Read in <code>train.csv</code> from the <code>data</code> folder using a relative path (not <code>here::here</code>).</li>
<li>Use <code>dplyr::sample_frac</code> or <code>dplyr::slice_sample</code> (basically equivalent) to randomly select a proportion of 0.005 of all rows (i.e., half of one percent). This is so you can work with the models more easily locally, then you’ll comment this part out when you go to talapas.</li>
<li>Create your initial split object, pull the training data from it, and use the training data to create a <span class="math inline">\(k\)</span>-fold cross-validation data object.</li>
<li>Create a basic recipe that you will work with for the lab. Use <strong>classification</strong> as the outcome (not <em>score</em>) and <strong>do not model all variables</strong>. It will be too computationally intensive. Just select of few variables (more than 3, less than 10).
<ul>
<li><strong>Hint</strong>: When developing your recipe, make sure you exclude your outcome with things like <code>step_novel()</code>, <code>step_dummy()</code>, or any other operation you apply to <code>all_nominal()</code>, unless of course you want that operation to be conducted on your outcome too.</li>
</ul></li>
<li><strong>optional</strong> everything up to this point will be needed for all model fits. You can save this as a stand-alone R file and <code>source</code> it in your other scripts, or you can just copy and past it into each model fit script. We’ll be writing separate scripts for each model fit.</li>
<li>Create a KNN model object, setting the appropriate mode and engine - pick a random <span class="math inline">\(k\)</span> for now.</li>
<li>Use <code>fit_resamples</code> to fit the model. Save these resamples to an object.</li>
<li>Save the fit resamples object as a <code>.Rds</code> file in the models folder using a relative path.</li>
<li>Use session -> restart R, and run the script locally to ensure everything works.</li>
</ul>
</div>
<div id="preliminary-run-on-talapas" class="section level3">
<h3>3. Preliminary run on talapas</h3>
<ul>
<li>Create a <code>.srun</code> script
<ul>
<li>Ask for something like 1 node with 8 CPUs per task with 16 gigs of memory per CPU. You can ask for more, but you might get waitlisted a bit. You can also try different combinations to see what works fastest for you, but the above specs are pretty decent.</li>
</ul></li>
<li>Move all the files over to talapas in your folder
<ul>
<li>Data file should be there already</li>
<li>Comment out the sampling of rows (or change the proportion) and move the script over</li>
<li>Move the <code>.srun</code> file over</li>
</ul></li>
<li>Run the the job through your terminal connection with <code>sbatch my-batch-script.srun</code>, replacing <code>my-batch-script</code> with the name of your <code>.srun</code> file.</li>
<li>Check the status of your job with <code>squeue -u duckid</code>, replacing <code>duckid</code> with your duckid. Take a screenshot of the status and place it in the screenshots folder.</li>
<li>Move the saved file back to your local.</li>
<li>Report on the AUC for this model. You can report these either through an Rmd file or by just writing out a csv.</li>
</ul>
</div>
<div id="model-tuning" class="section level3">
<h3>4. Model tuning</h3>
<p>Create a new R script to conduct modeling tuning. You will need all the same pieces up to the <strong>optional</strong> part above (i.e., everything up to where you define the model). Create a non-regular, space-filling design grid using <code>grid_max_entropy</code> and 25 parameter values.</p>
<ul>
<li>Tune the range of <code>neighbors</code> hyperparameter for <code>c(1, 20)</code> along with the <code>dist_power</code> hyperparameter.</li>
<li>On your local, produce a <code>ggplot</code> plot to show a graphical representation of your non-regular grid. Save this in your “plots” folder. Note - you only need one of these for your group, not for each member. Comment this code out when you’re done so it doesn’t run when you fit the model with talapas.</li>
<li>Fit your tuned <span class="math inline">\(K\)</span>NN model (using only the small proportion of rows again) to your <code>resamples</code>, using your specified <code>recipe</code> and non-regular <code>grid</code>.</li>
<li>Once you are certain the script is working as you expect, save it and move it to talapas. Modify or create a new <code>.srun</code> script. Run the tuning script with the full data or the sample of rows you have chosen, saving the model tuning object into the models folder. Make sure to save it with a different name than your previous model. Take a screenshot of your models folder on talapas showing the two fitted models and place it in your screenshots folder.</li>
<li>Move the tuned model object back to your local and use it to report the top 5 tuned parameter models with the best AUC estimates. You can again report these either through an Rmd file (using the same one as before) or by just writing out a csv.</li>
</ul>
</div>
<div id="final-fit" class="section level3">
<h3>5. Final fit</h3>
<p>Conduct your final fit. Do this first on your local using the small proportion of rows, then replicate it on talapas. Again, please make sure to save the final fit as a new model object and take a screenshot showing your models folder after it has saved (the folder should have three model objects). Use:</p>
<ul>
<li><code>select_best</code> to select your best tuned model.</li>
<li><code>finalize_model</code> to finalize your model using your best tuned model.</li>
<li><code>finalize_recipe</code> to finalize your recipe using your best tuned model (in this case, we didn’t tune anything in our recipe, but it’s a good habit to get into).</li>
<li><code>last_fit</code> to run your last fit with your <code>finalized_model</code> and <code>finalized_recipe</code>on your initial data split. When working on talapas, this is the model object you should save and transfer to your local.</li>
<li><code>collect_metrics</code> from your final results. Similar to your model tuning, you will report these on the model run on talapas. You can report these in the same Rmd as the preliminary fit and tuning, or in a third CSV that you write to your local.</li>
</ul>
</div>
<div id="submit" class="section level3">
<h3>6. Submit</h3>
<p>Please submit your lab on canvas by pasting a link to your GitHub repo.</p>
</div>
<p>
<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/">
<img alt="Creative Commons License" style="border-width:0" src="by-nc.png" width="65"/></a>
<link rel="stylesheet" href="website-custom.css" type="text/css" />
</p>
<script src="highlight-custom.js"></script>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
$(document).ready(function () {
$('.tabset-dropdown > .nav-tabs > li').click(function () {
$(this).parent().toggleClass('nav-tabs-open')
});
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_');
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>