代码之家  ›  专栏  ›  技术社区  ›  momo

在Keras中读取LSTM文本时,如何修复这个'UnicodeEncodeError'/'TypeError'

  •  1
  • momo  · 技术社区  · 6 年前

    我在用密码 here by the keras team 用于使用LSTMs生成文本,但我使用自己的文本数据。我在GPU上运行时遇到这个错误。

    我想我们需要使用“utf-8”,但在读取文本文件时已经指定了它。

    path = 'path/to/mytxt.txt'
    with io.open(path, encoding='utf-8') as f:
        text = f.read().lower()
    

    def on_epoch_end(epoch, _):
        # Function invoked at end of each epoch. Prints generated text.
    
        start_index = random.randint(0, len(text) - maxlen - 1)
        for diversity in [0.2, 0.5, 1.0, 1.2]:
    
            generated = ''
            sentence = text[start_index: start_index + maxlen]
            generated += sentence
            print('----- Generating with seed: "' + sentence + '"')
    

    文件“txt_py代,第77行,在

    打印('----正在生成,带种子:“'+句子+'”')

    尝试 print('----- Generating with seed: "' + sentence.encoded('utf-8') + '"') 现在错误变为

    TypeError:无法将“bytes”对象隐式转换为str

    0 回复  |  直到 5 年前