Hello everyone,

This is the first post of a new course about Deep Learning and Torch.

In this post you will see how to install, how to configure the development environment, and a simple overview about the Lua language.

1. About Deep Learning with Torch Tutorial / Course

I’ll be covering the basic theoretical aspects of Deep Learning, and show how to implement it with Torch.

Published content:

Source Codes:

Get updates Follow @aron-bordin
Star it: Star
Download: Download

2. About Torch

Torch is a scientific computing framework with wide support for machine learning algorithms that puts GPUs first. It is easy to use and efficient, thanks to an easy and fast scripting language, LuaJIT, and an underlying C/CUDA implementation.

A summary of core features:

  • a powerful N-dimensional array
  • lots of routines for indexing, slicing, transposing, …
  • amazing interface to C, via LuaJIT
  • linear algebra routines
  • neural network, and energy-based models
  • numeric optimization routines
  • Fast and efficient GPU support
  • Embeddable, with ports to iOS, Android and FPGA backends

When working with Deep Learning, there are 2 essential packages that you will use with Torch: nn and optim. These two packages will be covered in the following posts. Also, the main component of Torch is the Tensor that will be covered in the next post.

3. Installing Torch

Feel free to follow the installation instructions in the official page: http://torch.ch/docs/getting-started.html

Basically, you need to run the following command on terminal:

# in a terminal, run the commands WITHOUT sudo
git clone https://github.com/torch/distro.git ~/torch --recursive
cd ~/torch; bash install-deps;

After the installation process, you should have some new commands available such as torch, luarocks, and so on.

If this commands are not available, make sure to append the ~/torch/bin folder to the system path.

4. Playing with Lua

If you are experienced with Lua, you can skip to the next section :)

Torch uses a LuaJIT, to play with it, opens a new terminal and type: luajit, and you may see something like:

LuaJIT 2.1.0-beta1 -- Copyright (C) 2005-2015 Mike Pall. http://luajit.org/

 _____              _     
|_   _|            | |    
  | | ___  _ __ ___| |__  
  | |/ _ \| '__/ __| '_ \
  | | (_) | | | (__| | | |
  \_/\___/|_|  \___|_| |_|

JIT: ON SSE2 SSE3 SSE4.1 fold cse dce fwd dse narrow loop abc sink fuse

4.1 - Variables

In Lua, all variables are defined as global by default (you need to use the local directive to declare it as a local variable).

The name can be composed with letters, digits, and the underscore char. And Lua is case sensitive.

Lua is dinamically typed, so you don’t defined the type of the variable, it will be automatically defined by the compiler.

-- declaring Variables

myGlobalVar = 1
local myLocalVar = 2


-- swapping (like python)
myGlobalVar, myLocalVar = myLocalVar, myGlobalVar


local f = 3.0

print('Value of f = ', f)

local a, b, c = 1, 2, 3

print('Values of a,b,c:', a, b, c)


Value of f = 	3
Values of a,b,c:	1	2	3

Here is a list of some of the standard variable types:

  • nil: empty value (with no value)
  • boolean: accept true and false
  • number: real values (with double precision)
  • string: array of characters, can be defined with ' or "
  • function: a method written in Lua or C
  • table: ordinary arrays, dictionaries, symbol tables, sets, records, graphs, trees, etc

if you want to see the type of a variable, you can use the type function.

print(type('Hi !'))



4.2 - Operators

Lua provides the following operators:

  • Arithmetic:
    • +: sum two numbers
    • -: subtract two numbers
    • *: multiply two numbers
    • /: divide two numbers
    • %: module
    • ^: power
  • Relational:
    • ==: equal
    • ~=: not equal
    • >: greater than
    • <: lesser than
    • >=: greater or equal than
    • <=: lesses or equal than
  • Logical:
    • and
    • or
    • not
  • Others:
    • ..: concatenates two strings
    • #: returns the length of the object (example: #”Hi!” is 3)

4.3 Loops

You can use the following loop methods with Lua:


while (condition) do


for counter = initial_value,max/min_value, increment do

for example:

for i = 1, 10, 2
   print(i) -- prints odd numbers

repeat while:

while( condition )

4.4 - If

You can control your logic with the if statement as follows


if boolean_expression then

if and else:

if boolean_expression then

if, else, and elseif:

if boolean_expression then
elseif boolean_expression

4.6 - More about Lua

As you need more resources about Lua, follows the Lua Tutorial, and google about it.

5. luarocks

LuaRocks is the package manager for Lua modules.

It allows you to create and install Lua modules as self-contained packages called rocks. You can download and install LuaRocks on Unix and Windows.

To install a new package, for example, the dp package, a pretty good deep learning library for streamlining research and development using the Torch7 distribution, you can use the command:

luarocks install dp

6. Acknowledgements

This posts covered the basic of Torch7 installation and a simple overview of the Lua package. We’re going to start with more technical details in the next post, where you are going to learn about Tensor, the core of Torch.

Thank you for reading, I see you in the next post.