Torch (machine learning)

Torch is an open-source machine learning library, a scientific computing framework, and a scripting language based on Lua. It provides LuaJIT interfaces to deep learning algorithms implemented in C. It was created by the Idiap Research Institute at EPFL. Torch development moved in 2017 to PyTorch, a port of the library to Python.

torch
The core package of Torch is. It provides a flexible N-dimensional array or Tensor, which supports basic routines for indexing, slicing, transposing, type-casting, resizing, sharing storage and cloning. This object is used by most other packages and thus forms the core object of the library. The Tensor also supports mathematical operations like,  ,  , statistical distributions like uniform, normal and multinomial, and BLAS operations like dot product, matrix–vector multiplication, matrix–matrix multiplication  and matrix product.

The following exemplifies using torch via its REPL interpreter:

The  package also simplifies object-oriented programming and serialization by providing various convenience functions which are used throughout its packages. The  function can be used to create object factories (classes). When the constructor is called, torch initializes and sets a Lua table with the user-defined metatable, which makes the table an object.

Objects created with the torch factory can also be serialized, as long as they do not contain references to objects that cannot be serialized, such as Lua coroutines, and Lua userdata. However, userdata can be serialized if it is wrapped by a table (or metatable) that provides   and   methods.

nn
The  package is used for building neural networks. It is divided into modular objects that share a common  interface. Modules have a  and   method that allow them to feedforward and backpropagate, respectively. Modules can be joined using module composites, like,   and   to create complex task-tailored graphs. Simpler modules like,   and   make up the basic component modules. This modular interface provides first-order automatic gradient differentiation. What follows is an example use-case for building a multilayer perceptron using Modules:

Loss functions are implemented as sub-classes of, which has a similar interface to. It also has  and   methods for computing the loss and backpropagating gradients, respectively. Criteria are helpful to train neural network on classical tasks. Common criteria are the mean squared error criterion implemented in  and the cross-entropy criterion implemented in. What follows is an example of a Lua function that can be iteratively called to train an  Module on input Tensor , target Tensor   with a scalar  :

It also has  class for training a neural network using stochastic gradient descent, although the   package provides much more options in this respect, like momentum and weight decay regularization.

Other packages
Many packages other than the above official packages are used with Torch. These are listed in the torch cheatsheet. These extra packages provide a wide range of utilities such as parallelism, asynchronous input/output, image processing, and so on. They can be installed with LuaRocks, the Lua package manager which is also included with the Torch distribution.

Applications
Torch is used by the Facebook AI Research Group, IBM, Yandex and the Idiap Research Institute. Torch has been extended for use on Android and iOS. It has been used to build hardware implementations for data flows like those found in neural networks.

Facebook has released a set of extension modules as open source software.