用LSTM进行Keras情绪分析如何测试 [英] Keras sentiment analysis with LSTM how to test it

查看:85
本文介绍了用LSTM进行Keras情绪分析如何测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用示例

I'm trying to do sentiment analysis with Keras on my texts using example imdb_lstm.py but I dont know how to test it. I stored my model and weights into file and it look like this:

model = model_from_json(open('my_model_architecture.json').read())
model.compile(loss='binary_crossentropy',
          optimizer='adam',
          metrics=['accuracy'])
model.load_weights('my_model_weights.h5')

results = model.evaluate(X_test, y_test, batch_size=32)

但是我当然不知道X_testy_test应该是什么样子.有人可以帮我吗?

but ofcourse I dont know how should X_test and y_test look like. Would anyone pls help me?

推荐答案

首先,将数据集拆分为testvalidtrain并进行一些预处理:

First, split your dataset to test, valid and train and do some preprocessing:

from tensorflow import keras

print('load data')
(x_train, y_train), (x_test, y_test) = keras.datasets.imdb.load_data(num_words=10000)
word_index = keras.datasets.imdb.get_word_index()

print('preprocessing...')
x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=256)
x_test = keras.preprocessing.sequence.pad_sequences(x_test, maxlen=256)

x_val = x_train[:10000]
y_val = y_train[:10000]

x_train = x_train[10000:]
y_train = y_train[10000:]

如您所见,我们还加载了word_index,因为稍后我们需要它来将句子转换为整数序列.

As you see we also load word_index because we need it later to convert our sentence to the sequence of integers.

第二,定义模型:

print('build model')
model = keras.Sequential()
model.add(keras.layers.Embedding(10000, 16))
model.add(keras.layers.LSTM(100))
model.add(keras.layers.Dense(16, activation='relu'))
model.add(keras.layers.Dense(1, activation='sigmoid'))

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

print('train model')
model.fit(x_train,
          y_train,
          epochs=5,
          batch_size=512,
          validation_data=(x_val, y_val),
          verbose=1)

最后saveload您的模型具有:

print('save trained model...')
model.save('sentiment_keras.h5')
del model

print('load model...')
from keras.models import load_model
model = load_model('sentiment_keras.h5')

您可以使用test-set评估模型:

print('evaluation')
evaluation = model.evaluate(x_test, y_test, batch_size=512)
print('Loss:', evaluation[0], 'Accuracy:', evaluation[1])

如果您要用全新的句子测试模型,可以执行以下操作:

If you want to test the model on completely new sentence you can do:

sample = 'this is new sentence and this very bad bad sentence'
sample_label = 0
# convert input sentence to tokens based on word_index
inps = [word_index[word] for word in sample.split() if word in word_index]
# the sentence length should be the same as the input sentences
inps = keras.preprocessing.sequence.pad_sequences([inps], maxlen=256)
print('Accuracy:', model.evaluate(inps, [sample_label], batch_size=1)[1])
print('Sentiment score: {}'.format(model.predict(inps)[0][0]))

这篇关于用LSTM进行Keras情绪分析如何测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