Command Parser in Lua with Torch

Reading time ~2 minutes

I recently started learning Lua because the interest of learning popular deep learning package Torch. Before I jumped into Torch, I have already fallen in love with Lua. First of all, it is very lightweight and fast as it says. It contains some very interesting features that I haven’t met in C/C++ and Python such as the function in class. It is more ‘astonishing’ that Lua is written in pure C and the size is very small. I only read few lines from source code, but I have to admit it is the most beautiful code I ever met before based on my personal experiences. Some of useful links of learning lua is below:

Torch and Caffe are the most widely spread deep learning toolboxes in current research field. It seems that Caffe is more popular in US universities and Torch is more popular in Europe maybe because where are the developers from. The reason I selected Torch is because the post in Tomasz’s blog and Yann Lecun said that

1
Torch is for research in deep learning, Caffe is OK for using ConvNets as a "black box" (or a grey box), but not flexible enough for innovative research in deep learning. That's why Facebook and DeepMind both use Torch for almost everything.

Command parser in Lua using Torch

If you are experienced in

1
Boost.Program_options
, the following example will be straight forward to you:

1
2
3
4
5
6
7
8
9
10
11
cmd = torch.CmdLine()
cmd:text()
cmd:text('Student info')
cmd:text()
cmd:text('Options:')
-- global:
cmd:option('-age', 18, 'age of the student') -- int param
cmd:option('-gender', true, 'is boy')        -- true = boy, false = girl
cmd:option('-name', 'adam', 'name')          -- name, string
cmd:text()
opt = cmd:parse(arg or {})

By type in

1
th -i <filename> -h/--help
, the program options will printed in console:

1
2
3
4
5
6
7
8
Usage: /home/kanzhi/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th [options]

Student info

Options:
  -age    age of the student [18]
  -gender is boy [true]
  -name   name [adam]

After parse the args to

1
opt
, the options are saved as the table. You can also simply run the following code to the the parameters settings:

1
2
3
4
print 'default paremter settings: '
for k, v in pairs(opt) do
   print (k, v)
end

Kernel Descriptor in Depth

Title| Kernel Descriptors for Visual Recognition-----|---------------------------------------------Conference|NIPS 2010Author |Liefeng...… Continue reading