首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么texts_to_sequences()输出为空数组?

为什么texts_to_sequences()输出为空数组?
EN

Stack Overflow用户
提问于 2020-03-06 02:05:17
回答 1查看 1.1K关注 0票数 1

我试着用预先训练过的模型来预测。

texts_to_sequences(twt)返回空数组。因此,预测总是否定的。所有的投入。

代码语言:javascript
复制
from keras.preprocessing.sequence import pad_sequences
twt=['happy']
#vectorizing the tweet by the pre-fitted tokenizer instance
twt = tokenizer.texts_to_sequences(twt)
print(twt)
#padding the tweet to have exactly the same shape as `embedding_2` input
twt = pad_sequences(twt, maxlen=50, dtype='int32', value=0)
print(twt)
sentiment = model.predict(twt,batch_size=1,verbose = 2)[0]
print(sentiment)
if(np.argmax(sentiment) == 0):
    print("negative")
elif (np.argmax(sentiment) == 1):
    print("positive")

产出:

代码语言:javascript
复制
[[]]
[0.89889544 0.10110457]
negative

怎么解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2021-01-22 17:35:55

我遇到了同样的问题,您需要做的就是在这两个函数tokenizer.fit_on_textstokenizer.texts_to_sequences.中传递列表。

示例: tokenizer.fit_on_texts(test_word)

代码语言:javascript
复制
model = ks.models.load_model('trained')
tokenizer = Tokenizer(num_words=5000)
test_word ="This is soo cool"
tokenizer.fit_on_texts([test_word])
tw = tokenizer.texts_to_sequences([test_word])
print('tw: ', tw)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60556576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档