在Pytorch中创建自定义连接/非链接的层
,如图所示,它是一个带有NN的3层,即输入层,隐藏层和输出层。我想设计NN(在Pytorch,Just Arch),其中隐藏层的输入完全连接。但是,从隐藏层到输出,隐藏层的前两个神经元应连接到输出层的第一个神经元,第二两个应连接到输出层中的第二个神经元,依此类推。应该如何设计?
from torch import nn
layer1 = nn.Linear(input_size, hidden_size)
layer2 = ??????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AS @jan 说 /a>,您可以超载 > 并提供一个点的面膜,以掩盖您要避免的相互作用。请记住,完全连接的层仅是具有可选添加偏置的矩阵乘法。
查看其 source code 我们可以做:
具有
f
定义为torch。 nn.功能
考虑您对第二层的约束:
似乎您正在寻找这种模式:
可以使用
torch.block_diag
:可以将网络定义为:
有了这个,您 它在自定义层中:
然后像这样定义:
As @Jan said here, you can overload
nn.Linear
and provide a point-wise mask to mask the interaction you want to avoid having. Remember that a fully connected layer is merely a matrix multiplication with an optional additive bias.Looking at its source code, we can do:
Having
F
defined astorch.nn.functional
Considering the constraint you have given to the second layer:
It seems you are looking for this pattern:
Which can be obtained using
torch.block_diag
:Having this, you can define your network as:
If you feel like it, you can even implement it inside the custom layer:
And defining it like so:
而不是直接使用
nn.lin.linear
,而是创建权重张量wigith
和一个掩码mask
掩盖了您不打算使用的权重。然后,您使用torch.nn.functional.linear(输入,重量 * mask)
( https://pytorch.org/docs/stable/generated/generated/torch.nn.functional.linear.linear.html )向第二层转发。请注意,这是在您的torch.nn.module
'sforward
函数中实现的。重量需要注册为nn.module
的参数,以便通过nn.module.parameters()
识别。请参阅。Instead of using
nn.Linear
directly, create a weights tensorweight
and a mask tensormask
that masks those weights that you do not intend to use. Then you usetorch.nn.functional.linear(input, weight * mask)
(https://pytorch.org/docs/stable/generated/torch.nn.functional.linear.html) to forward the second layer. Note that this is implemented in yourtorch.nn.Module
'sforward
function. The weight needs to be registered as a parameter to yournn.Module
so that it's recognized bynn.Module.parameters()
. See https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.register_parameter.伊万的一般方法(掩盖了完全连接的层)可能会像我的评论一样进行修改,但它增加了很多无用的计算!
最好在此处写一个自定义图层,并具有形状
的权重矩阵(2,hidden_size // 2)
。然后重新设计从(hidden_size)
到(sideen_size // 2,2)
的输入从隐藏层的输出到层的输入。这样的东西(未经测试):
Ivan's general approach (masking the fully connected layer) may work with modifications as in my comment, but it adds a lot of useless computation!
It's probably best to write a custom layer here, with a weight matrix of shape
(2, hidden_size//2)
. Then reshape the input to the layer from output of the hidden layer from(hidden_size)
to(hidden_size//2, 2)
and do the matrix multiply.Something like this (untested):