nn.Conv1d代码
out_channels = 卷积核个数 = 输出数据y.shpae[-2]。
in_channels = 输入数据x.shape[1]。
而输出数据y.shape[-1]可以根据kernel_size和步长来算。
import torch
from torch import nn
# in_channels = embed_dim
conv1 = nn.Conv1d(in_channels=4, out_channels = 2, kernel_size = 2)
input = torch.ones(3,6,4)
# batch_size x text_len x embedding_size -> batch_size x embedding_size x text_len
# batch_size*ts_d x seg_num x embed_dim -> batch_size*ts_d x embed_dim x seg_num
input = input.permute(0, 2, 1)
print(input)
print("---------------")
out = conv1(input)
print(out)
print(out.shape)
运行结果
tensor([[[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]],
[[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]]])
---------------
tensor([[[-0.8792, -0.8792, -0.8792, -0.8792, -0.8792],
[ 0.7349, 0.7349, 0.7349, 0.7349, 0.7349]],
[[-0.8792, -0.8792, -0.8792, -0.8792, -0.8792],
[ 0.7349, 0.7349, 0.7349, 0.7349, 0.7349]],
[[-0.8792, -0.8792, -0.8792, -0.8792, -0.8792],
[ 0.7349, 0.7349, 0.7349, 0.7349, 0.7349]]],
grad_fn=<ConvolutionBackward0>)
torch.Size([3, 2, 5])

浙公网安备 33010602011771号