Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions _lectures/12-deep-gps-II.html
Original file line number Diff line number Diff line change
Expand Up @@ -1007,21 +1007,23 @@ <h2 id="subsample-of-the-mnist-data">Subsample of the MNIST Data</h2>
<p>The data is a modified version of a data set from the US National Institute of Standards and Technology (<a href="https://www.nist.gov/">NIST</a>.</p>
<p>First load in the MNIST data set from scikit learn. This can take a little while because it’s large to download.</p>
<div class="sourceCode" id="cb66"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb66-1"><a href="#cb66-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> sklearn.datasets <span class="im">import</span> fetch_openml</span></code></pre></div>
<div class="sourceCode" id="cb67"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb67-1"><a href="#cb67-1" aria-hidden="true" tabindex="-1"></a>mnist <span class="op">=</span> fetch_openml(<span class="st">&#39;mnist_784&#39;</span>)</span></code></pre></div>
<div class="sourceCode" id="cb67"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb67-1"><a href="#cb67-1" aria-hidden="true" tabindex="-1"></a>mnist <span class="op">=</span> fetch_openml(<span class="st">&#39;mnist_784&#39;</span>, version<span class="op">=</span><span class="dv">1</span>, as_frame<span class="op">=</span><span class="va">False</span>, parser<span class="op">=</span><span class="st">&#39;liac-arff&#39;</span>)</span></code></pre></div>
<p>Sub-sample the dataset to make the training faster.</p>
<div class="sourceCode" id="cb68"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb68-1"><a href="#cb68-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> numpy <span class="im">as</span> np</span></code></pre></div>
<div class="sourceCode" id="cb69"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb69-1"><a href="#cb69-1" aria-hidden="true" tabindex="-1"></a>np.random.seed(<span class="dv">0</span>)</span>
<span id="cb69-2"><a href="#cb69-2" aria-hidden="true" tabindex="-1"></a>digits <span class="op">=</span> [<span class="dv">0</span>,<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>]</span>
<span id="cb69-3"><a href="#cb69-3" aria-hidden="true" tabindex="-1"></a>N_per_digit <span class="op">=</span> <span class="dv">100</span></span>
<span id="cb69-4"><a href="#cb69-4" aria-hidden="true" tabindex="-1"></a>Y <span class="op">=</span> []</span>
<span id="cb69-5"><a href="#cb69-5" aria-hidden="true" tabindex="-1"></a>labels <span class="op">=</span> []</span>
<span id="cb69-6"><a href="#cb69-6" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> d <span class="kw">in</span> digits:</span>
<span id="cb69-7"><a href="#cb69-7" aria-hidden="true" tabindex="-1"></a> imgs <span class="op">=</span> mnist[<span class="st">&#39;data&#39;</span>][mnist[<span class="st">&#39;target&#39;</span>]<span class="op">==</span><span class="bu">str</span>(d)]</span>
<span id="cb69-8"><a href="#cb69-8" aria-hidden="true" tabindex="-1"></a> Y.append(imgs.loc[np.random.permutation(imgs.index)[:N_per_digit]])</span>
<span id="cb69-9"><a href="#cb69-9" aria-hidden="true" tabindex="-1"></a> labels.append(np.ones(N_per_digit)<span class="op">*</span>d)</span>
<span id="cb69-10"><a href="#cb69-10" aria-hidden="true" tabindex="-1"></a>Y <span class="op">=</span> np.vstack(Y).astype(np.float64)</span>
<span id="cb69-11"><a href="#cb69-11" aria-hidden="true" tabindex="-1"></a>labels <span class="op">=</span> np.hstack(labels)</span>
<span id="cb69-12"><a href="#cb69-12" aria-hidden="true" tabindex="-1"></a>Y <span class="op">/=</span> <span class="dv">255</span></span></code></pre></div>
<span id="cb69-6"><a href="#cb69-6" aria-hidden="true" tabindex="-1"></a>data <span class="op">=</span> np.asarray(mnist.data)</span>
<span id="cb69-7"><a href="#cb69-7" aria-hidden="true" tabindex="-1"></a>target <span class="op">=</span> np.asarray(mnist.target).astype(<span class="bu">str</span>)</span>
<span id="cb69-8"><a href="#cb69-8" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> d <span class="kw">in</span> digits:</span>
<span id="cb69-9"><a href="#cb69-9" aria-hidden="true" tabindex="-1"></a> imgs <span class="op">=</span> data[target <span class="op">==</span> <span class="bu">str</span>(d)]</span>
<span id="cb69-10"><a href="#cb69-10" aria-hidden="true" tabindex="-1"></a> Y.append(imgs[np.random.permutation(<span class="bu">len</span>(imgs))[:N_per_digit]])</span>
<span id="cb69-11"><a href="#cb69-11" aria-hidden="true" tabindex="-1"></a> labels.append(np.ones(N_per_digit)<span class="op">*</span>d)</span>
<span id="cb69-12"><a href="#cb69-12" aria-hidden="true" tabindex="-1"></a>Y <span class="op">=</span> np.vstack(Y).astype(np.float64)</span>
<span id="cb69-13"><a href="#cb69-13" aria-hidden="true" tabindex="-1"></a>labels <span class="op">=</span> np.hstack(labels)</span>
<span id="cb69-14"><a href="#cb69-14" aria-hidden="true" tabindex="-1"></a>Y <span class="op">/=</span> <span class="dv">255</span></span></code></pre></div>
<p>Now let’s visualise some examples from our subsampled dataset to get a sense of what the data looks like.</p>
<h2 id="mnist-digit-examples">MNIST Digit Examples</h2>
<div style="text-align:right">
Expand Down
27 changes: 19 additions & 8 deletions _notebooks/11-deep-gps-I.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,10 @@
"outputs": [],
"source": [
"from matplotlib import rc\n",
"from mlai import plot\n",
"import mlai\n",
"\n",
"rc(\"font\", **{'family':'sans-serif','sans-serif':['Helvetica'],'size':30})\n",
"rc(\"text\", usetex=True)"
"rc(\"font\", **{'family':'sans-serif','sans-serif':['Helvetica'],'size':30})"
],
"id": "b4899adc-c510-4ced-bf32-adf9da122d91"
},
Expand All @@ -672,7 +673,9 @@
"outputs": [],
"source": [
"pgm = plot.horizontal_chain(depth=5)\n",
"pgm.render().figure.savefig(\"./deepgp/deep-markov.svg\", transparent=True)"
"mlai.write_figure('deep-markov.svg',\n",
" figure=pgm.render().figure,\n",
" directory='./deepgp')"
],
"id": "e1f06e27-f3e2-4ebc-92ce-67e9c5f3ab1a"
},
Expand All @@ -699,8 +702,10 @@
"outputs": [],
"source": [
"from matplotlib import rc\n",
"rc(\"font\", **{'family':'sans-serif','sans-serif':['Helvetica'], 'size':15})\n",
"rc(\"text\", usetex=True)"
"from mlai import plot\n",
"import mlai\n",
"\n",
"rc(\"font\", **{'family':'sans-serif','sans-serif':['Helvetica'], 'size':15})"
],
"id": "e0db8165-a2d6-4ae6-9182-88ad41625848"
},
Expand All @@ -711,7 +716,9 @@
"outputs": [],
"source": [
"pgm = plot.vertical_chain(depth=5)\n",
"pgm.render().figure.savefig(\"./deepgp/deep-markov-vertical.svg\", transparent=True)"
"mlai.write_figure('deep-markov-vertical.svg',\n",
" figure=pgm.render().figure,\n",
" directory='./deepgp')"
],
"id": "0c6d23c5-f13e-49aa-9708-48d8bfac81d9"
},
Expand Down Expand Up @@ -781,7 +788,9 @@
"metadata": {},
"outputs": [],
"source": [
"import daft"
"import daft\n",
"from mlai import plot\n",
"import mlai"
],
"id": "734eaf8a-f0bc-42b1-bc97-aa370a311d86"
},
Expand All @@ -794,7 +803,9 @@
"pgm = plot.vertical_chain(depth=5, shape=[2, 7])\n",
"pgm.add_node(daft.Node('y_2', r'$\\mathbf{y}_2$', 1.5, 3.5, observed=True))\n",
"pgm.add_edge('f_2', 'y_2')\n",
"pgm.render().figure.savefig(\"./deepgp/deep-markov-vertical-side.svg\", transparent=True)"
"mlai.write_figure('deep-markov-vertical-side.svg',\n",
" figure=pgm.render().figure,\n",
" directory='./deepgp')"
],
"id": "a4160027-6d3b-4366-8f9a-9d488835c063"
},
Expand Down
8 changes: 5 additions & 3 deletions _notebooks/12-deep-gps-II.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2873,7 +2873,7 @@
"metadata": {},
"outputs": [],
"source": [
"mnist = fetch_openml('mnist_784')"
"mnist = fetch_openml('mnist_784', version=1, as_frame=False, parser='liac-arff')"
],
"id": "4dd1b849-4c7f-4da9-bc81-88a20da534c7"
},
Expand Down Expand Up @@ -2906,9 +2906,11 @@
"N_per_digit = 100\n",
"Y = []\n",
"labels = []\n",
"data = np.asarray(mnist.data)\n",
"target = np.asarray(mnist.target).astype(str)\n",
"for d in digits:\n",
" imgs = mnist['data'][mnist['target']==str(d)]\n",
" Y.append(imgs.loc[np.random.permutation(imgs.index)[:N_per_digit]])\n",
" imgs = data[target == str(d)]\n",
" Y.append(imgs[np.random.permutation(len(imgs))[:N_per_digit]])\n",
" labels.append(np.ones(N_per_digit)*d)\n",
"Y = np.vstack(Y).astype(np.float64)\n",
"labels = np.hstack(labels)\n",
Expand Down
Loading