Skip to content

Commit 3d3863a

Browse files
committed
MNIST
1 parent be42bc7 commit 3d3863a

7 files changed

+2240
-113
lines changed

BayesianLinearModel.ipynb

+53-35
Large diffs are not rendered by default.

DrawNN.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#Code from https://gist.github.com/craffel/2d727968c3aaebd10359
2+
3+
import matplotlib.pyplot as plt
4+
5+
def draw_neural_net(ax, left, right, bottom, top, layer_sizes, bias=0, draw_edges=False):
6+
'''
7+
Draw a neural network cartoon using matplotilb.
8+
9+
:usage:
10+
>>> fig = plt.figure(figsize=(12, 12))
11+
>>> draw_neural_net(fig.gca(), .1, .9, .1, .9, [4, 7, 2])
12+
13+
:parameters:
14+
- ax : matplotlib.axes.AxesSubplot
15+
The axes on which to plot the cartoon (get e.g. by plt.gca())
16+
- left : float
17+
The center of the leftmost node(s) will be placed here
18+
- right : float
19+
The center of the rightmost node(s) will be placed here
20+
- bottom : float
21+
The center of the bottommost node(s) will be placed here
22+
- top : float
23+
The center of the topmost node(s) will be placed here
24+
- layer_sizes : list of int
25+
List of layer sizes, including input and output dimensionality
26+
- bias : Boolean
27+
Draw an extra bias node at each layer
28+
- draw_edges : Boolean
29+
If false, omit edge connections
30+
'''
31+
n_layers = len(layer_sizes)
32+
v_spacing = (top - bottom)/float(max(layer_sizes)+bias)
33+
h_spacing = (right - left)/float(len(layer_sizes) - 1)
34+
# Nodes
35+
for n, layer_size in enumerate(layer_sizes):
36+
layer_top = v_spacing*(layer_size - 1)/2. + (top + bottom)/2.
37+
bias_node = (bias if n<len(layer_sizes)-1 else 0)
38+
for m in range(layer_size+bias_node ):
39+
node_color = 'w' if m<layer_size else 'b'
40+
circle = plt.Circle((n*h_spacing + left, layer_top - m*v_spacing), v_spacing/8.,
41+
color=node_color, ec='k', zorder=4)
42+
ax.add_artist(circle)
43+
# Edges
44+
if draw_edges:
45+
for n, (layer_size_a, layer_size_b) in enumerate(zip(layer_sizes[:-1], layer_sizes[1:])):
46+
layer_top_a = v_spacing*(layer_size_a - 1)/2. + (top + bottom)/2.
47+
layer_top_b = v_spacing*(layer_size_b - 1)/2. + (top + bottom)/2.
48+
for m in range(layer_size_a+bias):
49+
for o in range(layer_size_b):
50+
line = plt.Line2D([n*h_spacing + left, (n + 1)*h_spacing + left],
51+
[layer_top_a - m*v_spacing, layer_top_b - o*v_spacing],
52+
c='k')
53+
ax.add_artist(line)

KalmanFilter.ipynb

