首页 > 其他 > 详细

Pytorch实现对卷积的可插拔reparameterization

时间:2020-02-08 17:37:23      阅读:92      评论:0      收藏:0      [点我收藏+]

需要实现对卷积层的重参数化reparameterization

但是代码里卷积前weight并没有hook,很难在原本的卷积类上用pure oo的方式实现

目前的解决方案是继承原本的卷积,挂载一个weight module替代原本的weight parameter。需要hack一下getattr

大致代码:

class ReparamLayer(nn.module):
    def __init__(self, weight:nn.Parameter):
        self.weight = weight

    def forward(self):
        reparam = self.weight
        # do something
        # reparam = fn(reparam)
        return reparam
    
    @property
    def data(self):
        return self.forward()  # hack

class ReparamConv2d(nn.Conv2d):
    def __init__(self, *args, **kwargs):
        self._inited = False
        super().__init__
        w = self.weight
        self._inited = True
        self._weight = ReparamLayer(w)  # reparam weights here
        del self._parameters['weight']
    
    def __getattr__(self, item):
        if self._inited and item == 'weight':
            return self._weight  # hack
        else:
            return super().__getattr__(item)

Pytorch实现对卷积的可插拔reparameterization

原文:https://www.cnblogs.com/MisTariano/p/12284037.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!