如何将关键字参数传递给前转挂钩使用的前转?

时间:2019-08-29 05:56:30

标签: python callback pytorch tensor

给火炬的nn.Module带有一个前向挂钩,例如

import torch
import torch.nn as nn

class NeoEmbeddings(nn.Embedding):
    def __init__(self, num_embeddings:int, embedding_dim:int, padding_idx=-1):
        super().__init__(num_embeddings, embedding_dim, padding_idx)
        self.register_forward_pre_hook(self.neo_genesis)

    @staticmethod
    def neo_genesis(self, input, higgs_bosson=0):
        if higgs_bosson:
            input = input + higgs_bosson
        return input

在进入实际的forward()函数之前,可以让输入张量经过一些操作,例如

>>> x = NeoEmbeddings(10, 5, 1)
>>> x.forward(torch.tensor([0,2,5,8]))
tensor([[-1.6449,  0.5832, -0.0165, -1.3329,  0.6878],
        [-0.3262,  0.5844,  0.6917,  0.1268,  2.1363],
        [ 1.0772,  0.1748, -0.7131,  0.7405,  1.5733],
        [ 0.7651,  0.4619,  0.4388, -0.2752, -0.3018]],
       grad_fn=<EmbeddingBackward>)

>>> print(x._forward_pre_hooks)
OrderedDict([(25, <function NeoEmbeddings.neo_genesis at 0x1208d10d0>)])

我们如何传递预转发挂钩需要但默认*args函数不接受的参数(**kwargsforward())? < / p>

没有修改/覆盖forward()函数,这是不可能的:

>>> x = NeoEmbeddings(10, 5, 1)
>>> x.forward(torch.tensor([0,2,5,8]), higgs_bosson=2)

----------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-102-8705a40a3cc2> in <module>
      1 x = NeoEmbeddings(10, 5, 1)
----> 2 x.forward(torch.tensor([0,2,5,8]), higgs_bosson=2)

TypeError: forward() got an unexpected keyword argument 'higgs_bosson'

2 个答案:

答案 0 :(得分:3)

教规不兼容(自1.2.0起)

首先,您的示例torch.nn.Module有一些小错误(可能是偶然的错误)。

第二,您可以传递任何内容进行转发,register_forward_pre_hook只会得到将传递给您的torch.nn.Module(无论是图层还是模型或其他任何东西)的参数。 。您确实无法完成,而无需修改forward调用,但是为什么要避免这种情况呢?您可以简单地将参数转发给基本函数,如下所示:

import torch


class NeoEmbeddings(torch.nn.Embedding):
    def __init__(self, num_embeddings: int, embedding_dim: int, padding_idx=-1):
        super().__init__(num_embeddings, embedding_dim, padding_idx)
        self.register_forward_pre_hook(NeoEmbeddings.neo_genesis)

    # First argument should be named something like module, as that's what 
    # you are registering this hook to
    @staticmethod
    def neo_genesis(module, inputs):  # No need for self as first argument
        net_input, higgs_bosson = inputs  # Simply unpack tuple here
        return net_input

    def forward(self, inputs, higgs_bosson):
        # Do whatever you want here with both arguments, you can ignore 
        # higgs_bosson if it's only needed in the hook as done here
        return super().forward(inputs)


if __name__ == "__main__":
    x = NeoEmbeddings(10, 5, 1)
    # You should call () instead of forward so the hooks register appropriately
    print(x(torch.tensor([0, 2, 5, 8]), 1))

您不能以更简洁的方式进行操作,但是限制是基类的forward方法,而不是钩子本身(而且我不希望它更加简洁,因为它将变得难以理解IMO )。

与脚本兼容

如果您想使用torchscript(在1.2.0上进行了测试),则可以使用合成代替继承。您只需更改两行即可,您的代码可能看起来像这样:

import torch

# Inherit from Module and register embedding as submodule
class NeoEmbeddings(torch.nn.Module):
    def __init__(self, num_embeddings: int, embedding_dim: int, padding_idx=-1):
        super().__init__()
        # Just use it as a container inside your own class
        self._embedding = torch.nn.Embedding(num_embeddings, embedding_dim, padding_idx)
        self.register_forward_pre_hook(NeoEmbeddings.neo_genesis)

    @staticmethod
    def neo_genesis(module, inputs):
        net_input, higgs_bosson = inputs
        return net_input

    def forward(self, inputs: torch.Tensor, higgs_bosson: torch.Tensor):
        return self._embedding(inputs)


if __name__ == "__main__":
    x = torch.jit.script(NeoEmbeddings(10, 5, 1))
    # All arguments must be tensors in torchscript
    print(x(torch.tensor([0, 2, 5, 8]), torch.tensor([1])))

答案 1 :(得分:2)

由于通过定义仅使用张量调用forward pre-hook,因此关键字参数在这里没有多大意义。更有意义的是使用实例属性,例如:

def neo_genesis(self, input):
    if self.higgs_bosson:
        input = input + self.higgs_bosson
    return input

然后,您可以适当地切换该属性。您也可以为此使用上下文管理器:

from contextlib import contextmanager

@contextmanager
def HiggsBoson(module):
    module.higgs_boson = 1
    yield
    module.higgs_boson = 0

with HiggsBoson(x):
    x.forward(...)

如果您已经具有该功能,并且确实需要更改该参数,则仍然可以替换该功能的__defaults__属性:

x.neo_genesis.__defaults__ = (1,)  # this corresponds to `higgs_boson` parameter
x.forward(...)
x.neo_genesis.__defaults__ = (0,)  # reset to default