+25-64
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"metadata": {
6-
"deletable": true,
7-
"editable": true
8-
},
5+
"metadata": {},
96
"source": [
107
"# The Kalman Filter\n",
118
"\n",
@@ -14,10 +11,7 @@
1411
},
1512
{
1613
"cell_type": "markdown",
17-
"metadata": {
18-
"deletable": true,
19-
"editable": true
20-
},
14+
"metadata": {},
2115
"source": [
2216
"## The Linear Dynamical System (LDS)\n",
2317
"\n",
@@ -71,10 +65,7 @@
7165
},
7266
{
7367
"cell_type": "markdown",
74-
"metadata": {
75-
"deletable": true,
76-
"editable": true
77-
},
68+
"metadata": {},
7869
"source": [
7970
"Example: A point object moving with (almost) constant velocity.\n",
8071
"\n",
@@ -84,10 +75,7 @@
8475
},
8576
{
8677
"cell_type": "markdown",
87-
"metadata": {
88-
"deletable": true,
89-
"editable": true
90-
},
78+
"metadata": {},
9179
"source": [
9280
"The input to the algorithm are the parameters and a sequence of \n",
9381
"observations $y_t$ for $t=1 \\dots T$\n",
@@ -150,9 +138,6 @@
150138
"cell_type": "code",
151139
"execution_count": 24,
152140
"metadata": {
153-
"collapsed": false,
154-
"deletable": true,
155-
"editable": true,
156141
"scrolled": false
157142
},
158143
"outputs": [
@@ -218,10 +203,7 @@
218203
},
219204
{
220205
"cell_type": "markdown",
221-
"metadata": {
222-
"deletable": true,
223-
"editable": true
224-
},
206+
"metadata": {},
225207
"source": [
226208
"## Your Task:\n",
227209
"\n",
@@ -260,10 +242,7 @@
260242
},
261243
{
262244
"cell_type": "markdown",
263-
"metadata": {
264-
"deletable": true,
265-
"editable": true
266-
},
245+
"metadata": {},
267246
"source": [
268247
"The new algorithm is as follows\n",
269248
"## Kalman Filter with incremental updates\n",
@@ -339,11 +318,7 @@
339318
{
340319
"cell_type": "code",
341320
"execution_count": 18,
342-
"metadata": {
343-
"collapsed": false,
344-
"deletable": true,
345-
"editable": true
346-
},
321+
"metadata": {},
347322
"outputs": [],
348323
"source": [
349324
"A = np.mat('[1, 1; 0, 1]')\n",
@@ -363,10 +338,7 @@
363338
},
364339
{
365340
"cell_type": "markdown",
366-
"metadata": {
367-
"deletable": true,
368-
"editable": true
369-
},
341+
"metadata": {},
370342
"source": [
371343
"Below is a not very efficient and numerically unstable but nevertheless a readable implementation of the Kalman filter for your reference. For or small model sizes $M, N, T$ the results would give a base to test against. Cross check with your implementation that your numerical results are about the same.\n",
372344
"\n"
@@ -375,11 +347,7 @@
375347
{
376348
"cell_type": "code",
377349
"execution_count": 25,
378-
"metadata": {
379-
"collapsed": false,
380-
"deletable": true,
381-
"editable": true
382-
},
350+
"metadata": {},
383351
"outputs": [
384352
{
385353
"name": "stdout",
@@ -443,10 +411,7 @@
443411
},
444412
{
445413
"cell_type": "markdown",
446-
"metadata": {
447-
"deletable": true,
448-
"editable": true
449-
},
414+
"metadata": {},
450415
"source": [
451416
"For $i=1 \\dots M$ (InnerFor)\n",
452417
"$$\n",
@@ -473,11 +438,7 @@
473438
{
474439
"cell_type": "code",
475440
"execution_count": 26,
476-
"metadata": {
477-
"collapsed": false,
478-
"deletable": true,
479-
"editable": true
480-
},
441+
"metadata": {},
481442
"outputs": [
482443
{
483444
"name": "stdout",
@@ -549,9 +510,6 @@
549510
"cell_type": "code",
550511
"execution_count": 7,
551512
"metadata": {
552-
"collapsed": false,
553-
"deletable": true,
554-
"editable": true,
555513
"scrolled": true
556514
},
557515
"outputs": [
@@ -589,9 +547,6 @@
589547
"cell_type": "code",
590548
"execution_count": 1,
591549
"metadata": {
592-
"collapsed": false,
593-
"deletable": true,
594-
"editable": true,
595550
"scrolled": true
596551
},
597552
"outputs": [
@@ -651,11 +606,7 @@
651606
{
652607
"cell_type": "code",
653608
"execution_count": 2,
654-
"metadata": {
655-
"collapsed": false,
656-
"deletable": true,
657-
"editable": true
658-
},
609+
"metadata": {},
659610
"outputs": [
660611
{
661612
"data": {
@@ -707,7 +658,7 @@
707658
],
708659
"metadata": {
709660
"kernelspec": {
710-
"display_name": "Python 3",
661+
"display_name": "Python [default]",
711662
"language": "python",
712663
"name": "python3"
713664
},
@@ -721,9 +672,19 @@
721672
"name": "python",
722673
"nbconvert_exporter": "python",
723674
"pygments_lexer": "ipython3",
724-
"version": "3.5.2"
675+
"version": "3.6.5"
676+
},
677+
"toc": {
678+
"nav_menu": {},
679+
"number_sections": true,
680+
"sideBar": true,
681+
"skip_h1_title": false,
682+
"toc_cell": false,
683+
"toc_position": {},
684+
"toc_section_display": "block",
685+
"toc_window_display": false
725686
}
726687
},
727688
"nbformat": 4,
728-
"nbformat_minor": 0
689+
"nbformat_minor": 1
729690
}

0 commit comments

Comments
 (0)