How to convert predicted sequence back to text in keras?

前端 未结 5 634
花落未央
花落未央 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:30

    I had to resolve the same problem, so here is how I ended up doing it (inspired by @Ben Usemans reversed dictionary).

    # Importing library
    from keras.preprocessing.text import Tokenizer
    
    # My texts
    texts = ['These are two crazy sentences', 'that I want to convert back and forth']
    
    # Creating a tokenizer
    tokenizer = Tokenizer(lower=True)
    
    # Building word indices
    tokenizer.fit_on_texts(texts)
    
    # Tokenizing sentences
    sentences = tokenizer.texts_to_sequences(texts)
    
    >sentences
    >[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11, 12, 13]]
    
    # Creating a reverse dictionary
    reverse_word_map = dict(map(reversed, tokenizer.word_index.items()))
    
    # Function takes a tokenized sentence and returns the words
    def sequence_to_text(list_of_indices):
        # Looking up words in dictionary
        words = [reverse_word_map.get(letter) for letter in list_of_indices]
        return(words)
    
    # Creating texts 
    my_texts = list(map(sequence_to_text, sentences))
    
    >my_texts
    >[['these', 'are', 'two', 'crazy', 'sentences'], ['that', 'i', 'want', 'to', 'convert', 'back', 'and', 'forth']]
    

提交回复
热议问题