diff --git a/company-lua.el b/company-lua.el index 30065f3..3e61cd0 100644 --- a/company-lua.el +++ b/company-lua.el @@ -47,7 +47,8 @@ :type '(choice (const :tag "Lua 5.1" lua51) (const :tag "Lua 5.2" lua52) (const :tag "Lua 5.2" lua53) - (const :tag "LÖVE" love)) + (const :tag "LÖVE" love) + (const :tag "Torch" torch)) :safe #'symbolp) (defconst company-lua-complete-script @@ -91,7 +92,7 @@ (company-lua--parse-output)))))))))) (defun company-lua--get-interpreter() - (if (memq company-lua-interpreter '(lua51 lua52 lua53 love)) + (if (memq company-lua-interpreter '(lua51 lua52 lua53 love torch)) (symbol-name company-lua-interpreter) "lua52")) diff --git a/lua/api/nn.lua b/lua/api/nn.lua new file mode 100644 index 0000000..ebb11db --- /dev/null +++ b/lua/api/nn.lua @@ -0,0 +1,744 @@ +--[[ Torch api for auto completion. + Author: Pushpendre Rastogi + Date: 4 July 2017 + License: Anyone is free to copy and modify the data below, + but they should reproduce this header in their modified copy. +]] +return { + nn = { + type = "lib", + description = "This package provides an easy and modular way to build and train simple or complex neural networks using", + childs = { + Container = { + type = "class", + description = [[ This is an abstract Module class which declares methods defined in all containers. It reimplements many of the Module methods such that calls are propagated to the contained modules. For example, a call to zeroGradParameters will be propagated to all contained modules.]], + childs = { + add = { + type = "function", + args = ":(module)", + returns = "(container)", + description = "Adds a module to container" + }, + get = { + type = "function", + args = "(index: Integer)", + returns = "(module: Module)", + description = "Returns the contained module at index" + }, + size = { + type = "function", + args = ":()", + returns = "(size: Int)", + description = "" + }, + }, + }, + Sequential = { + type = "Container", + args = "", + returns = "", + description = "Plugs layers in a feed-forward fully connected manner." + }, + Parallel = { + type = "Container", + args = "(inputDimension, outputDimension)", + returns = "", + description = [[Applies its ith child module to the ith slice of the input Tensor + +Creates a container module that applies its `ith` child module to the `ith` slice of the input Tensor by using [select](https://github.com/torch/torch7/blob/master/doc/tensor.md#tensor-selectdim-index) +on dimension `inputDimension`. It concatenates the results of its contained modules together along dimension `outputDimension`. + +Example: +```lua +mlp = nn.Parallel(2,1); -- Parallel container will associate a module to each slice of dimension 2 + -- (column space), and concatenate the outputs over the 1st dimension. + +mlp:add(nn.Linear(10,3)); -- Linear module (input 10, output 3), applied on 1st slice of dimension 2 +mlp:add(nn.Linear(10,2)) -- Linear module (input 10, output 2), applied on 2nd slice of dimension 2 + + -- After going through the Linear module the outputs are + -- concatenated along the unique dimension, to form 1D Tensor +> mlp:forward(torch.randn(10,2)) -- of size 5. +-0.5300 +-1.1015 + 0.7764 + 0.2819 +-0.6026 +[torch.Tensor of dimension 5] +``` + +A more complicated example: +```lua + +mlp = nn.Sequential(); +c = nn.Parallel(1,2) -- Parallel container will associate a module to each slice of dimension 1 + -- (row space), and concatenate the outputs over the 2nd dimension. + +for i=1,10 do -- Add 10 Linear+Reshape modules in parallel (input = 3, output = 2x1) + local t=nn.Sequential() + t:add(nn.Linear(3,2)) -- Linear module (input = 3, output = 2) + t:add(nn.Reshape(2,1)) -- Reshape 1D Tensor of size 2 to 2D Tensor of size 2x1 + c:add(t) +end + +mlp:add(c) -- Add the Parallel container in the Sequential container + +pred = mlp:forward(torch.randn(10,3)) -- 2D Tensor of size 10x3 goes through the Sequential container + -- which contains a Parallel container of 10 Linear+Reshape. + -- Each Linear+Reshape module receives a slice of dimension 1 + -- which corresponds to a 1D Tensor of size 3. + -- Eventually all the Linear+Reshape modules' outputs of size 2x1 + -- are concatenated alond the 2nd dimension (column space) + -- to form pred, a 2D Tensor of size 2x10. + +> pred +-0.7987 -0.4677 -0.1602 -0.8060 1.1337 -0.4781 0.1990 0.2665 -0.1364 0.8109 +-0.2135 -0.3815 0.3964 -0.4078 0.0516 -0.5029 -0.9783 -0.5826 0.4474 0.6092 +[torch.DoubleTensor of size 2x10] + + +for i = 1, 10000 do -- Train for a few iterations + x = torch.randn(10,3); + y = torch.ones(2,10); + pred = mlp:forward(x) + + criterion = nn.MSECriterion() + local err = criterion:forward(pred,y) + local gradCriterion = criterion:backward(pred,y); + mlp:zeroGradParameters(); + mlp:backward(x, gradCriterion); + mlp:updateParameters(0.01); + print(err) +end +``` + +]] + }, + Concat = { + type = "Container", + args = "(dim)", + returns = "", + description = [[ Concatenates in one layer several modules along dimension dim + +Concat concatenates the output of one layer of "parallel" modules along the +provided dimension `dim`: they take the same inputs, and their output is +concatenated. + +]] + }, + NaN = { + type = "Container", + args = "", + returns = "", + description = "Decorates module to detect the source of NaN error" + }, + Profile = { + type = "Container", + args = "", + returns = "", + description = "Decorates module to time its forward and backwards passes." + }, + Jacobian = { + type = "lib", + description = [[ nn.Jacobian` class for testing the derivatives of their class, together with the [torch.Tester](https://github.com/torch/torch7/blob/master/doc/tester.md) class. ]], + childs = { + testJacobian = {args = "(module, input, minval, maxval, perturbation)", type = "function", returns = "", description = [[ + +Test the jacobian of a module w.r.t. to its input. + +`module` takes as its input a random tensor shaped the same as `input`. +`minval` and `maxval` specify the range of the random tensor ([-2, 2] by default). +`perturbation` is used as finite difference (1e-6 by default). + +Returns the L-inf distance between the jacobian computed by backpropagation and by finite difference. +]] + }, + testJacobianParameters = {args = "(module, input, param, dparam, minval, maxval, perturbation)", type = "function", returns = "", description = [[ + +Test the jacobian of a module w.r.t. its parameters (instead of its input). + +The input and parameters of `module` are random tensors shaped the same as `input` and `param`. +`minval` and `maxval` specify the range of the random tensors ([-2, 2] by default). +`dparam` points to the gradient w.r.t. parameters. +`perturbation` is used as finite difference (1e-6 by default). + +Returns the L-inf distance between the jacobian computed by backpropagation and by finite difference.]] + }, + testJacobianUpdateParameters = {args = "(module, input, param, minval, maxval, perturbation)", type = "function", returns = "", description = [[ + +Test the amount of update of a module to its parameters. + +The input and parameters of `module` are random tensors shaped the same as `input` and `param`. +`minval` and `maxval` specify the range of the random tensors ([-2, 2] by default). +`perturbation` is used as finite difference (1e-6 by default). + +Returns the L-inf distance between the update computed by backpropagation and by finite difference. + +]] + }, + forward = {args = "(module, input, param, perturbation)", type = "function", returns = "", description = [[ + +Compute the jacobian by finite difference. + +`module` has parameters `param` and input `input`. +If provided, `param` is regarded as independent variables, otherwise `input` is the independent variables. +`perturbation` is used as finite difference (1e-6 by default). + +Returns the jacobian computed by finite difference. +]] + }, + backward = {args = "(module, input, param, dparam)", type = "function", returns = "", description = [[ + +Compute the jacobian by backpropagation. + +`module` has parameters `param` and input `input`. +If provided, `param` is regarded as independent variables, otherwise `input` is the independent variables. +`dparam` is the gradient w.r.t. parameters, it must present as long as `param` is present. + +Returns the jacobian computed by backpropagation. +]] + }, + }, + }, + HardTanh = {args = "([min_value, max_value[, inplace]])", type = "function", returns = "Module", description = [[ ]]}, + HardShrink = {args = "([lambda])", type = "function", returns = "Module", description = [[ ]]}, + SoftShrink = {args = "([lambda])", type = "function", returns = "Module", description = [[ ]]}, + SoftMax = {args = "()", type = "function", returns = "Module", description = [[ ]]}, + SoftMin = {args = "()", type = "function", returns = "Module", description = [[ ]]}, + SoftPlus = {args = "()", type = "function", returns = "Module", description = [[ ]]}, + SoftSign = {args = "()", type = "function", returns = "Module", description = [[ ]]}, + LogSigmoid = {args = "()", type = "function", returns = "Module", description = [[ ]]}, + LogSoftMax = {args = "()", type = "function", returns = "Module", description = [[ ]]}, + Sigmoid = {args = "()", type = "function", returns = "Module", description = [[ ]]}, + Tanh = {args = "()", type = "function", returns = "Module", description = [[ ]]}, + ReLU = {args = "([inplace])", type = "function", returns = "Module", description = [[ ]]}, + ReLU6 = {args = "([inplace])", type = "function", returns = "Module", description = [[ ]]}, + PReLU = {args = "()", type = "function", returns = "Module", description = [[ ]]}, + RReLU = {args = "([l, u[, inplace]])", type = "function", returns = "Module", description = [[ ]]}, + CReLU = {args = "(nInputDims, [inplace])", type = "function", returns = "Module", description = [[ ]]}, + ELU = {args = "([alpha[, inplace]])", type = "function", returns = "Module", description = [[ ]]}, + LeakyReLU = {args = "([negval[, inplace]])", type = "function", returns = "Module", description = [[ ]]}, + SpatialSoftMax = {args = "()", type = "function", returns = "Module", description = [[ ]]}, + AddConstant = {args = "(k[, inplace])", type = "function", returns = "Module", description = [[ ]]}, + MulConstant = {args = "(k[, inplace])", type = "function", returns = "Module", description = [[ ]]}, + Module = { + type = "class", + description = [[ `Module` is an abstract class which defines fundamental methods necessary for a training a neural network. Modules are [serializable]. Modules contain two states variables: [output] and [gradInput]. ]], + childs = { + forward = { + type = "function", + args = "(input: Tensor)", + returns = "(output: Tensor)", + description = [[Takes an `input` object, and computes the corresponding `output` of the module. In general `input` and `output` are [Tensors]. + +It is not advised to override this function. Instead, one should +implement [updateOutput(input)](#nn.Module.updateOutput) +function. The forward module in the abstract parent class +[Module](#nn.Module) will call `updateOutput(input)`.]] + }, + backward = { + type = "function", + args = "(input, gradOutput)", + returns = "(gradInput)", + description = [[A _backpropagation step_ consist in computing two kind of gradients +at `input` given `gradOutput` (gradients with respect to the +output of the module). This function simply performs this task using +two function calls: + + - A function call to [updateGradInput(input, gradOutput)](#nn.Module.updateGradInput). + - A function call to accGradParameters(input,gradOutput,scale) ]] + }, + updateOutput = { + type = "function", + args = "(input: Tensor)", + returns = "(t: Tensor)", + description = [[Computes the output using the current parameter set of the class and input. This function returns the result which is stored in the [output] field.]] + }, + updateGradInput = { + type = "function", + args = "(input, gradOutput)", + returns = "", + description = [[Computing the gradient of the module with respect to its own +input. This is returned in `gradInput`. Also, the [gradInput] state variable is updated accordingly. ]] + }, + accGradParameters = { + type = "function", + args = "", + returns = "(input, gradOutput, scale)", + description = [[Computing the gradient of the module with respect to its +own parameters. Many modules do not perform this step as they do not +have any parameters. The state variable name for the parameters is +module dependent. The module is expected to _accumulate_ the +gradients with respect to the parameters in some variable. This function works in tandem with `zeroGradParameters` and `updateParameters`]] + }, + zeroGradParameters = { + type = "function", + args = "()", + returns = "()", + description = "Zero the accumulated gradients" + }, + updateParameters = { + type = "function", + args = "(learningRate: Numeric)", + returns = "()", + description = [[If the module has parameters, this will update these parameters, according +to the accumulation of the gradients with respect to these parameters, +accumulated through [backward()](#nn.Module.backward) calls.]] + }, + parameters = { + type = "function", + args = "()", + returns = "[{weights}, {gradWeights}]", + description = [[This function should returns two tables. One for the learnable +parameters `{weights}` and another for the gradients of the energy +wrt to the learnable parameters `{gradWeights}`. + +Custom modules should override this function if they use learnable +parameters that are stored in tensors. +]] + }, + findModules = { + type = "function", + args = "(typename)", + returns = "{List of Modules}", + description = [[Find all instances of modules in the network of a certain `typename`. It returns a flattened list of the matching nodes, as well as a flattened list of the container modules for each matching node. + +Modules that do not have a parent container (ie, a top level nn.Sequential for instance) will return their `self` as the container. + +This function is very helpful for navigating complicated nested networks. For example, a didactic example might be; if you wanted to print the output size of all `nn.SpatialConvolution` instances: + +```lua +-- Construct a multi-resolution convolution network (with 2 resolutions): +model = nn.ParallelTable() +conv_bank1 = nn.Sequential() +conv_bank1:add(nn.SpatialConvolution(3,16,5,5)) +conv_bank1:add(nn.Threshold()) +model:add(conv_bank1) +conv_bank2 = nn.Sequential() +conv_bank2:add(nn.SpatialConvolution(3,16,5,5)) +conv_bank2:add(nn.Threshold()) +model:add(conv_bank2) +-- FPROP a multi-resolution sample +input = {torch.rand(3,128,128), torch.rand(3,64,64)} +model:forward(input) +-- Print the size of the Threshold outputs +conv_nodes = model:findModules('nn.SpatialConvolution') +for i = 1, #conv_nodes do + print(conv_nodes[i].output:size()) +end +``` + +Another use might be to replace all nodes of a certain `typename` with another. For instance, if we wanted to replace all `nn.Threshold` with `nn.Tanh` in the model above: + +```lua +threshold_nodes, container_nodes = model:findModules('nn.Threshold') +for i = 1, #threshold_nodes do + -- Search the container for the current threshold node + for j = 1, #(container_nodes[i].modules) do + if container_nodes[i].modules[j] == threshold_nodes[i] then + -- Replace with a new instance + container_nodes[i].modules[j] = nn.Tanh() + end + end +end +``` +]] + }, + apply = { + type = "function", + args = ":(f::Module -> Void)", + returns = "()", + description = [[Calls provided function on itself and all child modules. This function takes +module to operate on as a first argument: + +```lua +model:apply(function(module) + module.train = true +end) +``` + +In the example above `train` will be set to to `true` in all modules of `model`. +This is how `training()` and `evaluate()` functions implemented.]] + }, + replace = { + type = "function", + args = ":(f::Module -> Void)", + returns = "", + description = [[Similar to apply takes a function which applied to all modules of a model, +but uses return value to replace the module. Can be used to replace all +modules of one type to another or remove certain modules. + +For example, can be used to remove `nn.Dropout` layers by replacing them with +`nn.Identity`: + +```lua +model:replace(function(module) + if torch.typename(module) == 'nn.Dropout' then + return nn.Identity() + else + return module + end +end) +``` +]] + }, + }, + }, + Linear = { type = "Module", returns = "Module", args = "(inputDimension, outputDimension, [bias = true])", description = [[ ]]}, + LinearWeightNorm = { type = "Module", returns = "Module", args = "(inputDimension, outputDimension, [bias = true])", description = [[LinearWeightNorm implements the reparametrization presented in [Weight Normalization](https://arxiv.org/pdf/1602.07868v3.pdf), which decouples the length of neural network weight vectors from their direction. The weight vector `w` is determined instead by parameters `g` and `v` such that `w = g * v / ||v||`, where `||v||` is the euclidean norm of vector `v`. In all other respects this layer behaves like `nn.Linear`. + +To convert between `nn.Linear` and `nn.LinearWeightNorm` you can use the `nn.LinearWeightNorm.fromLinear(linearModule)` and `weightNormModule:toLinear()` functions. + ]] + }, + SparseLinear = { type = "Module", returns = "Module", args = "(inputDimension, outputDimension)", description = [[Applies a linear transformation to the incoming sparse data, i.e. `y = Ax + b`. The `input` tensor given in `forward(input)` must be a sparse vector represented as 2D tensor of the form torch.Tensor(N, 2) where the pairs represent indices and values. The first column contains indices, the second column contains values in a a vector where all other elements are zeros. + +The SparseLinear layer is useful when the number of input dimensions is very large and the input data is sparse. + ]]}, +IndexLinear = { type = "Module", returns = "Module", args = "(inputSize, outputSize, doGradInput, keysOffset, weight, bias, normalize)", description = [[ Applies the following transformation to the incoming (optionally) normalized sparse input data: +`z = Weight * y + bias`, where +- `y_i = normalize and (x_i * (1 / x_i_max) + b_i) or x_i` +- `x_i` is the `i'th` feature of the input, +- `b_i` is a per-feature bias, +- `x_i_max` is the maximum absolute value seen so far during training for feature `i`. + +The normalization of input features is very useful to avoid explosions during training if sparse input values are really high. It also helps ditinguish between the presence and the absence of a given feature. +- `inputSize` is the maximum number of features. +- `outputSize` is the number of output neurons. +- `doGradInput`, if `false` (the default), the gradInput will not be computed. +- `keysOffset` lets you specify input keys are in the `[1+keysOffset, N+keysOffset]` range. (defaults to `0`) +- `weight` and `bias` allow you to create the module with existing weights without using additional memory. + When passing `weight` and `bias`, `inputSize` and `outputSize` are inferred from the weights. +- `normalize` will activate the normalization of the input feature values. (`false` by default) + +#### Differences from SparseLinear #### +- The layout of `weight` is transposed compared to `SparseLinear`. This was done for performance considerations. +- The `gradWeight` that is computed for in-place updates is a sparse representation of the whole gradWeight matrix. Its size changes from one +backward pass to another. This was done for performance considerations. +- The input format differs from the [SparseLinear](#nn.SparseLinear) input format by accepting keys and values as a table of tensors. This enables `IndexLinear` to have a larger range for keys than `SparseLinear`. + +The `input` tensors must be in one of the following formats. + +- An array of size 2 containing a batch of `keys` followed by a batch of `values`. +```lua +x = { + { torch.LongTensor({ 1, 200 }), torch.LongTensor({ 100, 200, 1000 }) }, + { torch.Tensor({ 1, 0.1 }), torch.Tensor({ 10, 0.5, -0.5 }) } +} +``` + +- an array of size 3 containing a flattened (pre-concatenated) batch of `keys`, followed by `values`, and `sizes`. +```lua +-- Equivalent to the input shown above +x = { + torch.LongTensor({ 1, 200, 100, 200, 1000 }), + torch.Tensor({ 1, 0.1, 10, .5, -0.5 }), + torch.LongTensor({ 2, 3 }) +} +``` +]]}, +Bilinear = { type = "Module", returns = "Module", args = "(inputDimension1, inputDimension2, outputDimension, [bias = true])", description = [[Applies a bilinear transformation to the incoming data, i.e. `\forall k: y_k = x_1 A_k x_2 + b`. The `input` tensor given in `forward(input)` is a table containing both inputs `x_1` and `x_2`, which are tensors of size `N x inputDimension1` +and `N x inputDimension2`, respectively. The layer can be trained without biases by setting `bias = false`. + ]]}, +PartialLinear = { type = "Module", returns = "Module", args = "(inputSize, outputSize, [bias = true])", description = [[ PartialLinear is a Linear layer that allows the user to a set a collection of +column indices. When the column indices are set, the layer will behave like a +Linear layer that only has those columns. Meanwhile, all parameters are +preserved, so resetting the PartialLinear layer will result in a module that +behaves just like a regular Linear layer. + +This module is useful, for instance, when you want to do forward-backward on +only a subset of a Linear layer during training but use the full Linear layer +at test time. + +You can create a layer in the following way: + +```lua + module = nn.PartialLinear(5, 3) -- 5 inputs, 3 outputs +``` + +Input data for this layer would look as follows: +```lua + input = torch.randn(128, 5) -- 128 input examples + module:forward(input) +``` + +One can set the partition of indices to compute using the function `setPartition(indices)` where `indices` is a tensor containing the indices to compute. +```lua +module = nn.PartialLinear(5, 3) -- 5 inputs, 3 outputs +module:setPartition(torch.Tensor({2,4})) -- only compute the 2nd and 4th indices out of a total of 5 indices +``` + +One can reset the partition via the `resetPartition()` function that resets the partition to compute all indices, making it's behaviour equivalent to `nn.Linear` + +]]}, +Dropout = { type = "Module", returns = "Module", args = "(p=0.5)", description = [[ During training, `Dropout` masks parts of the `input` using binary samples from a [bernoulli](http://en.wikipedia.org/wiki/Bernoulli_distribution) distribution. +Each `input` element has a probability of `p` of being dropped, i.e having its commensurate output element be zero. This has proven an effective technique for regularization and preventing the co-adaptation of neurons (see [Hinton et al. 2012](http://arxiv.org/abs/1207.0580)). + +Furthermore, the outputs are scaled by a factor of `1/(1-p)` during training. This allows the `input` to be simply forwarded as-is during evaluation. +]]}, +Add = { type = "Module", returns = "Module", args = "(inputDimension, scalar)", description = [[Applies a bias term to the incoming data, i.e. `yi = x_i + b_i`, or if `scalar = true` then uses a single bias term, `yi = x_i + b`. So if `scalar = true` then `inputDimension` value will be disregarded. + +Full Example of training the bias +```lua +y = torch.Tensor(5) +mlp = nn.Sequential() +mlp:add(nn.Add(5)) + +function gradUpdate(mlp, x, y, criterion, learningRate) + local pred = mlp:forward(x) + local err = criterion:forward(pred, y) + local gradCriterion = criterion:backward(pred, y) + mlp:zeroGradParameters() + mlp:backward(x, gradCriterion) + mlp:updateParameters(learningRate) + return err +end + +for i = 1, 10000 do + x = torch.rand(5) + y:copy(x); + for i = 1, 5 do y[i] = y[i] + i; end + err = gradUpdate(mlp, x, y, nn.MSECriterion(), 0.01) +end + +print(mlp:get(1).bias) +``` + +gives the output: + +```lua + 1.0000 + 2.0000 + 3.0000 + 4.0000 + 5.0000 +[torch.Tensor of dimension 5] +``` + + +]]}, +CAdd = { type = "Module", returns = "Module", args = "(size)", description = [[Applies a component-wise addition to the incoming data, i.e. `y_i = x_i + b_i`. Argument `size` can be one or many numbers (sizes) or a `torch.LongStorage`. For example, `nn.CAdd(3,4,5)` is equivalent to `nn.CAdd(torch.LongStorage{3,4,5})`. If the size for a particular dimension is 1, the addition will be expanded along the entire axis. + ]]}, +Mul = { type = "Module", returns = "Module", args = "()", description = [[Applies a _single_ scaling factor to the incoming data, i.e. `y = w x`, where `w` is a scalar. ]]}, +CMul = { type = "Module", returns = "Module", args = "(size)", description = [[Applies a component-wise multiplication to the incoming data, i.e. `y_i = w_i * x_i`. Argument `size` can be one or many numbers (sizes) or a `torch.LongStorage`. For example, `nn.CMul(3,4,5)` is equivalent to `nn.CMul(torch.LongStorage{3,4,5})`. +If the size for a particular dimension is 1, the multiplication will be expanded along the entire axis. ]]}, +Max = { type = "Module", returns = "Module", args = "(dimension, nInputDim)", description = [[Applies a max operation over dimension `dimension`. +Hence, if an `nxpxq` Tensor was given as input, and `dimension` = `2` then an `nxq` matrix would be output. +When `nInputDim` is provided, inputs larger than that value will be considered batches where the actual `dimension` to apply the max operation will be dimension `dimension + 1`. + ]]}, +Min = { type = "Module", returns = "Module", args = "(dimension, nInputDim)", description = [[ Applies a min operation over dimension `dimension`. +Hence, if an `nxpxq` Tensor was given as input, and `dimension` = `2` then an `nxq` matrix would be output. +When `nInputDim` is provided, inputs larger than that value will be considered batches where the actual `dimension` to apply the min operation will be dimension `dimension + 1`. +]]}, +Mean = { type = "Module", returns = "Module", args = "(dimension, nInputDim)", description = [[Applies a mean operation over dimension `dimension`. +Hence, if an `nxpxq` Tensor was given as input, and `dimension` = `2` then an `nxq` matrix would be output. +When `nInputDim` is provided , inputs larger than that value will be considered batches where the actual `dimension` to apply the sum operation will be dimension `dimension + 1`. +This module is based on [nn.Sum](#nn.Sum). + ]]}, +Sum = { type = "Module", returns = "Module", args = "(dimension, nInputDim, sizeAverage, squeeze)", description = [[Applies a sum operation over dimension `dimension`. +Hence, if an `nxpxq` Tensor was given as input, and `dimension` = `2` then an `nxq` matrix would be output. If argument `squeeze` is set to `false` then the output would be of size `nx1xq`. +When `nInputDim` is provided , inputs larger than that value will be considered batches where the actual `dimension` to apply the sum operation will be dimension `dimension + 1`. +Negative indexing is allowed by providing a negative value to `nInputDim`. +When `sizeAverage` is provided, the sum is divided by the size of the input in this `dimension`. This is equivalent to the mean operation performed by the [nn.Mean](#nn.Mean) module. ]]}, +Euclidean = { type = "Module", returns = "Module", args = "(inputSize,outputSize)", description = [[Outputs the Euclidean distance of the input to `outputSize` centers, i.e. this layer has the weights `w_j`, for `j` = `1`,..,`outputSize`, where `w_j` are vectors of dimension `inputSize`. + +The distance `y_j` between center `j` and input `x` is formulated as `y_j = || w_j - x ||`. + ]]}, +WeightedEuclidean = { type = "Module", returns = "Module", args = "(inputSize,outputSize)", description = [[ This module is similar to [Euclidean](#nn.Euclidean), but additionally learns a separate diagonal covariance matrix across the features of the input space _for each center_. + +In other words, for each of the `outputSize` centers `w_j`, there is a diagonal covariance matrices `c_j`, for `j` = `1`,..,`outputSize`, where `c_j` are stored as vectors of size `inputSize`. + +The distance `y_j` between center `j` and input `x` is formulated as `y_j = || c_j * (w_j - x) ||`.]]}, +Cosine = { type = "Module", returns = "Module", args = "(inputSize,outputSize)", description = [[This module is similar to [Euclidean](#nn.Euclidean), but additionally learns a separate diagonal covariance matrix across the features of the input space _for each center_. + +In other words, for each of the `outputSize` centers `w_j`, there is a diagonal covariance matrices `c_j`, for `j` = `1`,..,`outputSize`, where `c_j` are stored as vectors of size `inputSize`. + +The distance `y_j` between center `j` and input `x` is formulated as `y_j = || c_j * (w_j - x) ||`. ]]}, +Kmeans = {type = "Module", returns = "Module", args = "(k, dim)", description = [[`k` is the number of centroids and `dim` is the dimensionality of samples. +The `forward` pass computes distances with respect to centroids and returns index of closest centroid. +Centroids can be updated using gradient descent. +Centroids can be initialized randomly or by using [kmeans++](https://en.wikipedia.org/wiki/K-means%2B%2B) algoirthm: + +```lua +km:initRandom(samples) -- Randomly initialize centroids from input samples. +km:initKmeansPlus(samples) -- Use Kmeans++ to initialize centroids. +``` +Example showing how to use Kmeans module to do standard Kmeans clustering. + +```lua +attempts = 10 +iter = 100 -- Number of iterations +bestKm = nil +bestLoss = math.huge +learningRate = 1 +for j=1, attempts do + local km = nn.Kmeans(k, dim) + km:initKmeansPlus(samples) + for i=1, iter do + km:zeroGradParameters() + km:forward(samples) -- sets km.loss + km:backward(samples, gradOutput) -- gradOutput is ignored + + -- Gradient Descent weight/centroids update + km:updateParameters(learningRate) + end + + if km.loss < bestLoss then + bestLoss = km.loss + bestKm = km:clone() + end +end +``` +`nn.Kmeans()` module maintains loss only for the latest forward. If you want to maintain loss over the whole dataset then you who would need do it my adding the module loss for every forward. + +You can also use `nn.Kmeans()` as an auxillary layer in your network. +A call to `forward` will generate an `output` containing the index of the nearest cluster for each sample in the batch. +The `gradInput` generated by `updateGradInput` will be zero. +]]}, +Identity = { type = "Module", returns = "Module", args = "()", description = [[ +```lua +pred_mlp = nn.Sequential() -- A network that makes predictions given x. +pred_mlp:add(nn.Linear(5, 4)) +pred_mlp:add(nn.Linear(4, 3)) +xy_mlp = nn.ParallelTable() -- A network for predictions and for keeping the +xy_mlp:add(pred_mlp) -- true label for comparison with a criterion +xy_mlp:add(nn.Identity()) -- by forwarding both x and y through the network. +mlp = nn.Sequential() -- The main network that takes both x and y. +mlp:add(xy_mlp) -- It feeds x and y to parallel networks; +mlp:add( -- and then applies the criterion. + nn.CriterionTable( + nn.MSECriterion())) +for i = 1, 100 do -- Do a few training iterations + x = torch.ones(5) -- Make input features. + y = torch.Tensor(3) + y:copy(x:narrow(1,1,3)) -- Make output label. + err = mlp:forward{x,y} -- Forward both input and output. + print(err) -- Print error from criterion. + + mlp:zeroGradParameters() -- Do backprop... + mlp:backward({x, y}) + mlp:updateParameters(0.05) +end +``` +]]}, +Copy = { type = "Module", returns = "Module", args = "(inputType, outputType, [forceCopy, dontCast])", description = [[This layer copies the input to output with type casting from `inputType` to `outputType`. Unless `forceCopy` is true, when the first two arguments are the same, the input isn't copied, only transferred as the output. +The default `forceCopy` is false. +When `dontCast` is true, a call to `nn.Copy:type(type)` will not cast the module's `output` and `gradInput` `Tensor`s to the new type. +The default is false. ]]}, +Narrow = { type = "Module", returns = "Module", args = "(dimension, offset, length)", description = [[ ]]}, +Replicate = { type = "Module", returns = "Module", args = "(nFeature [, dim, ndim])", description = [[This class creates an output where the input is replicated `nFeature` times along dimension `dim` (default 1). +There is no memory allocation or memory copy in this module. +It sets the [stride](https://github.com/torch/torch7/blob/master/doc/tensor.md#torch.Tensor.stride) along the `dim`th dimension to zero. +When provided, `ndim` should specify the number of non-batch dimensions. +This allows the module to replicate the same non-batch dimension `dim` for both batch and non-batch `inputs`. ]]}, +Reshape = { type = "Module", returns = "Module", args = "(dimension1, dimension2, ... [, batchMode])", description = [[Reshapes an `nxpxqx..` `Tensor` into a `dimension1xdimension2x...` `Tensor`, taking the elements row-wise. + +The optional last argument `batchMode`, when `true` forces the first dimension of the input to be considered the batch dimension, and thus keep its size fixed. +This is necessary when dealing with batch sizes of one. +When `false`, it forces the entire input (including the first dimension) to be reshaped to the input size. +Default `batchMode=nil`, which means that the module considers inputs with more elements than the produce of provided sizes, i.e. `dimension1xdimension2x...`, to be batches. ]]}, +View = { type = "Module", returns = "Module", args = "(sizes)", description = [[ ]]}, +Contiguous = { type = "Module", returns = "Module", args = "()", description = [[ ]]}, +Select = { type = "Module", returns = "Module", args = "(dim, index)", description = [[ ]]}, +MaskedSelect = { type = "Module", returns = "Module", args = "()", description = [[ ]]}, +Index = { type = "Module", returns = "Module", args = "(dim)", description = [[ ]]}, +Squeeze = { type = "Module", returns = "Module", args = "([dim, numInputDims])", description = [[ ]]}, +Unsqueeze = { type = "Module", returns = "Module", args = "(pos [, numInputDims])", description = [[Insert singleton dim (i.e., dimension 1) at position `pos`. +For an `input` with `dim = input:dim()`, there are `dim + 1` possible positions to insert the singleton dimension. +For example, if `input` is `3` dimensional `Tensor` in size `p x q x r`, then the singleton dim can be inserted at the following `4` positions +``` +pos = 1: 1 x p x q x r +pos = 2: p x 1 x q x r +pos = 3: p x q x 1 x r +pos = 4: p x q x r x 1 +``` ]]}, +Transpose = { type = "Module", returns = "Module", args = "({dim1, dim2} [, {dim3, dim4}, ...])", description = [[Swaps dimension `dim1` with `dim2`, then `dim3` with `dim4`, and so on. So + +```lua +nn.Transpose({dim1, dim2}, {dim3, dim4}):forward(t) +``` + +gives the same output as + +```lua +t:transpose(dim1, dim2) +t:transpose(dim3, dim4) +``` + +The method `setNumInputDims()` allows to specify the expected number of dimensions of the inputs of the modules. This makes it possible to use minibatch inputs. ]]}, +Exp = { type = "Module", returns = "Module", args = "()", description = [[ ]]}, +Log = { type = "Module", returns = "Module", args = "()", description = [[ ]]}, +Square = { type = "Module", returns = "Module", args = "()", description = [[ ]]}, +Sqrt = { type = "Module", returns = "Module", args = "()", description = [[ ]]}, +Power = { type = "Module", returns = "Module", args = "(p)", description = [[ ]]}, +Clamp = { type = "Module", returns = "Module", args = "(min_value, max_value)", description = [[ ]]}, +Normalize = { type = "Module", returns = "Module", args = "(p, [eps])", description = [[ ]]}, +MM = { type = "Module", returns = "Module", args = "(transA, transB)", description = [[ ]]}, +BatchNormalization = { type = "Module", returns = "Module", args = "(N [, eps] [, momentum] [,affine])", description = [[where `N` is the dimensionality of input +`eps` is a small value added to the standard-deviation to avoid divide-by-zero. Defaults to `1e-5`. +`affine` is a boolean. When set to false, the learnable affine transform is disabled. Defaults to true + +During training, this layer keeps a running estimate of its computed mean and std. +The running sum is kept with a default momentum of 0.1 (unless over-ridden) +During evaluation, this running mean/std is used for normalization. + +Implements Batch Normalization as described in [the paper](http://arxiv.org/pdf/1502.03167v3.pdf): "Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift" by Sergey Ioffe, Christian Szegedy. + +The operation implemented is: + +```lua + x - mean(x) +y = ----------------------------- * gamma + beta + standard-deviation(x) + eps +``` + +where the mean and standard-deviation are calculated per-dimension over the mini-batches and where gamma and beta are learnable parameter vectors of size `N` (where `N` is the input size). +The learning of gamma and beta is optional. +The module only accepts 2D inputs. ]]}, +PixelShuffle = { type = "Module", returns = "Module", args = "(r)", description = [[ ]]}, +Padding = { type = "Module", returns = "Module", args = "(dim, pad [, nInputDim, value, index])", description = [[ ]]}, +L1Penalty = { type = "Module", returns = "Module", args = "(L1weight, sizeAverage)", description = [[L1Penalty is an inline module that in its forward propagation copies the input Tensor directly to the output, and computes an L1 loss of the latent state (input) and stores it in the module's `loss` field. +During backward propagation: `gradInput = gradOutput + gradLoss`. + +This module can be used in autoencoder architectures to apply L1 losses to internal latent state without having to use Identity and parallel containers to carry the internal code to an output criterion. + +Example (sparse autoencoder, note: decoder should be normalized): + +```lua +encoder = nn.Sequential() +encoder:add(nn.Linear(3, 128)) +encoder:add(nn.Threshold()) +decoder = nn.Linear(128, 3) + +autoencoder = nn.Sequential() +autoencoder:add(encoder) +autoencoder:add(nn.L1Penalty(l1weight)) +autoencoder:add(decoder) + +criterion = nn.MSECriterion() -- To measure reconstruction error +-- ... +``` ]]}, +GradientReversal = { type = "Module", returns = "Module", args = "([lambda = 1])", description = [[This module preserves the input, but takes the gradient from the subsequent layer, multiplies it by `-lambda` and passes it to the preceding layer. This can be used to maximise an objective function whilst using gradient descent, as described in "Domain-Adversarial Training of Neural Networks" (http://arxiv.org/abs/1505.07818). + ]]}, +TemporalDynamicKMaxPooling = { type = "Module", returns = "Module", args = "(minK, [factor])", description = [[ ]]}, +Constant = { type = "Module", returns = "Module", args = "(value, nInputDim)", description = [[ ]]}, +WhiteNoise = { type = "Module", returns = "Module", args = "([mean, stdev])", description = [[ ]]}, +OneHot = { type = "Module", returns = "Module", args = "(outputSize)", description = [[ ]]}, +PrintSize = { type = "Module", returns = "Module", args = "(name)", description = [[This module is useful for debugging complicated module composites. +It prints the size of the `input` and `gradOutput` during `forward` +and `backward` propagation respectively. +The `name` is a string used to identify the module along side the printed size. + ]]}, +ZeroGrad = { type = "Module", returns = "Module", args = "()", description = [[The module zeros the `gradInput` but forwards the `input` as-is. ]]}, +Collapse = { type = "Module", returns = "Module", args = "(nInputDim)", description = [[It collapses all non-batch dimensions. This is useful for converting +a spatial feature map to the single dimension required by a dense +hidden layer like Linear. + +This module is the equivalent of: +``` +view = nn.View(-1) +view:setNumInputDim(nInputDim) +```]]}, +Convert = { type = "Module", returns = "Module", args = "([inputShape, outputShape])", description = [[ ]]}, + }, + }, +} diff --git a/lua/api/torch.lua b/lua/api/torch.lua new file mode 100644 index 0000000..c1a3173 --- /dev/null +++ b/lua/api/torch.lua @@ -0,0 +1,723 @@ +--[[ Torch api for auto completion. + Author: Pushpendre Rastogi + Date: 4 July 2017 + License: Anyone is free to copy and modify the data below, + but they should reproduce this header in their modified copy. +]] +return { + torch = { + type = "lib", + description = [[__Torch__ is the main package in [Torch7](http://torch.ch) where data +structures for multi-dimensional tensors and mathematical operations +over these are defined. Additionally, it provides many utilities for +accessing files, serializing objects of arbitrary types and other +useful utilities.]], + childs = { + setdefaulttensortype = { + type = "function", + args = [[(s: String 'torch.[Float|Double|Int]Tensor')]], + returns = "()", + description = "Set the default tensor type throughout torch." + }, + isTensor = { + type = "function", + args = "(t:Any)", + returns = "(v: Boolean)", + description = "Is Tensor?" + }, + randn = { + type = "function", + args = "(x: Int, y: Int)", + returns = "(t: Tensor)", + description = "Returns a random tensor with standard guassian val" + }, + range = { + type = "function", + args = "(start: Int, end: Int)", + returns = "(t: Tensor)", + description = "1d Range" + }, + save = { + type = "function", + args = "(filename: String, obj: Object, [format: '(ascii|binary)', referenced: Boolean]", + returns = "()", + description = [[Writes `object` into a file named `filename`. The `format` can be set to +`ascii` or `binary` (default is binary). Binary format is platform +dependent, but typically more compact and faster to read/write. The ASCII +format is platform-independent, and should be used to share data structures +across platforms. The option `referenced` specifies if +[object references] should be tracked or not (`true` by default). + + +Sets the referenced property of the File to `ref`. `ref` has to be `true` +or `false`. + +By default `ref` is true, which means that a File object keeps track of +objects written (using [writeObject](#torch.File.writeObject) method) or +read (using [readObject](#torch.File.readObject) method). Objects with the +same address will be written or read only once, meaning that this approach +preserves shared memory structured. + +Keeping track of references has a cost: every object which is serialized in +the file is kept alive (even if one discards the object after +writing/reading) as File needs to track their pointer. This is not always a +desirable behavior, especially when dealing with large data structures. + +Another typical example when does not want reference tracking is when +one needs to push the same tensor repeatedly into a file but every time +changing its contents: calling `referenced(false)` ensures desired +behaviour.]] + }, + load = { + type = "function", + args = "(filename: String, [format, referenced])", + returns = "(obj: Any Type)", + description = [[Reads `object` from a file named `filename`. +The `format` can be set to `ascii`, `binary`, `b32` or `b64` (default is binary). +Binary format is platform dependent, but typically more compact and faster to read/write. +Use `b32`/`b64`, instead of `binary`, for loading files saved on a 32/64 bit OS. +The ASCII format is platform-independent, and may be used to share data structures across platforms. + +The option `referenced` specifies if [object references](file.md#torch.File.referenced) should be tracked or not (`true` by default). +Note that files written with `referenced` at `true` cannot be loaded with `referenced` at `false`.]] + }, + seed = { + type = "function", + args = "([rng: RNG])", + returns = "(seed: Long)", + description = [[Set the seed of the random number generator using `/dev/urandom` +(on Windows the time of the computer with granularity of seconds is used). +Returns the seed obtained. ]] + }, + manualSeed = { + type = "function", + args = "([rng: RNG, ] num: Long)", + returns = "()", + description = "Seed rng provided, or the global one, using `num`." + }, + rand = { + type = "function", + args = "", + returns = "", + description = "" + }, + randn = { + type = "function", + args = "", + returns = "", + description = "" + }, + randperm = { + type = "function", + args = "(n: Long)", + returns = "(t: Tensor)", + description = "returns a random permutation of integers from 1 to `n`." + }, + Generator = { + type = "function", + args = "()", + returns = "(rng: RNG)", + description = [[Creates a non-global random generator that carries its own state and can be +passed as the first argument to any function that generates a random number.]] + }, + serialize = { + type = "function", + args = "(object: Any Object, [format: String])", + returns = "(s: String)", + description = "Serializes `object` into a string." + }, + deserialize = { + type = "function", + args = "(object: Object, [format: String])", + returns = "(o: AnyObject)", + description = "" + }, + random = { + type = "function", + args = "([rng, start=1, end=2^32])", + returns = "(r: UInt32)", + description = "Return random int from [start, end)." + }, + uniform = { + type = "function", + args = "([gen, a=0, b=1])", + returns = "(r: Double)", + description = [[Returns a random real number according to uniform distribution on `[a,b)`.]] + }, + normal = { + type = "function", + args = "([gen, mean=0, stdev=1])", + returns = "(r: Double)", + description = "" + }, + exponential = { + type = "function", + args = "([gen,] lambda)", + returns = "(r: Double)", + description = "" + }, + cauchy = { + type = "function", + args = "([gen,] median, sigma)", + returns = "(r: Double)", + description = [[Returns a random real number according to the Cauchy distribution +`p(x) = sigma/(pi*(sigma^2 + (x-median)^2))`]] + }, + geometric = { + type = "function", + args = "([gen,] p)", + returns = "", + description = "" + }, + bernoulli = { + type = "function", + args = "([gen, p=0.5])", + returns = "", + description = "" + }, + Timer = { + type = "class", + description = [[This class is able to measure time (in seconds) elapsed in a particular period. Example: +```lua + timer = torch.Timer() -- the Timer starts to count now + x = 0 + for i=1,1000000 do + x = x + math.sin(x) + end + print('Time elapsed for 1,000,000 sin: ' .. timer:time().real .. ' seconds') +``` +]], + childs = { + reset = { + type = "function", + args = ":()", + returns = ":()", + description = "" + }, + resume = { + type = "function", + args = ":()", + returns = "()", + description = "" + }, + stop = { + type = "function", + args = ":()", + returns = "()", + description = "" + }, + time = { + type = "function", + args = ":()", + returns = ":(b: Table)", + description = [[Returns a table reporting the accumulated time elapsed until now. Following the UNIX shell `time` command, +there are three fields in the table: + * `real`: the wall-clock elapsed time. + * `user`: the elapsed CPU time. Note that the CPU time of a threaded program sums time spent in all threads. + * `sys`: the time spent in system usage.]] + }, + }, + }, + Tensor = { + type = "class", + description = [[A `Tensor` is a multi-dimensional matrix. The number of +dimensions is unlimited, up to what can be created using +[LongStorage](storage.md). The elements in the same row , i.e. along the last dimension, are contiguous in memory for a tensor.]], + args = "(n: Integer, m: Integer, ...)", + childs = { + nDimension = { + type = "function", + args = ":()", + returns = "(nDim: Integer)", + description = "Return the number of dimensions in tensor" + }, + dim = { + type = "function", + args = ":()", + returns = "(nDim: Integer)", + description = "Return the number of dimensions in tensor" + }, + size = { + type = "function", + args = "(i: Integer)", + returns = "(size: Integer)", + description = "Return the size along a dimension." + }, + apply = { + type = "function", + args = ":(f::(x:Numeric -> y:Numeric): function)", + returns = "()", + description = "Elementwise apply function inplace" + }, + zero = { + type = "function", + args = ":()", + returns = "()", + description = "Zero all elements of tensor inplace." + }, + fill = { + type = "function", + args = ":(x: Numeric)", + returns = "()", + description = "Fill tensor with x" + }, + narrow = { + type = "function", + args = ":(dim:Int, index:Int, size:Int)", + returns = "(t: Tensor)", + description = "Returns view of a slice of original tensor along dimension `dim` from `index` to `index+size` EXCLUSIVE." + }, + sub = { + type = "function", + args = ":(d1_start:Int, d1_end:Integer, ...)", + returns = "(t:Tensor)", + description = [[Returns a sub-tensor view of original tensor with di_start and di_end indices (INCLUSIVE) +> x = torch.Tensor(5, 6):zero() +> y = x:sub(2,4):fill(1) +> print(x) -- x has been modified! + + 0 0 0 0 0 0 + 1 1 1 1 1 1 + 1 1 1 1 1 1 + 1 1 1 1 1 1 + 0 0 0 0 0 0 +]] + }, + select = { + type = "function", + args = ":(dim:Int, index:int)", + returns = "(t: Tensor)", + description = "Returns the slice at `index` along dimension `dim`." + }, + copy = { + type = "function", + args = ":()", + returns = "(t:Tensor)", + description = "A copy of original tensor" + }, + contiguous = { + type = "function", + args = ":()", + returns = "(t: Tensor)", + description = "If the tensor is contiguous in storage then same reference otherwise copy tensor to contiguous memory and return that." + }, + type = { + type = "function", + args = ":([s: TensorType])", + returns = "(t: Tensor)", + description = "If no arg then type of tensor otherwise a new tensor with desired type" + }, + copy = { + type = "function", + args = ":(t: Tensor)", + returns = "(x: Tensor)", + description = "Copies values from t to x" + }, + indexCopy = { + type = "function", + args = ":(dim: Int, index:Int Or Tensor, T:Tensor)", + returns = "void", + description = [[ +> x + 0.8020 0.7246 0.1204 0.3419 0.4385 + 0.0369 0.4158 0.0985 0.3024 0.8186 + 0.2746 0.9362 0.2546 0.8586 0.6674 + 0.7473 0.9028 0.1046 0.9085 0.6622 + 0.1412 0.6784 0.1624 0.8113 0.3949 +[torch.DoubleTensor of dimension 5x5] + +z=torch.Tensor(5,2) +z:select(2,1):fill(-1) +z:select(2,2):fill(-2) +> z +-1 -2 +-1 -2 +-1 -2 +-1 -2 +-1 -2 +[torch.DoubleTensor of dimension 5x2] + +x:indexCopy(2,torch.LongTensor{5,1},z) +> x +-2.0000 0.7246 0.1204 0.3419 -1.0000 +-2.0000 0.4158 0.0985 0.3024 -1.0000 +-2.0000 0.9362 0.2546 0.8586 -1.0000 +-2.0000 0.9028 0.1046 0.9085 -1.0000 +-2.0000 0.6784 0.1624 0.8113 -1.0000 +[torch.DoubleTensor of dimension 5x5] +]] + }, + indexAdd = { + type = "function", + args = ":(dim:Int, index:Int or Tensor, t:Tensor)", + returns = "(t:Tensor)", + description = [[ +Accumulate the elements of `tensor` into the original tensor by adding to the indices in the order +given in `index`. The shape of `tensor` must exactly match the elements indexed or an error will be thrown. + +> x +-2.1742 0.5688 -1.0201 0.1383 1.0504 + 0.0970 0.2169 0.1324 0.9553 -1.9518 +-0.7607 0.8947 0.1658 -0.2181 -2.1237 +-1.4099 0.2342 0.4549 0.6316 -0.2608 + 0.0349 0.4713 0.0050 0.1677 0.2103 +[torch.DoubleTensor of size 5x5] + +z=torch.Tensor(5, 2) +z:select(2,1):fill(-1) +z:select(2,2):fill(-2) +> z +-1 -2 +-1 -2 +-1 -2 +-1 -2 +-1 -2 +[torch.DoubleTensor of dimension 5x2] + +> x:indexAdd(2,torch.LongTensor{5,1},z) +> x +-4.1742 0.5688 -1.0201 0.1383 0.0504 +-1.9030 0.2169 0.1324 0.9553 -2.9518 +-2.7607 0.8947 0.1658 -0.2181 -3.1237 +-3.4099 0.2342 0.4549 0.6316 -1.2608 +-1.9651 0.4713 0.0050 0.1677 -0.7897 +[torch.DoubleTensor of size 5x5] +]] + }, + maskedSelect = { + type = "function", + args = ":(mask:ByteTensor)", + returns = "(t: Tensor)", + description = "Select according to mask" + }, + maskedCopy = { + type = "function", + args = ":(mask: ByteTensor, src:Tensor)", + returns = "", + description = "Copy from `src` to tensor based on the mask." + }, + nonzero = { + type = "function", + args = ":()", + returns = "(t: LongTensor)", + description = "Returns N x d LongTensor where d is the number of dimensions that are nonzero." + }, + le = { + type = "function", + args = ":(x: Numeric or Tensor)", + returns = "(t: ByteTensor)", + description = "" + }, + expand = { + type = "function", + args = "(sizes: torch.LongStorage or Numbers)", + returns = "(t: Tensor)", + description = [[An expanded view of the original tensor. +Expanding a tensor does not allocate new memory, but only creates a +new view on the existing tensor where singleton dimensions can be +expanded to multiple ones by setting the `stride` to 0. + +x = torch.rand(2,1) +> x + 0.3837 + 0.5966 +y = torch.expand(x,2,2) +> y + 0.3837 0.3837 + 0.5966 0.5966 +]] + }, + repeatTensor = { + type = "function", + args = "([result: Tensor], sizes=Ints)", + returns = "(t: Tensor)", + description = [[ +`sizes` can either be a `torch.LongStorage` or numbers. Repeating a tensor allocates + new memory, unless `result` is provided, in which case its memory is + resized. `sizes` specify the number of times the tensor is repeated in each dimension. + + ```lua +x = torch.rand(5) +> x + 0.7160 + 0.6514 + 0.0704 + 0.7856 + 0.7452 +[torch.DoubleTensor of dimension 5] + +> torch.repeatTensor(x,3,2) + 0.7160 0.6514 0.0704 0.7856 0.7452 0.7160 0.6514 0.0704 0.7856 0.7452 + 0.7160 0.6514 0.0704 0.7856 0.7452 0.7160 0.6514 0.0704 0.7856 0.7452 + 0.7160 0.6514 0.0704 0.7856 0.7452 0.7160 0.6514 0.0704 0.7856 0.7452 +[torch.DoubleTensor of dimension 3x10] +]] + }, + squeeze = { + type = "function", + args = "([dim: Int])", + returns = "(t: Tensor)", + description = [[Removes all singleton dimensions of the tensor. +If `dim` is given, squeezes only that particular dimension of the tensor.]] + }, + permute = { + type = "function", + args = ":(dim1: Int, dim2: Int, ..., dimN: Int)", + returns = "(t: Tensor)", + description = [[ +Generalizes the function [transpose()](#torch.Tensor.transpose) and can be used +as a convenience method replacing a sequence of transpose() calls. +Returns a tensor where the dimensions were permuted according to the permutation +given by (dim1, dim2, ... , dimn). The permutation must be specified fully, i.e. +there must be as many parameters as the tensor has dimensions. +]] + }, + unfold = { + type = "function", + args = ":(dim: Int, size: Int, step: Int)", + returns = "(t: Tensor)", + description = [[ +Returns a tensor which contains all slices of size `size` in the dimension `dim`. Step between +two slices is given by `step`. An additional dimension of size `size` is appended in the returned tensor. +]] + }, + map = { + type = "function", + args = ":(x: Tensor, f::(a: Numeric, b: Numeric)->(c:Numeric): Function)", + returns = ":(t: Tensor)", + description = [[Apply the given function to all elements of self and `tensor`. The number of elements of both tensors must match, but sizes do not matter. Also see `map2`]] + }, + split = { + type = "function", + args = ":([result: table], size: Tensor Size, dim: Integer)", + returns = "(b: Table)", + description = [[Splits Tensor `tensor` along dimension `dim` +into a `result` table of Tensors of size `size` (a number) +or less (in the case of the last Tensor). The sizes of the non-`dim` +dimensions remain unchanged. +x = torch.randn(3,4,5) + +> x:split(2,1) +{ + 1 : DoubleTensor - size: 2x4x5 + 2 : DoubleTensor - size: 1x4x5 +} +]] + }, + chunk = { + type = "function", + args = ":([result: table], size: Tensor Size, dim: Integer)", + returns = "(b: Table)", + description = [[Splits tensor into n chunks of approx equal size, just like split function]] + }, + free = { + type = "function", + args = ":()", + returns = "()", + description = "Free the memory held by tensor." + }, + abs = {type = "function", args="", returns = "()", description = [[ ]]}, +acos = {type = "function", args="", returns = "()", description = [[ ]]}, +add = {type = "function", args="", returns = "()", description = [[ ]]}, +addbmm = {type = "function", args="", returns = "()", description = [[Batch matrix matrix product of matrices stored in `batch1` and `batch2`, with a reduced add step ]]}, +addcdiv = {type = "function", args = "([res,] x [,value], tensor1, tensor2)", returns = "()", description = [[Performs the element-wise division of `tensor1` by `tensor2`, multiply the result by the scalar `value` and add it to `x`. ]]}, +addcmul = {type = "function", args="", returns = "()", description = [[Performs the element-wise multiplication of `tensor1` by `tensor2`, multiply the result by the scalar `value` (1 if not present) and add it to `x`. ]]}, +addmm = {type = "function", args="([res,] [v1,] M, [v2,] mat1, mat2)", returns = "()", description = [[res = (v1 * M) + (v2 * mat1 * mat2) ]]}, +addmv = {type = "function", args="([res,] [v1,] vec1, [v2,] mat, vec2)", returns = "()", description = [[ res = (v1 * vec1) + (v2 * (mat * vec2))]]}, +addr = {type = "function", args="([res,] [v1,] mat, [v2,] vec1, vec2)", returns = "()", description = [[res_ij = (v1 * mat_ij) + (v2 * vec1_i * vec2_j) ]]}, +all = {type = "function", args="", returns = "(b: Boolean)", description = [[Logical and over allocate elements of ByteTensor ]]}, +any = {type = "function", args="", returns = "(b: Boolean)", description = [[ Logical OR over allocate elements of ByteTensor]]}, +asin = {type = "function", args="", returns = "()", description = [[ ]]}, +atan = {type = "function", args="", returns = "()", description = [[ ]]}, +atan2 = {type = "function", args="", returns = "()", description = [[ ]]}, +baddbmm = {type = "function", args="([res,] [v1,] M, [v2,] batch1, batch2)", returns = "()", description = [[res_i = (v1 * M_i) + (v2 * batch1_i * batch2_i) ]]}, +bhistc = {type = "function", args="([res,] x [,nbins=100, min_value=-inf, max_value=inf])", returns = "()", description = [[returns the histogram of the elements in 2d tensor `x` along the last dimension. with nbins ]]}, +bitand = {type = "function", args="(b: Boolean)", returns = "()", description = [[Performs bitwise `and` operation on all elements in the `Tensor` by the given `value`. ]]}, +bitor = {type = "function", args="", returns = "()", description = [[ ]]}, +bitxor = {type = "function", args="", returns = "()", description = [[ ]]}, +bmm = {type = "function", args="", returns = "()", description = [[ ]]}, +cat = {type = "function", args="cat( [res,] x_1, x_2, [dimension] )", returns = "()", description = [[ ]]}, +cbitand = {type = "function", args="cbitand([res,] tensor1, tensor2)", returns = "()", description = [[ ]]}, +cbitor = {type = "function", args="([res,] tensor1, tensor2)", returns = "()", description = [[ ]]}, +cbitxor = {type = "function", args="([res,] tensor1, tensor2)", returns = "()", description = [[ ]]}, +cdiv = {type = "function", args="([res,] tensor1, tensor2)", returns = "()", description = [[ ]]}, +ceil = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +cfmod = {type = "function", args="([res,] tensor1, tensor2)", returns = "()", description = [[ ]]}, +cinv = {type = "function", args="", returns = "()", description = [[ ]]}, +clamp = {type = "function", args="([res,] tensor, min_value, max_value)", returns = "()", description = [[ ]]}, +clshift = {type = "function", args="([res,] tensor1, tensor2)", returns = "()", description = [[ ]]}, +cmax = {type = "function", args="([res,] tensor, value|tensor)", returns = "()", description = [[ ]]}, +cmin = {type = "function", args="([res,] tensor, value)", returns = "()", description = [[ ]]}, +cmod = {type = "function", args="([res,] tensor1, tensor2)", returns = "()", description = [[ ]]}, +cmul = {type = "function", args="([res,] tensor1, tensor2)", returns = "()", description = [[ ]]}, +conv2 = {type = "function", args="([res,] x, k, [, 'F' or 'V'])", returns = "()", description = [[ ]]}, +conv3 = {type = "function", args="([res,] x, k, [, 'F' or 'V'])", returns = "()", description = [[ ]]}, +cos = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +cosh = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +cpow = {type = "function", args="([res,] tensor1, tensor2)", returns = "()", description = [[ ]]}, +cremainder = {type = "function", args="([res,] tensor1, tensor2)", returns = "()", description = [[ ]]}, +cross = {type = "function", args="([res,] a, b [,n])", returns = "()", description = [[ ]]}, +crshift = {type = "function", args="([res,] tensor1, tensor2)", returns = "()", description = [[ ]]}, +csub = {type = "function", args="", returns = "()", description = [[Subtracts the given value from all elements in the `Tensor`, in place. ]]}, +cumprod = {type = "function", args="([res,] x [,dim])", returns = "()", description = [[ ]]}, +cumsum = {type = "function", args="([res,] x [,dim])", returns = "()", description = [[ ]]}, +diag = {type = "function", args="([res,] x [,k])", returns = "()", description = [[ ]]}, +dist = {type = "function", args="(x, y, [p=2])", returns = "(n: Numeric)", description = [[ Return p-norm of x-y]]}, +div = {type = "function", args="([res,] tensor, value)", returns = "()", description = [[ ]]}, +dot = {type = "function", args="(t: Tensor)", returns = "(n: Numeric)", description = [[Elem wise multiply and reduced by sums]]}, +eig = {type = "function", args="([rese, resv,] a [, 'N' or 'V'])", returns = "(e, V)", description = [[`e, V = torch.eig(A)` returns eigenvalues and eigenvectors of a general real square matrix `A`. + +`A` and `V` are `m × m` matrices and `e` is a `m` dimensional vector. + +This function calculates all right eigenvalues (and vectors) of `A` such that `A = V diag(e) V'`. + +Third argument defines computation of eigenvectors or eigenvalues only. +If it is `'N'`, only eigenvalues are computed. +If it is `'V'`, both eigenvalues and eigenvectors are computed. + +The eigen values returned follow [LAPACK convention](https://software.intel.com/sites/products/documentation/hpc/mkl/mklman/GUID-16EB5901-5644-4DA6-A332-A052309010C4.htm) and are returned as complex (real/imaginary) pairs of numbers (`2 * m` dimensional `Tensor`). + ]]}, +eq = {type = "function", args="(t: Tensor)", returns = "(t: New ByteTensor)", description = [[ ]]}, +equal = {type = "function", args="(t: Tensor)", returns = "(b: Boolean)", description = [[ ]]}, +exp = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +eye = {type = "function", args="([res,] n [,m])", returns = "()", description = [[ ]]}, +floor = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +fmod = {type = "function", args="([res,] tensor, value)", returns = "()", description = [[ ]]}, +frac = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +ge = {type = "function", args="", returns = "(t: ByteTensor)", description = [[ ]]}, +gels = {type = "function", args="(b: Tensor, a: Tensor)", returns = "(x: Tensor)", description = [[ Solution of least squares and least norm problems for a full rank `m × n` matrix `A`. + + * If `n ≤ m`, then solve `||AX-B||_F`. + * If `n > m` , then solve `min ||X||_F` s.t. `AX = B`. + +On return, first `n` rows of `x` matrix contains the solution and the rest contains residual information. +Square root of sum squares of elements of each column of `x` starting at row `n + 1` is the residual for corresponding column. + +Note: Irrespective of the original strides, the returned matrices `resb` and `resa` will be transposed, i.e. with strides `1, m` instead of `m, 1`. +]]}, +ger = {type = "function", args="([res,] vec1, vec2)", returns = "()", description = [[ ]]}, +gesv = {type = "function", args="([resb, resa,] B, A)", returns = "[x, lu]", description = [[ `X, LU = torch.gesv(B, A)` returns the solution of `AX = B` and `LU` contains `L` and `U` factors for `LU` factorization of `A`. + +If `resb` and `resa` are given, then they will be used for temporary storage and returning the result. + + * `resa` will contain `L` and `U` factors for `LU` factorization of `A`. + * `resb` will contain the solution `X`. +]]}, +gt = {type = "function", args="", returns = "()", description = [[ ]]}, +histc = {type = "function", args="([res,] x [,nbins, min_value, max_value])", returns = "()", description = [[ ]]}, +inverse = {type = "function", args="", returns = "()", description = [[ ]]}, +kthvalue = {type = "function", args="([resval, resind,] x, k [,dim])", returns = "(y: Tensor, i: LongTensor)", description = [[`y = torch.kthvalue(x, k)` returns the `k`-th smallest element of `x` over its last dimension. + +`y, i = torch.kthvalue(x, k, 1)` returns the `k`-th smallest element in each column (across rows) of `x`, and a `Tensor` `i` of their corresponding indices in `x`. + +`y, i = torch.kthvalue(x, k, 2)` performs the `k`-th value operation for each row. + +`y, i = torch.kthvalue(x, k, n)` performs the `k`-th value operation over the dimension `n`. + ]]}, +le = {type = "function", args="", returns = "()", description = [[ ]]}, +lerp = {type = "function", args="([res,] a, b, weight)", returns = "()", description = [[ ]]}, +linspace = {type = "function", args="([res,] x1, x2, [,n])", returns = "()", description = [[ ]]}, +log = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +log1p = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +logspace = {type = "function", args="([res,] x1, x2, [,n])", returns = "()", description = [[ ]]}, +lshift = {type = "function", args="([res,] tensor, value)", returns = "()", description = [[ ]]}, +lt = {type = "function", args="", returns = "()", description = [[ ]]}, +max = {type = "function", args="([resval, resind,] x [,dim])", returns = "(m: Tensor [, i: Tensor])", description = [[ +`y = torch.max(x)` returns the single largest element of `x`. + +`y, i = torch.max(x, 1)` returns the largest element in each column (across rows) of `x`, and a `Tensor` `i` of their corresponding indices in `x`. + +`y, i = torch.max(x, 2)` performs the max operation for each row. + +`y, i = torch.max(x, n)` performs the max operation over the dimension `n`. + +th> m = torch.range(1, 12):reshape(3,4) +th> a = torch.Tensor(4) +th> b = torch.LongTensor(4) +th> torch.max(a, b, m, 1) + 9 10 11 12 +[torch.DoubleTensor of size 1x4] + 3 3 3 3 +[torch.LongTensor of size 1x4] + +Now the results are stored in a, b +]]}, +mean = {type = "function", args="([res,] x [,dim])", returns = "()", description = [[ ]]}, +median = {type = "function", args="", returns = "()", description = [[ ]]}, +min = {type = "function", args="", returns = "()", description = [[ ]]}, +mm = {type = "function", args="([res,] mat1, mat2)", returns = "()", description = [[ ]]}, +mod = {type = "function", args="([res,] tensor, value)", returns = "()", description = [[ ]]}, +mode = {type = "function", args="", returns = "()", description = [[ ]]}, +mul = {type = "function", args="([res,] tensor1, value)", returns = "()", description = [[ ]]}, +multinomial = {type = "function", args="([res,], p, n, [,replacement])", returns = "()", description = [[ ]]}, +mv = {type = "function", args="([res,] mat, vec)", returns = "()", description = [[ ]]}, +ne = {type = "function", args="", returns = "()", description = [[ ]]}, +neg = {type = "function", args="", returns = "()", description = [[ ]]}, +norm = {type = "function", args="", returns = "()", description = [[ ]]}, +numel = {type = "function", args="", returns = "()", description = [[ ]]}, +ones = {type = "function", args="([res,] m [,n...])", returns = "()", description = [[ ]]}, +orgqr = {type = "function", args="", returns = "()", description = [[ ]]}, +ormqr = {type = "function", args="", returns = "()", description = [[ ]]}, +potrf = {type = "function", args="", returns = "()", description = [[ ]]}, +potri = {type = "function", args="", returns = "()", description = [[ ]]}, +potrs = {type = "function", args="", returns = "()", description = [[ ]]}, +pow = {type = "function", args="([res,] x, n)", returns = "()", description = [[ ]]}, +prod = {type = "function", args="([res,] x [,n])", returns = "()", description = [[ ]]}, +pstrf = {type = "function", args="", returns = "()", description = [[ ]]}, +qr = {type = "function", args="", returns = "()", description = [[ ]]}, +rand = {type = "function", args="([res,] [gen,] m [,n...])", returns = "()", description = [[ ]]}, +randn = {type = "function", args="([res,] [gen,] m [,n...])", returns = "()", description = [[ ]]}, +randperm = {type = "function", args="([res,] [gen,] n)", returns = "()", description = [[ ]]}, +range = {type = "function", args="([res,] x, y [,step])", returns = "()", description = [[ ]]}, +remainder = {type = "function", args="([res,] tensor, value)", returns = "()", description = [[ ]]}, +renorm = {type = "function", args="", returns = "()", description = [[ ]]}, +reshape = {type = "function", args="([res,] x, m [,n...])", returns = "()", description = [[ ]]}, +round = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +rshift = {type = "function", args="([res,] tensor, value)", returns = "()", description = [[ ]]}, +rsqrt = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +sigmoid = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +sign = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +sin = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +sinh = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +sort = {type = "function", args="", returns = "()", description = [[ ]]}, +sqrt = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +std = {type = "function", args="([res,] x, [,dim] [,flag])", returns = "()", description = [[ ]]}, +sum = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +svd = {type = "function", args="", returns = "()", description = [[ ]]}, +symeig = {type = "function", args="", returns = "()", description = [[ ]]}, +tan = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +tanh = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +topk = {type = "function", args="([resval, resind,] x, k, [,dim] [,dir] [,sort])", returns = "(y, i)", description = [[`y, i = torch.topk(x, k)` returns all `k` smallest elements in `x` over its last dimension including their indices, in unsorted order. + +`y, i = torch.topk(x, k, dim, dir, true)` specifies that the results in `y` should be sorted with respect to `dir`; by default, the results are potentially unsorted since the computation may be faster, but if sorting is desired, the sort flag may be passed, in which case the results are returned from smallest to `k`-th smallest (`dir == false`) or highest to `k`-th highest (`dir == true`). +]]}, +trace = {type = "function", args="", returns = "()", description = [[ ]]}, +tril = {type = "function", args="([res,] x [,k])", returns = "()", description = [[ ]]}, +triu = {type = "function", args="([res,] x, [,k])", returns = "()", description = [[ ]]}, +trtrs = {type = "function", args="[x]", returns = "([resb, resa,] b, a [, 'U' or 'L'] [, 'N' or 'T'] [, 'N' or 'U'])", description = [[`X = torch.trtrs(B, A)` returns the solution of `AX = B` where `A` is upper-triangular. + +`A` has to be a square, triangular, non-singular matrix (2D `Tensor`). +`A` and `resa` are `m × m`, `X` and `B` are `m × k`. +(To be very precise: `A` does not have to be triangular and non-singular, rather only its upper or lower triangle will be taken into account and that part has to be non-singular.) + +The function has several options: + +* `uplo` (`'U'` or `'L'`) specifies whether `A` is upper or lower triangular; the default value is `'U'`. +* `trans` (`'N'` or `'T`') specifies the system of equations: `'N'` for `A * X = B` (no transpose), or `'T'` for `A^T * X = B` (transpose); the default value is `'N'`. +* `diag` (`'N'` or `'U'`) `'U'` specifies that `A` is unit triangular, i.e., it has ones on its diagonal; `'N'` specifies that `A` is not (necessarily) unit triangular; the default value is `'N'`. + +If `resb` and `resa` are given, then they will be used for temporary storage and returning the result. +`resb` will contain the solution `X`. + +Note: Irrespective of the original strides, the returned matrices `resb` and `resa` will be transposed, i.e. with strides `1, m` instead of `m, 1`. + ]]}, +trunc = {type = "function", args="([res,] x)", returns = "()", description = [[ ]]}, +var = {type = "function", args="([res,] x [,dim] [,flag])", returns = "()", description = [[ ]]}, +xcorr2 = {type = "function", args="([res,] x, k, [, 'F' or 'V'])", returns = "()", description = [[ ]]}, +xcorr3 = {type = "function", args="([res,] x, k, [, 'F' or 'V'])", returns = "()", description = [[ ]]}, +zeros = {type = "function", args="", returns = "()", description = [[ ]]}, + }, + }, + }, + }, +} diff --git a/lua/complete.lua b/lua/complete.lua index 42d9113..3938636 100644 --- a/lua/complete.lua +++ b/lua/complete.lua @@ -62,6 +62,10 @@ local function generateList() if interpreter == "love" then table.insert(apis, "love2d") end + if interpreter == "torch" then + table.insert(apis, "torch") + table.insert(apis, "nn") + end for _, api in pairs(apis) do local status, module = pcall(require, "api/" .. api)