def decode_captions(captions, idx_to_word):
    """
    Decode output captions into worded captions.
    Arguments:
        captions: output captions to be decoded
        idx_to_word: dictionary of word-index vocabulary table
    Outputs:
        decoded: decoded worded captions
    """
    singleton = False
    if captions.ndim == 1:
        singleton = True
        captions = captions[None]
    decoded = []
    N, T = captions.shape
    for i in range(N):
        words = []
        for t in range(T):
            word = idx_to_word[captions[i, t]]
            if word != '<NULL>':
                words.append(word)
            if word == '<END>':
                break
        decoded.append(' '.join(words))
    if singleton:
        decoded = decoded[0]
    return decoded