def forward_conv_batch(self, x):
"""
:param x: (batch, length, dim)
:return: (batch, length - kernel + 2*padding_size + 1, hidden_dim)
"""
# T.nn.conv2d (batch size, input channels, input rows, input columns)
# dl4nlp (batch size, 1, length, in_dim)
x = x.dimshuffle([0, 'x', 1, 2])
# T.nn.conv2d (output channels, input channels, filter rows, filter columns)
# dl4nlp (hidden_dim, 1, kernel_size, in_dim)
filter_w = self.W.dimshuffle([1, 'x', 0, 2])
# T.nn.conv2d (batch size, output channels, output rows, output columns)
# dl4nlp (batch size, hidden_dim, length+kernel-1, 1)
conv_result = T.nnet.conv2d(x, filter_w,
border_mode='valid',)
# from theano.printing import Print
# conv_result = Print()(conv_result)
# (batch size, hidden_dim, length - kernel + 2*padding_size + 1, 1)
# -> (batch, length - kernel + 2*padding_size + 1, hidden_dim)
conv_result = T.transpose(conv_result[:, :, :, 0], (0, 2, 1))
return conv_result
评论列表
文章目录