Torch is an
open-source machine learning
Machine learning (ML) is a field of study in artificial intelligence concerned with the development and study of Computational statistics, statistical algorithms that can learn from data and generalise to unseen data, and thus perform Task ( ...
library,
a
scientific computing framework, and a
scripting language
In computing, a script is a relatively short and simple set of instructions that typically automation, automate an otherwise manual process. The act of writing a script is called scripting. A scripting language or script language is a programming ...
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
torch
. It provides a flexible N-dimensional array or
Tensor
In mathematics, a tensor is an algebraic object that describes a multilinear relationship between sets of algebraic objects associated with a vector space. Tensors may map between different objects such as vectors, scalars, and even other ...
, 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
max
,
min
,
sum
, statistical distributions like
uniform
A uniform is a variety of costume worn by members of an organization while usually participating in that organization's activity. Modern uniforms are most often worn by armed forces and paramilitary organizations such as police, emergency serv ...
,
normal and
multinomial, and
BLAS operations like
dot product
In mathematics, the dot product or scalar productThe term ''scalar product'' means literally "product with a Scalar (mathematics), scalar as a result". It is also used for other symmetric bilinear forms, for example in a pseudo-Euclidean space. N ...
,
matrix–vector multiplication,
matrix–matrix multiplication and
matrix product.
The following exemplifies using torch via its
REPL interpreter:
> a = torch.randn(3, 4)
> =a
-0.2381 -0.3401 -1.7844 -0.2615
0.1411 1.6249 0.1708 0.8299
-1.0434 2.2291 1.0525 0.8465
orch.DoubleTensor of dimension 3x4
> a 2]
-0.34010116549482
> a:narrow(1,1,2)
-0.2381 -0.3401 -1.7844 -0.2615
0.1411 1.6249 0.1708 0.8299
orch.DoubleTensor of dimension 2x4
> a:index(1, torch.LongTensor)
-0.2381 -0.3401 -1.7844 -0.2615
0.1411 1.6249 0.1708 0.8299
orch.DoubleTensor of dimension 2x4
> a:min()
-1.7844365427828
The
torch
package also simplifies
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
and
serialization
In computing, serialization (or serialisation, also referred to as pickling in Python (programming language), Python) is the process of translating a data structure or object (computer science), object state into a format that can be stored (e. ...
by providing various convenience functions which are used throughout its packages. The
torch.class(classname, parentclass)
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
coroutine
Coroutines are computer program components that allow execution to be suspended and resumed, generalizing subroutines for cooperative multitasking. Coroutines are well-suited for implementing familiar program components such as cooperative task ...
s, and Lua ''userdata''. However, ''userdata'' can be serialized if it is wrapped by a table (or metatable) that provides
read()
and
write()
methods.
nn
The
nn
package is used for building
neural network
A neural network is a group of interconnected units called neurons that send signals to one another. Neurons can be either biological cells or signal pathways. While individual neurons are simple, many of them together in a network can perfor ...
s. It is divided into modular objects that share a common
Module
interface. Modules have a
forward()
and
backward()
method that allow them to
feedforward and
backpropagate, respectively. Modules can be joined using module
composites, like
Sequential
,
Parallel
and
Concat
to create complex task-tailored graphs. Simpler modules like
Linear
,
Tanh
and
Max
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:
> mlp = nn.Sequential()
> mlp:add(nn.Linear(10, 25)) -- 10 input, 25 hidden units
> mlp:add(nn.Tanh()) -- some hyperbolic tangent transfer function
> mlp:add(nn.Linear(25, 1)) -- 1 output
> =mlp:forward(torch.randn(10))
-0.1815
orch.Tensor of dimension 1
Loss functions are implemented as sub-classes of
Criterion
, which has a similar interface to
Module
. It also has
forward()
and
backward()
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
In statistics, the mean squared error (MSE) or mean squared deviation (MSD) of an estimator (of a procedure for estimating an unobserved quantity) measures the average of the squares of the errors—that is, the average squared difference betwee ...
criterion implemented in
MSECriterion
and the
cross-entropy criterion implemented in
ClassNLLCriterion
. What follows is an example of a Lua function that can be iteratively called to train
an
mlp
Module on input Tensor
x
, target Tensor
y
with a scalar
learningRate
:
function gradUpdate(mlp, x, y, learningRate)
local criterion = nn.ClassNLLCriterion()
local pred = mlp:forward(x)
local err = criterion:forward(pred, y);
mlp:zeroGradParameters();
local t = criterion:backward(pred, y);
mlp:backward(x, t);
mlp:updateParameters(learningRate);
end
It also has
StochasticGradient
class for training a neural network using
stochastic gradient descent, although the
optim
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
International Business Machines Corporation (using the trademark IBM), nicknamed Big Blue, is an American Multinational corporation, multinational technology company headquartered in Armonk, New York, and present in over 175 countries. It is ...
,
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.
See also
*
Comparison of deep learning software
*
PyTorch
References
External links
*
{{Lua programming language
Deep learning software
Free statistical software
Lua (programming language) software
Software using the BSD license