Torch is an
open-source
Open source is source code that is made freely available for possible modification and redistribution. Products include permission to use the source code, design documents, or content of the product. The open-source model is a decentralized sof ...
machine learning
Machine learning (ML) is a field of inquiry devoted to understanding and building methods that 'learn', that is, methods that leverage data to improve performance on some set of tasks. It is seen as a part of artificial intelligence.
Machine ...
library,
a
scientific computing
Computational science, also known as scientific computing or scientific computation (SC), is a field in mathematics that uses advanced computing capabilities to understand and solve complex problems. It is an area of science that spans many disc ...
framework, and a
script language based on the
Lua
Lua or LUA may refer to:
Science and technology
* Lua (programming language)
* Latvia University of Agriculture
* Last universal ancestor, in evolution
Ethnicity and language
* Lua people, of Laos
* Lawa people, of Thailand sometimes referred t ...
programming language.
It provides a wide range of algorithms for
deep learning, and uses the scripting language
LuaJIT, and an underlying
C implementation. It was created at
IDIAP
The Idiap Research Institute is a semi-private non-profit research institute at Martigny in the canton of Valais, in south-western Switzerland. It conducts research in the areas of speech processing, computer vision, information retrieval, biom ...
at
EPFL. As of 2018, Torch is no longer in active development. However
PyTorch
PyTorch is a machine learning framework based on the Torch library, used for applications such as computer vision and natural language processing, originally developed by Meta AI and now part of the Linux Foundation umbrella. It is free and op ...
, which is based on the Torch library, is actively developed as of October 2022.
torch
The core package of Torch is
torch
. 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
max
,
min
,
sum
, statistical distributions like
uniform
A uniform is a variety of clothing worn by members of an organization while participating in that organization's activity. Modern uniforms are most often worn by armed forces and paramilitary organizations such as police, emergency services, se ...
,
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 as a result". It is also used sometimes for other symmetric bilinear forms, for example in a pseudo-Euclidean space. is an alg ...
,
matrix–vector multiplication,
matrix–matrix multiplication and
matrix product
In mathematics, particularly in linear algebra, matrix multiplication is a binary operation that produces a matrix from two matrices. For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the ...
.
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", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
and
serialization
In computing, serialization (or serialisation) is the process of translating a data structure or object state into a format that can be stored (e.g. files in secondary storage devices, data buffers in primary storage devices) or transmitted (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
Constructor may refer to:
Science and technology
* Constructor (object-oriented programming), object-organizing method
* Constructors (Formula One), person or group who builds the chassis of a car in auto racing, especially Formula One
* Construc ...
is called, torch initializes and sets a Lua
table
Table may refer to:
* Table (furniture), a piece of furniture with a flat surface and one or more legs
* Table (landform), a flat area of land
* Table (information), a data arrangement with rows and columns
* Table (database), how the table data ...
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 generalize subroutines for non-preemptive multitasking, by allowing execution to be suspended and resumed. Coroutines are well-suited for implementing familiar program components such as cooperative ...
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 network or neural circuit, circuit of biological neurons, or, in a modern sense, an artificial neural network, composed of artificial neurons or nodes. Thus, a neural network is either a biological neural network, made up ...
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
Feedforward is the provision of context of what one wants to communicate prior to that communication. In purposeful activity, feedforward creates an expectation which the actor anticipates. When expected experience occurs, this provides confirmato ...
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
A multilayer perceptron (MLP) is a fully connected class of feedforward artificial neural network (ANN). The term MLP is used ambiguously, sometimes loosely to mean ''any'' feedforward ANN, sometimes strictly to refer to networks composed of mul ...
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 function
In mathematical optimization and decision theory, a loss function or cost function (sometimes also called an error function) is a function that maps an event or values of one or more variables onto a real number intuitively representing some "co ...
s 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 betwe ...
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()
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
Stochastic gradient descent (often abbreviated SGD) is an iterative method for optimizing an objective function with suitable smoothness properties (e.g. differentiable or subdifferentiable). It can be regarded as a stochastic approximation of ...
, 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
LuaRocks is a package manager for the Lua programming language that provides a standard format for distributing Lua modules (in a self-contained format called a "rock"), a tool designed to easily manage the installation of rocks, and a server ...
, the Lua package manager which is also included with the Torch distribution.
Applications
Torch is used by the Facebook
AI Research Group,
IBM,
Yandex
Yandex LLC (russian: link=no, Яндекс, p=ˈjandəks) is a Russian multinational technology company providing Internet-related products and services, including an Internet search engine, information services, e-commerce, transportation, map ...
and the
Idiap Research Institute
The Idiap Research Institute is a semi-private non-profit research institute at Martigny in the Cantons of Switzerland, canton of Valais, in south-western Switzerland. It conducts research in the areas of speech processing, computer vision, info ...
. Torch has been extended for use on
Android
Android may refer to:
Science and technology
* Android (robot), a humanoid robot or synthetic organism designed to imitate a human
* Android (operating system), Google's mobile operating system
** Bugdroid, a Google mascot sometimes referred to ...
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
PyTorch is a machine learning framework based on the Torch library, used for applications such as computer vision and natural language processing, originally developed by Meta AI and now part of the Linux Foundation umbrella. It is free and op ...
References
External links
*
{{Lua programming language
Deep learning software
Free statistical software
Lua (programming language) software
Software using the BSD license