【问题标题】:How can i extract the prediction from the output LSTM如何从输出 LSTM 中提取预测
【发布时间】:2020-03-06 00:09:18
【问题描述】:

我已经建立了一个使用 LSTM 来预测情绪的模型。 该模型以超过 80% 的准确率完成。 但是当我试图预测一个外部值时。 model.predict() 不预测。它只是提供一个空数组的情绪。

模型如下。

import numpy as np
import pandas as pd

import warnings
warnings.filterwarnings("ignore")
from keras.utils import to_categorical
from keras.preprocessing.text import Tokenizer
from keras.preprocessing import sequence
import keras.backend as K
from keras.models import Sequential
from keras.layers import Dense,Embedding,LSTM
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report


# Load Data
df = pd.read_csv('/content/drive/My Drive/Colab Notebooks/analysis.csv')
test = pd.read_csv('/content/drive/My Drive/Colab Notebooks/analysis.csv')
pd.set_option('display.max_colwidth', -1)

seed = 101 
np.random.seed(seed)

X = df['ride_review']
temp = test['ride_review']
y = to_categorical(df['sentiment'])
num_classes = df['sentiment'].nunique()

# Spilt Train Test sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.2,stratify=y,random_state=seed)
#print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)

# Tokenize Text
max_features = 15000
tokenizer = Tokenizer(num_words=max_features)
tokenizer.fit_on_texts(list(X_train))
X_train = tokenizer.texts_to_sequences(X_train)
X_test = tokenizer.texts_to_sequences(X_test)
temp = tokenizer.texts_to_sequences(temp)

max_words = 50 
X_train = sequence.pad_sequences(X_train, maxlen=max_words)
X_test = sequence.pad_sequences(X_test, maxlen=max_words)
temp = sequence.pad_sequences(temp, maxlen=max_words)
#print(X_train.shape,X_test.shape)


batch_size = 128
epochs = 7
def get_model(max_features, embed_dim, embedding_matrix):
    np.random.seed(seed)
    K.clear_session()
    model = Sequential()
    model.add(Embedding(max_features, embed_dim, input_length=X_train.shape[1],
                       weights=[embedding_matrix]))#,trainable=False
    model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
    model.add(Dense(num_classes, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    print(model.summary())
    return model

def get_coefs(word, *arr):
    return word, np.asarray(arr, dtype='float32')

def get_embed_mat(EMBEDDING_FILE, max_features=20000):
    # word vectors
    embeddings_index = dict(get_coefs(*o.rstrip().rsplit(' ')) for o in open(EMBEDDING_FILE, encoding='utf8'))
    print('Found %s word vectors.' % len(embeddings_index))

    # embedding matrix
    word_index = tokenizer.word_index
    num_words = min(max_features, len(word_index) + 1)
    all_embs = np.stack(embeddings_index.values()) #for random init
    embedding_matrix = np.random.normal(all_embs.mean(), all_embs.std(), 
                                        (num_words, embed_dim))
    for word, i in word_index.items():
        if i >= max_features:
            continue
        embedding_vector = embeddings_index.get(word)
        if embedding_vector is not None:
            embedding_matrix[i] = embedding_vector
    max_features = embedding_matrix.shape[0]

    return max_features, embedding_matrix

# embedding matrix
EMBEDDING_FILE = '/content/drive/My Drive/Colab Notebooks/Unwanted/glove.twitter.27B.200d.txt'
embed_dim = 100 #word vector dim
max_features, embedding_matrix = get_embed_mat(EMBEDDING_FILE)

# train the model
model = get_model(max_features, embed_dim, embedding_matrix)
model.fit(X_train, y_train, validation_data=(X_test, y_test),epochs=epochs, batch_size=batch_size, verbose=2)

sub = pd.read_csv('/content/drive/My Drive/Colab Notebooks/analysis.csv')
sub['Prediction Sentiment '] =  model.predict_classes(temp, batch_size=batch_size, verbose=0)
sub.to_csv("/content/drive/My Drive/Colab Notebooks/predict.csv", index=False)

这是我用来预测外部值的代码:

a=['completed', 'running', 'new', 'york', 'marathon', 'requested', 'pool', 'ride', 'back', 'hotel']
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
tk = Tokenizer()
tk.fit_on_texts(a)
index_list = tk.texts_to_sequences(a)
a = pad_sequences(index_list, maxlen=50)
sentiment = model.predict(a)
print(sentiment)

输出如下:

[[0.9001644  0.09983556]
 [0.8839435  0.11605652]
 [0.9005757  0.09942431]
 [0.9305595  0.06944045]
 [0.85847026 0.14152978]
 [0.8978375  0.10216247]
 [0.93535316 0.06464689]
 [0.9622155  0.03778455]
 [0.7891844  0.2108156 ]
 [0.9265106  0.07348941]]

这是什么意思?我怎样才能得到情绪的预测?

【问题讨论】:

    标签: python tensorflow keras lstm recurrent-neural-network


    【解决方案1】:

    您需要使用dict 来存储您的实际标签:

    act_labels = {val: ind for ind, val in enumerate(df['sentiment'].unique())}
    df['sentiment'] = df['sentiment'].replace(act_labels)
    y = to_categorical(df['sentiment'])
    

    要获得预测,您需要执行以下操作:

    rev_act_labels = {val: key for key, val in act_labels.items()}
    predictions = np.argmax(sentiment, axis=1)
    act_predictions  = [rev_act_labels[val] for val in predictions]
    print(act_predictions)
    

    如果model.predict_classes 有效,那么您可以尝试以下方法:

    rev_act_labels = {val: key for key, val in act_labels.items()}
    sentiment = model.predict_classes(a)
    # show the inputs and predicted outputs
    for i in range(len(sentiment)):
        print("X=%s, Predicted=%s" % (a[i], rev_act_labels[sentiment[i])])
    

    【讨论】:

    • 这是 Model.predict_classes X=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 的输出0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1],预测=0
    • for ng.argmax : array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    • np.argmax(sentiment, axis=1) 给出数组[[0.9479169 0.05208309] [0.94631827 0.05368178] ... 的预测值,你想得到像positive or negative 这样的实际情绪吗
    • 是的。我需要实际的情绪是积极的还是消极的
    • 我已经更新了解决方案,如果您遇到任何问题,请告诉我
    猜你喜欢
    • 2021-06-30
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2019-01-05
    • 1970-01-01
    • 2019-07-05
    相关资源
    最近更新 更多