gpt4 book ai didi

python - 如何将预测序列转换回keras中的文本?

转载 作者:太空狗 更新时间:2023-10-29 17:24:44 26 4
gpt4 key购买 nike

我有一个序列到序列学习模型,它运行良好并且能够预测一些输出。问题是我不知道如何将输出转换回文本序列。

这是我的代码。

from keras.preprocessing.text import Tokenizer,base_filter
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Dense

txt1="""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."""

#txt1 is used for fitting
tk = Tokenizer(nb_words=2000, filters=base_filter(), lower=True, split=" ")
tk.fit_on_texts(txt1)

#convert text to sequence
t= tk.texts_to_sequences(txt1)

#padding to feed the sequence to keras model
t=pad_sequences(t, maxlen=10)

model = Sequential()
model.add(Dense(10,input_dim=10))
model.add(Dense(10,activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy'])

#predicting new sequcenc
pred=model.predict(t)

#Convert predicted sequence to text
pred=??

最佳答案

您可以直接使用反tokenizer.sequences_to_texts 函数。

    text = tokenizer.sequences_to_texts(<list_of_integer_equivalent_encodings>)

我已经测试了上面的内容,它按预期工作。

PS.: 要格外小心,使参数成为整数编码的列表,而不是 One Hot。

关于python - 如何将预测序列转换回keras中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41971587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com