# Lua notes for Torch7

I heard about Lua two years ago when I was interested about make computer games. I didn’t know exactly what was the philosophy(?) behind the language, but the syntax looked clean and simple, which was eye-catching for someone who came from languages such as C/C++ and avoind C# at all costs.

I won’t bother you to talk about why my enthusiasm with game programming has been vanished and thereafter I haven’t heard about Lua until…

By the spring of 2016 I got in touch with some projects in Lua again, but this time when I was starting to read about Neural Networks and Deep Learning, particularly a framework called Torch 7. I was reading about a project called neural-style and decided to take a look at source code (which was getting popular fast at github) and again, I was surprised with how beautiful the components look at that code (projects written in Torch looks like lego pieces, it’s simple and powerful. Except the part of data parsing, maybe another post about this).

So, what about Lua? Lua is cross-platform programming language with support to multiple programming paradigms. A fun fact is that the world Lua means “moon” in portuguese and that made me go after the history about the language. And eureka! That language was created here in my state, in a university called Pontifical Catholic University (or PUC-RIO) and was an alternative to the market reserve in Brazil (Political settlement).

But let’s skip the history part and talk some details about the language and syntax.

1. Lua is really fast. The source code is writen in ANSI C, it is simple and small. For some algorithms Lua can be compared to Java. And win most of the others scripting Languages. With the LuaJIT compiler you can speed up even more. Check this Benchmark.

Lua is a powerful, efficient, lightweight, embeddable scripting language. It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description.

2. Table: Love and hate relationship. Everything in Lua is a Table. Tables are like hashtables and we can construct a empty table with {}.

Tables in Lua are not a data structure; they are the data structure. All structures that other languages offer(arrays, records, lists, queues, sets) are represented with tables in Lua. More to the point, tables implement all these structures efficiently.

 -- Empty table
a = {}

-- Use as a dict in python (1-based)
a[1] = 0
a[2] = 1
a[100] = 99

-- |> a = {1: 0, 2: 1, 100: 99}

3. Lua is Free!

Lua is free open-source software, distributed under a very liberal license (the well-known MIT license). It may be used for any purpose, including commercial purposes, at absolutely no cost. Just download it and use it.

4. Package manager: No, it isn’t npm. But the LuaRocks is a good package manager and easy to use!

LuaRocks is the package manager for Lua modules. It allows you to create and install Lua modules as self-contained packages called rocks.

5. Torch 7: One of the most popular Deep Learning frameworks use Lua! And again, it’s really fast. You can find a lot of DL projects using Torch such as:

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.

So should I learn Lua to start on Deep Learning? Hard question. I’ve explained why I started with Lua, but… To start learning about DL the Keras API it is easy to manipulate (But not so flexible as Torch). And today there is the PyTorch but Torch 7 is not a dying framework.

Do you wanna know more about the syntax and start programming? Here are some really awesome resources: