(self, tgt, memory_bank, state, memory_lengths=None, step=None)
return outputs, state, attns
See :obj:`onmt.modules.RNNDecoderBase.forward()`
See :obj:`onmt.modules.RNNDecoderBase.forward()`
See : obj : onmt . modules . RNNDecoderBase . forward ()
def forward(self, tgt, memory_bank, state, memory_lengths=None, step=None):
        """ See :obj:`onmt.modules.RNNDecoderBase.forward()`"""
        # NOTE: memory_lengths is only here for compatibility reasons
        #       with onmt.modules.RNNDecoderBase.forward()
        # CHECKS
        assert isinstance(state, CNNDecoderState)
        _, tgt_batch, _ = tgt.size()
        _, contxt_batch, _ = memory_bank.size()
        aeq(tgt_batch, contxt_batch)
        # END CHECKS

        if state.previous_input is not None:
            tgt = torch.cat([state.previous_input, tgt], 0)

        # Initialize return variables.
        outputs = []
        attns = {"std": []}
        assert not self._copy, "Copy mechanism not yet tested in conv2conv"
        if self._copy:
            attns["copy"] = []

        emb = self.embeddings(tgt)
        assert emb.dim() == 3  # len x batch x embedding_dim

        tgt_emb = emb.transpose(0, 1).contiguous()
        # The output of CNNEncoder.
        src_memory_bank_t = memory_bank.transpose(0, 1).contiguous()
        # The combination of output of CNNEncoder and source embeddings.
        src_memory_bank_c = state.init_src.transpose(0, 1).contiguous()

        # Run the forward pass of the CNNDecoder.
        emb_reshape = tgt_emb.contiguous().view(
            tgt_emb.size(0) * tgt_emb.size(1), -1)
        linear_out = self.linear(emb_reshape)
        x = linear_out.view(tgt_emb.size(0), tgt_emb.size(1), -1)
        x = shape_transform(x)

        pad = torch.zeros(x.size(0), x.size(1),
                          self.cnn_kernel_width - 1, 1)

        pad = pad.type_as(x)
        base_target_emb = x

        for conv, attention in zip(self.conv_layers, self.attn_layers):
            new_target_input = torch.cat([pad, x], 2)
            out = conv(new_target_input)
            c, attn = attention(base_target_emb, out,
                                src_memory_bank_t, src_memory_bank_c)
            x = (x + (c + out) * SCALE_WEIGHT) * SCALE_WEIGHT
        output = x.squeeze(3).transpose(1, 2)

        # Process the result and update the attentions.
        outputs = output.transpose(0, 1).contiguous()
        if state.previous_input is not None:
            outputs = outputs[state.previous_input.size(0):]
            attn = attn[:, state.previous_input.size(0):].squeeze()
            attn = torch.stack([attn])
        attns["std"] = attn
        if self._copy:
            attns["copy"] = attn

        # Update the state.
        state.update_state(tgt)

        return outputs, state, attns
def forward ( self , tgt , memory_bank , state , memory_lengths = None , step = None ) : # NOTE: memory_lengths is only here for compatibility reasons #       with onmt.modules.RNNDecoderBase.forward() # CHECKS assert isinstance ( state , CNNDecoderState ) _ , tgt_batch , _ = tgt . size ( ) _ , contxt_batch , _ = memory_bank . size ( ) aeq ( tgt_batch , contxt_batch ) # END CHECKS if state . previous_input is not None : tgt = torch . cat ( [ state . previous_input , tgt ] , 0 ) # Initialize return variables. outputs = [ ] attns = { "std" : [ ] } assert not self . _copy , "Copy mechanism not yet tested in conv2conv" if self . _copy : attns [ "copy" ] = [ ] emb = self . embeddings ( tgt ) assert emb . dim ( ) == 3 # len x batch x embedding_dim tgt_emb = emb . transpose ( 0 , 1 ) . contiguous ( ) # The output of CNNEncoder. src_memory_bank_t = memory_bank . transpose ( 0 , 1 ) . contiguous ( ) # The combination of output of CNNEncoder and source embeddings. src_memory_bank_c = state . init_src . transpose ( 0 , 1 ) . contiguous ( ) # Run the forward pass of the CNNDecoder. emb_reshape = tgt_emb . contiguous ( ) . view ( tgt_emb . size ( 0 ) * tgt_emb . size ( 1 ) , - 1 ) linear_out = self . linear ( emb_reshape ) x = linear_out . view ( tgt_emb . size ( 0 ) , tgt_emb . size ( 1 ) , - 1 ) x = shape_transform ( x ) pad = torch . zeros ( x . size ( 0 ) , x . size ( 1 ) , self . cnn_kernel_width - 1 , 1 ) pad = pad . type_as ( x ) base_target_emb = x for conv , attention in zip ( self . conv_layers , self . attn_layers ) : new_target_input = torch . cat ( [ pad , x ] , 2 ) out = conv ( new_target_input ) c , attn = attention ( base_target_emb , out , src_memory_bank_t , src_memory_bank_c ) x = ( x + ( c + out ) * SCALE_WEIGHT ) * SCALE_WEIGHT output = x . squeeze ( 3 ) . transpose ( 1 , 2 ) # Process the result and update the attentions. outputs = output . transpose ( 0 , 1 ) . contiguous ( ) if state . previous_input is not None : outputs = outputs [ state . previous_input . size ( 0 ) : ] attn = attn [ : , state . previous_input . size ( 0 ) : ] . squeeze ( ) attn = torch . stack ( [ attn ] ) attns [ "std" ] = attn if self . _copy : attns [ "copy" ] = attn # Update the state. state . update_state ( tgt ) return outputs , state , attns