How to convert predicted sequence back to text in keras?

前端 未结 5 625
花落未央
花落未央 2021-01-01 13:39

I have a sequence to sequence learning model which works fine and able to predict some outputs. The problem is I have no idea how to convert the output back to text sequence

5条回答
  •  再見小時候
    2021-01-01 14:28

    You can make the dictionary that map index back to character.

    index_word = {v: k for k, v in tk.word_index.items()} # map back
    seqs = tk.texts_to_sequences(txt1)
    words = []
    for seq in seqs:
        if len(seq):
            words.append(index_word.get(seq[0]))
        else:
            words.append(' ')
    print(''.join(words)) # output
    
    >>> 'what makes this problem difficult is that the sequences can vary in length  
    >>> be comprised of a very large vocabulary of input symbols and may require the model  
    >>> to learn the long term context or dependencies between symbols in the input sequence '
    

    However, in the question, you're trying to use sequence of characters to predict output of 10 classes which is not the sequence to sequence model. In this case, you cannot just turn prediction (or pred.argmax(axis=1)) back to sequence of characters.

提交回复
热议问题