MCPcopy Create free account
hub / github.com/pytorch/pytorch / LSTMWithAttention

Function LSTMWithAttention

caffe2/python/rnn_cell.py:1840–1946  ·  view source on GitHub ↗

Adds a LSTM with attention mechanism to a model. The implementation is based on https://arxiv.org/abs/1409.0473, with a small difference in the order how we compute new attention context and new hidden state, similarly to https://arxiv.org/abs/1508.04025. The model uses en

(
    model,
    decoder_inputs,
    decoder_input_lengths,
    initial_decoder_hidden_state,
    initial_decoder_cell_state,
    initial_attention_weighted_encoder_context,
    encoder_output_dim,
    encoder_outputs,
    encoder_lengths,
    decoder_input_dim,
    decoder_state_dim,
    scope,
    attention_type=AttentionType.Regular,
    outputs_with_grads=(0, 4),
    weighted_encoder_outputs=None,
    lstm_memory_optimization=False,
    attention_memory_optimization=False,
    forget_bias=0.0,
    forward_only=False,
)

Source from the content-addressed store, hash-verified

1838
1839
1840def LSTMWithAttention(
1841 model,
1842 decoder_inputs,
1843 decoder_input_lengths,
1844 initial_decoder_hidden_state,
1845 initial_decoder_cell_state,
1846 initial_attention_weighted_encoder_context,
1847 encoder_output_dim,
1848 encoder_outputs,
1849 encoder_lengths,
1850 decoder_input_dim,
1851 decoder_state_dim,
1852 scope,
1853 attention_type=AttentionType.Regular,
1854 outputs_with_grads=(0, 4),
1855 weighted_encoder_outputs=None,
1856 lstm_memory_optimization=False,
1857 attention_memory_optimization=False,
1858 forget_bias=0.0,
1859 forward_only=False,
1860):
1861 '''
1862 Adds a LSTM with attention mechanism to a model.
1863
1864 The implementation is based on https://arxiv.org/abs/1409.0473, with
1865 a small difference in the order
1866 how we compute new attention context and new hidden state, similarly to
1867 https://arxiv.org/abs/1508.04025.
1868
1869 The model uses encoder-decoder naming conventions,
1870 where the decoder is the sequence the op is iterating over,
1871 while computing the attention context over the encoder.
1872
1873 model: ModelHelper object new operators would be added to
1874
1875 decoder_inputs: the input sequence in a format T x N x D
1876 where T is sequence size, N - batch size and D - input dimension
1877
1878 decoder_input_lengths: blob containing sequence lengths
1879 which would be passed to LSTMUnit operator
1880
1881 initial_decoder_hidden_state: initial hidden state of LSTM
1882
1883 initial_decoder_cell_state: initial cell state of LSTM
1884
1885 initial_attention_weighted_encoder_context: initial attention context
1886
1887 encoder_output_dim: dimension of encoder outputs
1888
1889 encoder_outputs: the sequence, on which we compute the attention context
1890 at every iteration
1891
1892 encoder_lengths: a tensor with lengths of each encoder sequence in batch
1893 (may be None, meaning all encoder sequences are of same length)
1894
1895 decoder_input_dim: input dimension (last dimension on decoder_inputs)
1896
1897 decoder_state_dim: size of hidden states of LSTM

Callers

nothing calls this directly

Calls 4

appendMethod · 0.45
apply_over_sequenceMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…