进行预测时,每一行的输出相同。因此我有错误的预测

时间:2020-02-13 17:40:15

标签: python pandas dataframe lstm

我已经使用lstm方法实施了情绪分析,已经在代码的第一部分训练了模型,现在正在做预测部分。当我运行系统时,所有行的结果都相同,这是不正确的。有人可以告诉我我的错误以及我需要做些什么才能做出正确的预测吗?拜托。

我已经实现了:


#Re-create the model to get attention vectors as well as label predictions

model_with_attentions = keras.Model(inputs=model.input,
                                    output=[model.output,
                                              model.get_layer('attention_vec').output])


import json

with open(r"C:\Users\User\Desktop\Coding\parsehubjsonfileeg\22 results.json", encoding="utf8") as f:
    data = json.load(f)
df = pd.DataFrame(data['selection1'][0]['CommentID'])

a=df['comment']
print(a)
dtt = pd.DataFrame(a, columns=['comment'])
print(dtt)
import nltk
result=[]
for sentence in dtt["comment"]:

    # Encode samples
    tokenize = dtt.apply(lambda row: nltk.word_tokenize(row['comment']), axis=1)
    #encoded_samples =[[word2id[word] for sentence in dtt["comment"]]]
    encoded_samples = [[word2id[word] for sentence in tokenize]]

    # Padding
    encoded_samples = keras.preprocessing.sequence.pad_sequences(encoded_samples, maxlen=max_words)

# Make predictions
    label_probs, attentions = model_with_attentions.predict(encoded_samples)
    label_probs = {id2label[_id]: prob for (label, _id), prob in zip(label2id.items(), label_probs[0])}
    result.append(label_probs)

dtt["Result"] = result
print(dtt)

我得到了输出:

                                               comment                                             Result
0                                enjoy a lovely moment  {'joy': 0.2003046, 'happy': 0.20040178, 'disap...
1    I was there for my honeymoon. The hotel was si...  {'joy': 0.2003046, 'happy': 0.20040178, 'disap...
2    Had an amazing stay for 2 nights.\nThe cleanli...  {'joy': 0.2003046, 'happy': 0.20040178, 'disap...
3                       Had a good time. Food is good.  {'joy': 0.2003046, 'happy': 0.20040178, 'disap...
4    A highly recommendable hotel. Value for money ...  {'joy': 0.2003046, 'happy': 0.20040178, 'disap...
..                                                 ...                                                ...
131  Wonderful experience, a quite different experi...  {'joy': 0.2003046, 'happy': 0.20040178, 'disap...
132                               Was a paradise stay.  {'joy': 0.2003046, 'happy': 0.20040178, 'disap...
133  It was really a place to be for relaxing provi...  {'joy': 0.2003046, 'happy': 0.20040178, 'disap...
134  It was just perfect with an excellent service!...  {'joy': 0.2003046, 'happy': 0.20040178, 'disap...
135                                 It's was excellent  {'joy': 0.2003046, 'happy': 0.20040178, 'disap...

[136 rows x 2 columns]

Process finished with exit code 0

有人可以帮助我吗?请

0 个答案:

没有答案