给定带有前向前钩的火炬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>)])
我们如何传递前向钩子需要但默认forward()
函数不接受的参数(*args
或**kwargs
?
如果不修改/覆盖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'
火炬脚本不兼容(截至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))
你不能以更简洁的方式做到这一点,但限制是 base 的类forward
方法,而不是钩子本身(tbh 我不希望它更简洁,因为它会变得不可读 IMO(。
火炬脚本兼容
如果你想使用火炬脚本(在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])))
由于根据定义,前向预钩子仅使用张量调用,因此关键字参数在这里没有多大意义。更有意义的是使用实例属性,例如:
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