Skip to content

Commit

Permalink
notebook updates
Browse files Browse the repository at this point in the history
  • Loading branch information
denizyuret committed Jan 21, 2019
1 parent 74d6a0d commit c95ef34
Showing 1 changed file with 116 additions and 13 deletions.
129 changes: 116 additions & 13 deletions tutorial/20.mnist.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@
"* New functions: [dir](http://denizyuret.github.io/Knet.jl/latest/reference.html#Knet.dir), [minibatch, Data](http://denizyuret.github.io/Knet.jl/latest/reference.html#Knet.minibatch), [mnist, mnistview](https://github.com/denizyuret/Knet.jl/blob/master/data/mnist.jl)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the next few notebooks, we build classification models for the MNIST handwritten digit recognition dataset. MNIST has 60000 training and 10000 test examples. Each input x consists of 784 pixels representing a 28x28 image. The corresponding output indicates the identity of the digit 0..9."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "slide"
Expand All @@ -42,13 +49,32 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"┌ Info: Loading MNIST...\n",
"└ @ Main /home/gridsan/dyuret/.julia/dev/Knet/data/mnist.jl:33\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"28×28×1×60000 Array{Float32,4}\n",
"60000-element Array{UInt8,1}\n",
"28×28×1×10000 Array{Float32,4}\n",
"10000-element Array{UInt8,1}\n"
]
}
],
"source": [
"# This loads the MNIST handwritten digit recognition dataset:\n",
"include(Knet.dir(\"data\",\"mnist.jl\")) # Knet.dir constructs a path relative to Knet root\n",
Expand All @@ -58,13 +84,51 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAR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KLKUptjZpfjqKxYmXLlmO3cvH262TfP2lpDnqtLjAnQoTEjVVhmRYxSox2O11xf4lAGaTM/Zu60nJ0q1KuTkgqOYIMP1KrKWF/7aMCbObmJMQE6MGYPp/nT9cGd5D/0NlMFPihpRWJvQ33muZjZ9TmRagPNBGuDo92kxTiL3R61D1Gjnza05eQmxgTowJgZ6iz1VoxClzsCz5c8w0zWx4s7cCGzXAi+MrnAG9AblDnDU1FrnbZoB/1wTmZiTICCxpzhOr+5KVah6eCiEmcY+mF9yZexxIDoqNkbBfKana1OTdSaj0dNwqaczMSYAAWM6e0uXSyJtYfZTlX7ivCrLSr0RC+XgW2uKvBUrx6LYp+nTDlOx4P6orx9XzOqw74rrUxr/f1cstaqrKzEmABBY7pYan8triuq3EftR8/4wV4mZPx2jTmBfkPtVdR55mrEIikiV7Y//S2WMmXk+JIYEyRoTI3BmB77TnmJMzPa46OfW6WwUJN8dyTbGaOLlUU8J15ghuqMyFrNpkbvzouhLc+ul8SYIAFj+nkZM4pYJ8a6MnpjfXA0szzkKyzQ3GHfSiMx37bYZ2s10RiXpkXmFPGNqnR2J+8zoMSYAIFvbc5xDY6M/R55x6iw3I/OKfrd5WmmqrfQA8p8UtJ3RFljNze5MyeeGBMgrzFDLdbDv2fMzuQFd+S8ISAxJkjeVel4PdBS8p3yf4n6QDwxJkBw5/uhU2K9rf6/khiTkNAp/AWFPiBLadIx6wAAAABJRU5ErkJggg==",
"text/plain": [
"28×140 Array{Gray{Float32},2} with eltype Gray{Float32}:\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) … Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) … Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) … Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" ⋮ ⋱ \n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) … Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) … Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)\n",
" Gray{Float32}(0.0) Gray{Float32}(0.0) Gray{Float32}(0.0)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# mnistview(x,i) converts the i'th instance in x into an image\n",
"# Here is the first five images from the test set:\n",
Expand All @@ -74,27 +138,46 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[7, 2, 1, 10, 4]\n"
]
}
],
"source": [
"# Here are their labels (10 is used to represent 0)\n",
"println(Int.(ytst[1:5]));"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"Data{Tuple{Array{Float32,4},Array{UInt8,1}}}(Float32[0.0 0.0 … 0.0 0.0; 0.0 0.0 … 0.0 0.0; … ; 0.0 0.0 … 0.0 0.0; 0.0 0.0 … 0.0 0.0], UInt8[0x07 0x02 … 0x05 0x06], 100, 10000, false, 9901, 1:10000, false, (28, 28, 1, 10000), (10000,), Array{Float32,4}, Array{UInt8,1})"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# `minibatch` splits the data tensors to small chunks called minibatches.\n",
"# It returns a Knet.Data struct: an iterator of (x,y) pairs.\n",
Expand All @@ -104,13 +187,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"28×28×1×100 Array{Float32,4}\n",
"100-element Array{UInt8,1}\n"
]
}
],
"source": [
"# Each minibatch is an (x,y) pair where x is 100 (28x28x1) images and y are the corresponding 100 labels.\n",
"# Here is the first minibatch in the test set:\n",
Expand All @@ -120,13 +212,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"600"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Iterators can be used in for loops, e.g. `for (x,y) in dtrn`\n",
"# dtrn generates 600 minibatches of 100 images (total 60000)\n",
Expand Down

0 comments on commit c95ef34

Please sign in to comment.