正如您提到的discussion 中指出的那样:
同时,#1935 将使线性层不需要 TimeDistributed/Bottle。
对于 TDD 层,它会将线性层直接应用于具有时间片的输入。
In [1]: import torch
In [2]: m = torch.nn.Linear(20, 30)
In [3]: input = torch.randn(128, 5, 20)
In [4]: output = m(input)
In [5]: print(output.size())
torch.Size([128, 5, 30])
以下是计算结果的简短说明
In [1]: import torch
In [2]: m = torch.nn.Linear(2, 3, bias=False)
...:
...: for name, param in m.named_parameters():
...: print(name)
...: print(param)
...:
weight
Parameter containing:
tensor([[-0.3713, -0.1113],
[ 0.2938, 0.4709],
[ 0.2791, 0.5355]], requires_grad=True)
In [3]: input = torch.stack([torch.ones(3, 2), 2 * torch.ones(3, 2)], dim=0)
...: print(input)
tensor([[[1., 1.],
[1., 1.],
[1., 1.]],
[[2., 2.],
[2., 2.],
[2., 2.]]])
In [4]: m(input)
Out[4]:
tensor([[[-0.4826, 0.7647, 0.8145],
[-0.4826, 0.7647, 0.8145],
[-0.4826, 0.7647, 0.8145]],
[[-0.9652, 1.5294, 1.6291],
[-0.9652, 1.5294, 1.6291],
[-0.9652, 1.5294, 1.6291]]], grad_fn=<UnsafeViewBackward>)
更多nn.Linear的操作细节可以看torch.matmul。请注意,您可能需要添加另一个非线性函数,如 torch.tanh() 以获得与 Keras 中的 Dense() 完全相同的层,其中它们支持关键字参数 activation='tanh' 等非线性。
对于时间分布,例如 CNN 层,PyTorch forum 中的 sn-p 可能有用。