pytorch 中的pack_padded_sequence操作详解

今天终于搞清楚了这个pack操作,链接可见https://zhuanlan.zhihu.com/p/34418001

packed = nn.utils.rnn.pack_padded_sequence(embedded, input_lengths)
 # Forward pass through GRU
 outputs, hidden = self.gru(packed, hidden)
 # Unpack padding
 outputs, _ = nn.utils.rnn.pad_packed_sequence(outputs)

但是还有一个问题,就是GRU如何识别pack操作后的数据:那么这里是有一个多态的判断——即调用gru网络函数,其内部有判断的代码。在pytorch的doc中有源码。如下:

def forward(self, input, hx=None):
        if isinstance(input, PackedSequence):
            return self.forward_packed(input, hx)
        else:
            return self.forward_tensor(input, hx)

接下来的forward_packed()函数和forward_tensor()函数有不同计算方式,这里给出两者对比

def forward_packed(self, input, hx=None):
        # type: (Tuple[Tensor, Tensor, Optional[Tensor], Optional[Tensor]], Optional[Tuple[Tensor, Tensor]]) -> Tuple[Tuple[Tensor, Tensor, Optional[Tensor], Optional[Tensor]], Tuple[Tensor, Tensor]]  # noqa
        input, batch_sizes, sorted_indices, unsorted_indices = input
        max_batch_size = batch_sizes[0]
        max_batch_size = int(max_batch_size)

        output, hidden = self.forward_impl(input, hx, batch_sizes, max_batch_size, sorted_indices)

        output = get_packed_sequence(output, batch_sizes, sorted_indices, unsorted_indices)
        return output, self.permute_hidden(hidden, unsorted_indices)

 

 def forward_tensor(self, input, hx=None):
        # type: (Tensor, Optional[Tuple[Tensor, Tensor]]) -> Tuple[Tensor, Tuple[Tensor, Tensor]]
        batch_sizes = None
        max_batch_size = input.size(0) if self.batch_first else input.size(1)
        sorted_indices = None
        unsorted_indices = None

        output, hidden = self.forward_impl(input, hx, batch_sizes, max_batch_size, sorted_indices)

        return output, self.permute_hidden(hidden, unsorted_indices)

至于之后一层套一层的函数计算方式,有感兴趣的可以按顺序一个一个找出来分析。

你可能感兴趣的:(pytorch,tutorials,text)