Coder Social home page Coder Social logo

liujingyao203081 / pytorchpartiallayerfreezing Goto Github PK

View Code? Open in Web Editor NEW

This project forked from galidor/pytorchpartiallayerfreezing

0.0 0.0 0.0 8 KB

A simple code snippet for freezing only part of the layer

License: MIT License

Python 100.00%

pytorchpartiallayerfreezing's Introduction

PyTorch Partial Layer Freezing

The motivation for this repo is to allow PyTorch users to freeze only part of the layers in PyTorch. It doesn't require any externat packages other than PyTorch itself.

Usage

  1. Clone this repo.
  2. Copy partial_freezing.py to folder, where you intend to run it.
  3. Import partial_freezing into your .py file:
    import partial_freezing
  4. For a given layer (assume this is a Conv2d layer) do the following:
    partial_freezing.freeze_conv2d_params(layer, indices)
    That will register a backward hook for a given parameters within the layer, which will zero the gradients at specified indices. In this case, indices is a list of integers, specifying which filters you intend to freeze.
  5. If you plan to re-use the function (change the indices of frozen layers), make sure, you save the handles to the backward hooks returned by freeze_conv2d_params(), and pass them as arguments, when re-using the function in your code at the same layer. It can be done as follows:
    weight_hook_handle, bias_hook_handle = partial_freezing.freeze_conv2d_params(layer, indices)
    (...) # your code
    new_weight_hook_handle, new_bias_hook_handle = partial_freezing.freeze_conv2d_params(layer, indices, weight_hook_handle=weight_hook_handle, bias_hook_handle=bias_hook_handle)
    This will ensure that your current hooks will be removed and the new will be added properly.

Some more use cases can be found in test.py.

Limitations

  • Our code freezes entire filters of convolutional layers, rather than specific weighs. We kept it this way to simplify the usage. If you want us to extend the functionality of our code, feel free to write to us, and we will be happy to do so.
  • Since the mechanism for updating weights in case of using weight decay is a bit different, the weights may still be changing if weight_decay > 0 in your optimzier settings.

pytorchpartiallayerfreezing's People

Contributors

galidor 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.