Lua and Torch
On this book I stressed out the importance of knowing how to write your own deep learning/artificial intelligence library. But is also very important specially while researching some topic, to understand the most common libraries. This chapter will teach the basics on Torch, but before that we're going also to learn Lua.
Lua language
Lua was first created to be used on embedded systems, the idea was to have a simple cross-platform and fast language. One the main features of Lua is it's easy integration with C/C++.
Lua was originally designed in 1993 as a language for extending software applications to meet the increasing demand for customization at the time.
This extension means that you could have a large C/C++ program and, some parts in Lua where you could easily change without the need to recompile everything.
Torch
Torch is a scientific computing framework based on Lua with CPU and GPU backends. You can imagine like a Numpy but with CPU and GPU implementation. Some nice features:
Efficient linear algebra functions with GPU support
Neural Network package, with automatic differentiation (No need to backpropagate manually)
Multi-GPU support
First contact with Lua
Bellow we have some simple examples on Lua just to have some contact with the language.
print("Hello World") -- First thing, note that there is no main...
--[[
This is how we do a multi-line comment
on lua, to execute a lua program just use...
lua someFile.lua
]]Lua datatypes
The language offer those basic types:
Numbers(Float)
string
boolean
table
Doing some math
Normally we will rely on Torch, but Lua has some math support as well.
Lua include (require)
The lua statement to include other lua files is the "require", as usual it is used to add some library

Conditionals
Just the simple if-then-else. Lua does not have switch statement.
Loops
Lua have while, repeat and for loops. For loops has also a "for-each" extension to iterate on tables.
Functions
Defining functions in Lua is quite easy, it's syntax reminds matlab.
Tables
On Lua we use tables for everything else (ie: Lists, Dictionaries, Classes, etc...)
Object oriented programming
Lua does not support directly OOP, but you can emulate all it's main functionalities (Inheritance, Encapsulation) with tables and metatables
Metatable tutorial: Used to override operations (metamethods) on tables.
File I/O
Run console commands
First contact with Torch
On this section we're going to see how to do simple operations with Torch, more complex stuff will be dealt latter.
One of the torch objectives is to give some matlab functionality, an usefull cheetsheat can be found here:
Some Matrix operations
Doing operations on GPU
Plotting

Starting with nn (XOR problem)
Define the loss function
On torch the loss function is called criterion, as on this case we're dealling with a binary classification, we will choose the Mean Squared Error criterion
Training Manually
Here we're going to back-propagate our model to get the output related to the loss gradient then use gradient descent to update the parameters.
Test the network
Trainning with optimim
Torch provides a standard way to optimize any function with respect to some parameters. In our case, our function will be the loss of our network, given an input, and a set of weights. The goal of training a neural net is to optimize the weights to give the lowest loss over our training set of input data. So, we are going to use optim to minimize the loss with respect to the weights, over our training set.
Test the network
Last updated
Was this helpful?