Giter VIP home page Giter VIP logo

maxnorm's Introduction

maxnorm.py

This is a tiny wrapper adding a max norm constraint as discussed in Hinton et al. (2012) and Srivastava et al. (2014) for PyTorch (I've used it on PyTorch 1.13.1) that wraps it around a PyTorch optimizer, in this case torch.optim.SGD.

Usage

You can use MNSGD as a drop-in replacement for SGD, but you do need to pass it params as an iterable of dicts, and you need to pass 'max_norm' as keyword for each parameter group to which you want to apply the max-norm constraint, like this:

model = NeuralNetwork(dropout_p = 0.5).to(device)

loss_fn = nn.CrossEntropyLoss()
model = NeuralNetwork().to(device)
optimizer = MNSGD(
    [
        {'params': model.hidden.parameters(), 'max_norm': 2 }, 
        {'params': model.output.parameters() }
    ],
    lr=0.5,
    momentum=0.5
)

See PyTorch's doc for torch.optim.Optimizer for more. The result of the above is that parameters matrices contained in model.hidden will get clipped, and parameter matrix in model.output won't be. Note that this is something of a hack that relies on torch.optim.Optimizer accepting keyword arguments that don't match what it internally uses.

Here is an example of use that includes a test of correctness.

Rationale

The max-norm constraint is essentially an additional normalization constraint applied after each optimization steps; the optimizer checks for each unit if its incoming weight vector $\mathbf{w}$ has some $p$-norm $\lVert \mathbf{w} \rVert_p$ greater than or equal than some scalar max norm, and normalizes it if it does not. From what I've seen, its use is mostly associated with the use of the dropout technique. This constraint can be used out-of-the-box in Keras (as tf.keras.constraint.MaxNorm), but I couldn't find any out-of-the-box implementation for PyTorch.

To-do in near future

Add other optimizers and support for non-iterable params argument to the optimizer constructor. Run more tests, including with CUDA.

maxnorm's People

Contributors

mehhl avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.