Conv1d()
class torch.nn.Conv1d(
in_channels,
out_channels,
kernel_size,
stride=1,
padding=0,
dilation=1,
groups=1,
bias=True)
- in_channels(int) – 輸入信號的通道。即為詞向量的維度。2維RGB圖像卷積中,為3。
- out_channels(int) – 卷積產(chǎn)生的通道。有多少個out_channels,就需要多少個1維卷積(也就是卷積核的數(shù)量)
- kernel_size(int or tuple) - 卷積核的尺寸,卷積核的大小為(k,),第二個維度是由in_channels來決定的,所以實際上卷積大小為kernel_size*in_channels
- stride(int or tuple, optional) - 卷積步長
- padding (int or tuple, optional)- 輸入的每一條邊補充0的層數(shù)
- dilation(int or tuple, `optional``) – 卷積核元素之間的間距
- groups(int, optional) – 從輸入通道到輸出通道的阻塞連接數(shù)
- bias(bool, optional) - 如果bias=True,添加偏置
舉例:實體鏈接(x,y),x,y是兩個實體,當x,y為同一實體,標注為1,否則標注為1。
conv1 = nn.Conv1d(in_channels=200,out_channels=50, kernel_size=2)
input = torch.randn(32,8,200)
# batch_size x entity_len x embedding_size -> batch_size x embedding_size x text_len
input = input.permute(0,2,1)
out = conv1(input)
print(out.size())
這里32為batch_size,8為實體中詞的個數(shù),200為詞向量。50為卷積核的數(shù)量,2為卷積核的尺寸。
輸入一維卷積的時候,需要將32*8*200變換為32*200*8,因為一維卷積是在最后維度上掃的,卷積核大小為200*2,最后out的大小即為:32*50*(8-2+1)=32*50*7,最大池化后的大小為:32*50*1