{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "require 'nn'\n",
    "\n",
    "net = nn.Sequential()\n",
    "net:add(nn.SpatialConvolution(3, 6, 5, 5)) -- 1 input image channel, 6 output channels, 5x5 convolution kernel\n",
    "net:add(nn.SpatialMaxPooling(2,2,2,2))     -- A max-pooling operation that looks at 2x2 windows and finds the max.\n",
    "net:add(nn.SpatialConvolution(6, 16, 5, 5))\n",
    "net:add(nn.SpatialMaxPooling(2,2,2,2))\n",
    "net:add(nn.View(16*5*5))                    -- reshapes from a 3D tensor of 16x5x5 into 1D tensor of 16*5*5\n",
    "net:add(nn.Linear(16*5*5, 120))             -- fully connected layer (matrix multiplication between input and weights)\n",
    "net:add(nn.Linear(120, 84))\n",
    "net:add(nn.Linear(84, 10))                   -- 10 is the number of outputs of the network (in this case, 10 digits)\n",
    "net:add(nn.LogSoftMax())                     -- converts the output to a log-probability. Useful for classification problems"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "criterion = nn.ClassNLLCriterion()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "trainer = nn.StochasticGradient(net, criterion)\n",
    "trainer.learningRate = 0.001\n",
    "trainer.maxIteration = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "ename": "[string \"-- ignore setmetatable for now, it is a featu...\"]:2: bad argument #1 to 'setmetatable' (table expected, got nil)\nstack traceback:\n\t[C]: in function 'setmetatable'\n\t[string \"-- ignore setmetatable for now, it is a featu...\"]:2: in main chunk\n\t[C]: in function 'xpcall'\n\t/usr/local/share/lua/5.1/itorch/main.lua:179: in function </usr/local/share/lua/5.1/itorch/main.lua:143>\n\t/usr/local/share/lua/5.1/lzmq/poller.lua:75: in function 'poll'\n\t/usr/local/share/lua/5.1/lzmq/impl/loop.lua:307: in function 'poll'\n\t/usr/local/share/lua/5.1/lzmq/impl/loop.lua:325: in function 'sleep_ex'\n\t/usr/local/share/lua/5.1/lzmq/impl/loop.lua:370: in function 'start'\n\t/usr/local/share/lua/5.1/itorch/main.lua:350: in main chunk\n\t[C]: in function 'require'\n\t[string \"arg={'/Users/carpedm20/Library/Jupyter/runtim...\"]:1: in main chunk",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "[string \"-- ignore setmetatable for now, it is a featu...\"]:2: bad argument #1 to 'setmetatable' (table expected, got nil)\nstack traceback:\n\t[C]: in function 'setmetatable'\n\t[string \"-- ignore setmetatable for now, it is a featu...\"]:2: in main chunk\n\t[C]: in function 'xpcall'\n\t/usr/local/share/lua/5.1/itorch/main.lua:179: in function </usr/local/share/lua/5.1/itorch/main.lua:143>\n\t/usr/local/share/lua/5.1/lzmq/poller.lua:75: in function 'poll'\n\t/usr/local/share/lua/5.1/lzmq/impl/loop.lua:307: in function 'poll'\n\t/usr/local/share/lua/5.1/lzmq/impl/loop.lua:325: in function 'sleep_ex'\n\t/usr/local/share/lua/5.1/lzmq/impl/loop.lua:370: in function 'start'\n\t/usr/local/share/lua/5.1/itorch/main.lua:350: in main chunk\n\t[C]: in function 'require'\n\t[string \"arg={'/Users/carpedm20/Library/Jupyter/runtim...\"]:1: in main chunk"
     ]
    }
   ],
   "source": [
    "-- ignore setmetatable for now, it is a feature beyond the scope of this tutorial. It sets the index operator.\n",
    "setmetatable(trainset, \n",
    "    {__index = function(t, i) \n",
    "                    return {t.data[i], t.label[i]} \n",
    "                end}\n",
    ");\n",
    "trainset.data = trainset.data:double() -- convert the data from a ByteTensor to a DoubleTensor.\n",
    "\n",
    "function trainset:size() \n",
    "    return self.data:size(1) \n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "iTorch",
   "language": "lua",
   "name": "itorch"
  },
  "language_info": {
   "name": "lua",
   "version": "20003"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
