nninit: A focused weight initialisation toolkit for Torch7
Weight initialisation schemes for Torch7 neural network modules
At a glance
- What is it?
- nninit adds an init method to Torch7's nn.Module for applying a range of initialisation schemes to selected parameters. It is a small, MIT-licensed Lua library that works with nn, nngraph, and cudnn modules.
- Who is it for?
- Adopt nninit if you work in Torch7 and need a compact, flexible way to apply standard initialisation schemes to specific parameters of nn or cudnn modules. Its accessor system lets you target subtensors, which is useful for layers with tied or partitioned weights.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Probably not. The repository last received commits 114 months ago, on June 21, 2017.
- What is it written in?
- Mainly Lua, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 25, 2026, and from our analysis. They are not legal advice.
Star history
- Kaixhin/nninit100 ★ · +0
| Repository | 2026-08-18 | 2026-11-25 |
|---|---|---|
| Kaixhin/nninit | 100 | 100 |
0 Stars in the last 99 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
What nninit solves and who needs it
Training deep networks in Torch7 often required manual loops over module parameters to set initial weights. nninit removes that boilerplate by adding an init method to nn.Module. The library targets researchers and engineers who build models with nn, nngraph, or cudnn and want to apply proven initialisation schemes without writing custom tensor-filling code each time. It is not a general-purpose deep learning framework; it is a small utility that fills one gap in the Torch7 ecosystem. The README lists support for nn.Linear, nn.LookupTable, nn.TemporalConvolution, nn.SpatialConvolution, and nn.VolumetricConvolution, including cudnn variants. If you use only those module types, nninit covers your needs.
The init method and its accessors
The core API is module:init(accessor, initialiser, ...). The accessor determines which tensor gets initialised. A string accessor treats the argument as a property name, so module:init('weight', nninit.constant, 1) fills the weight tensor. A table accessor first gets the property and then applies Torch's indexing operator, which allows initialising a subtensor, as in {'weight', {{1, 5}, {}}}. A function accessor returns a tensor directly, giving full control, for example function(m) return m.weight:narrow(1, 1, 10) end. This design means you can initialise only part of a weight matrix, which is useful for layers with shared or partitioned weights. The initialiser function receives the module and tensor, modifies the tensor, and returns the module so calls can be chained. That chaining is a practical convenience when a module has both weight and bias to set.
Initialisers: from constant to convolutionAware
nninit ships with a set of initialiser functions. copy and constant are straightforward; addConstant and mulConstant modify the existing tensor rather than replacing it. normal and uniform fill with the corresponding distributions, and addNormal and addUniform add noise to the current values. eye fills weights with the identity matrix for linear layers and lookup tables, and with a Dirac delta for convolutional layers, normalising by the number of input layers. xavier uses stdv = gain * sqrt(2 / (fanIn + fanOut)) with a uniform distribution by default. kaiming uses stdv = gain * sqrt(1 / fanIn) with a normal distribution by default. orthogonal fills a tensor with a random orthogonal matrix, but only supports tensors with at least 2 dimensions. sparse sets a percentage of the tensor to zero. convolutionAware fills 2D convolution filters with matrices that are orthogonal in frequency space, but only supports symmetric filter sizes. Each initialiser is tied to a specific paper, which the README cites. That is useful for reproducibility.
Gains and distributions: handling nonlinearities
For xavier, kaiming, orthogonal, and convolutionAware, nninit supports a gain parameter that adjusts the scale based on the activation function. The gain can be a number or a string. The README maps 'linear' and 'sigmoid' to 1, 'tanh' to 5/3, 'relu' to sqrt(2), and 'lrelu' to sqrt(2 / (1 + leakiness^2)). For lrelu, you must pass a table with the leakiness value, like {gain = {'lrelu', leakiness = 0.3}}. The distribution for xavier and kaiming can be set to 'normal' or 'uniform'. The default for xavier is uniform, and for kaiming it is normal. The README notes that the gain for ReLU units has to be manually specified, for example with {gain = 'relu'}. This is a deliberate design choice: the library does not infer the activation from the module, so the user must know what gain to apply. That is a small extra burden but keeps the library simple.
Getting it running: installation and a concrete example
Installation is a single luarocks command: luarocks install nninit. The README shows a usage example that requires nn, cunn, cudnn, rnn, and nninit. The example builds a sequential CNN with a cudnn.SpatialConvolution layer and calls :init('weight', nninit.eye) on it. The example also defines a getBias function that returns module.bias, presumably to use as an accessor. The README is truncated, so the full example is not visible, but the pattern is clear: you chain init calls on the module after construction. One thing to note: the example requires cunn and cudnn, which are CUDA-specific. If you run on CPU only, you would adapt the example. The library itself works with nn, so CPU modules should work, but the README does not give a CPU-only example.
Limitations and cases where nninit is the wrong tool
The documentation is explicit about several constraints. orthogonal only supports tensors with at least 2 dimensions. convolutionAware only supports 2D convolutions with a symmetric filter size. eye relies on the module type to determine the appropriate identity, so it may not work for every module. sparse expects sparsity between 0 and 1, and the README's example says a sparsity of 0.2 drops out 80% of the tensor, which is counterintuitive but clearly stated. The bigger limitation is scope: nninit is tied to Torch7, which is no longer actively developed by the original team. If you are starting a new project, you would likely choose PyTorch or another framework, and nninit would be useless there. Even within Torch7, the list of supported modules is finite. If you use a custom module that does not have a weight or bias property in the expected way, you can use a function accessor, but you are on your own for the initialiser logic. The README does not mention any handling of shared parameters or weight tying beyond the accessor's ability to index a subtensor.
Alternatives and how they differ
The most direct alternative is to write your own initialisation code in Lua, using Torch's tensor operations directly. That gives you full control but requires repeating the same loops and indexing logic across projects. Another alternative is to use the initialisation functions that come with Torch7's nn package, if any exist, but the README does not mention them. In the broader ecosystem, PyTorch's torch.nn.init module offers a similar set of initialisers (xavier_uniform_, kaiming_normal_, orthogonal_, and so on) but with a different API: you call functions that take a tensor, rather than adding an init method to a module. PyTorch's approach is more integrated with its autograd system and covers more module types out of the box. The key difference is that nninit's accessor system lets you target subtensors and arbitrary properties with a uniform interface, whereas PyTorch's init functions expect you to access the tensor yourself. If you are staying in Torch7, nninit is a reasonable convenience; if you are open to switching frameworks, PyTorch's init module is a more modern and actively maintained alternative.
Maintenance, licence, and what to verify before adopting
The repository is not archived, but it has no published GitHub releases. That makes the maintenance status unclear. The README is truncated, which may hide additional documentation. The licence is MIT, which is permissive and allows commercial use, modification, and redistribution, but that is not legal advice. Before adopting, you should check whether the repository has recent commits or open issues that indicate responsiveness. You should also verify that the luarocks package installs on your Torch7 setup, since Torch7 itself has platform-specific build quirks. The library depends on nn, and the example also requires cunn and cudnn for GPU use, so you need those installed. If your project relies on a specific initialisation scheme that is not in the list, you would need to write a custom initialiser function, which the API supports. The lack of recent releases suggests that the library is stable but possibly unmaintained, so plan to vendor the code if you need long-term support.
Editorial conclusion
Adopt nninit if you work in Torch7 and need a compact, flexible way to apply standard initialisation schemes to specific parameters of nn or cudnn modules. Its accessor system lets you target subtensors, which is useful for layers with tied or partitioned weights. Do not use it if you have moved to PyTorch or another framework, or if you need initialisation schemes that are not listed here. Before adopting, verify that the version of Torch7 you use is still maintained and that nninit installs cleanly via luarocks, since the project has not seen recent releases and the README is truncated.
Frequently asked questions
What is nninit used for?
nninit provides parameter initialisation schemes for Torch7 neural network modules. It works with nn and therefore nngraph, and it allows arbitrary indexing of weights, biases, and parameters.
How do I install nninit?
Install it from LuaRocks with luarocks install nninit. The repository ships nninit.lua plus a rocks directory, and the published example requires nn, cunn, cudnn, rnn, and nninit.
Which Torch7 modules does nninit support?
nn.Linear and nn.LinearNoBias, nn.LookupTable, nn.TemporalConvolution, nn.SpatialConvolution and cudnn.SpatialConvolution, and nn.VolumetricConvolution and cudnn.VolumetricConvolution.
How do I set the gain for leaky ReLU in nninit?
Pass a table whose first element is the gain string and whose named element carries the extra parameter, for example {gain = {'lrelu', leakiness = 0.3}}. The documented mapping is 'linear' 1, 'sigmoid' 1, 'tanh' 5 / 3, 'relu' sqrt(2), and 'lrelu' sqrt(2 / (1 + leakiness^2)).
What does nninit.sparse do to a tensor?
It sets (1 - sparsity) percent of the tensor to 0, with sparsity between 0 and 1, so a sparsity of 0.2 drops out 80% of the tensor.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/kaixhin-nninit)Star history chart for your README
[](https://hysenlabs.com/projects/kaixhin-nninit)Was this useful?