Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用LSTM进行Keras情绪分析如何检验_Python_Neural Network_Keras_Lstm - Fatal编程技术网

Python 用LSTM进行Keras情绪分析如何检验

Python 用LSTM进行Keras情绪分析如何检验,python,neural-network,keras,lstm,Python,Neural Network,Keras,Lstm,我试图用Keras对我的文章进行情感分析,但我不知道如何测试它。 我将模型和权重存储到文件中,如下所示: model = model_from_json(open('my_model_architecture.json').read()) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.load_weights('my_model

我试图用Keras对我的文章进行情感分析,但我不知道如何测试它。 我将模型和权重存储到文件中,如下所示:

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\u测试和
y\u测试应该是什么样子。有人能帮我吗?

首先,将数据集拆分为
测试
有效
训练
,并进行一些预处理:

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\u索引
,因为我们以后需要它来将我们的句子转换为整数序列

第二步,定义您的模型:

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)
最后
保存
加载
您的模型:

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')
您可以使用
测试集评估您的模型

print('evaluation')
evaluation = model.evaluate(x_test, y_test, batch_size=512)
print('Loss:', evaluation[0], 'Accuracy:', evaluation[1])
如果您想在全新的句子上测试模型,您可以执行以下操作:

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]))

您应该将数据分成培训和测试部分,一部分进行培训,另一部分进行测试,这就是您需要dothx回复的全部内容。你能给我举个小例子吗?模型已经通过imdb数据集进行了训练。您提供的链接(到.py文件)具有加载测试数据和计算结果的工作示例。但是,您不能使用“开箱即用”的经过训练的模型,因为您需要知道它是在哪部分数据上训练的。这就是为什么第32行将数据集拆分为训练和测试块的原因。我不明白,但我如何才能输入一个句子并获得它的情感?它实际上相当复杂,因为您使用keras预处理数据进行训练,所以您必须以同样的方式预处理自己的数据,因此在这一点上无法重建过程,因为它依赖于数据。因此,如果没有实际的训练/测试分割,您将无法重建预处理技术。