In the sequence to sequence model the decoder is an LSTM language model whose starting state is the encoder's summary of the source sentence. It defines
where is the end-of-sentence token <EOS> and every factor is a softmax over the whole target vocabulary. Training maximises this log-probability of the reference translation, and the same number is used to compare translations. Compute it.
sequence_log_prob(states, tgt_ids, emb, layers, W_out, b_out, eos_id) -> (total, per_token)
states: the encoder's final states, a list of tuples (h, c) (bottom layer first, arrays of shape (H,)). The decoder starts from exactly these.tgt_ids: the reference translation as a list of token ids, without <EOS> (may be empty).emb (V, E), layers (same layout as the encoder: "W" (4H, D_l + H), "b" (4H,)): the decoder's own embedding table and LSTM layers.W_out (V, H), b_out (V,): the output layer. At each step the logits are W_out @ h_top + b_out, where h_top is the top layer's new hidden state.eos_id: id of <EOS>.deep_lstm_step(x, states, layers) runs one step of the stack and returns the new states.Decoding with the reference: the first input is emb[eos_id] (the end-of-sentence token separates the source from the target), and the input at step is emb[y_{t-1}]. Step scores ; the last step scores <EOS>. So there are len(tgt_ids) + 1 factors.
Return total, the sum of the natural-log probabilities (a Python float), and per_token, the list of the len(tgt_ids) + 1 individual log-probabilities in order.