keras lstm分析imdb

今天就是各种想不务正业,就想尝试一下keras的情感分析

源码:https://github.com/fchollet/keras/blob/master/examples/imdb_lstm.py

这里比较麻烦的一点是,因为网络问题,这个imdb的数据文件一直用程序下不下来,建议直接去网站下imdb.npz文件,地址是:https://s3.amazonaws.com/text-datasets/imdb.npz,下载下来后直接复制到项目文件目录下。然后修改一下keras.dataset里面imdb.py文件。

文件在:/你的python目录/lib/python3.5/site-packages/keras/datasets
直接把第51行注释掉

51 #path = get_file(path, origin='https://s3.amazonaws.com/text-datasets/imdb.npz')

运行程序,代码如下:

from __future__ import print_function

from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Embedding
from keras.layers import LSTM
from keras.datasets import imdb

max_features = 20000
maxlen = 80  # cut texts after this number of words (among top max_features most common words)
batch_size = 32

print('Loading data...')
(x_train, y_train), (x_test, y_test) = imdb.load_data(path='./imdb.npz',num_words=max_features)
print(len(x_train), 'train sequences')
print(len(x_test), 'test sequences')

print('Pad sequences (samples x time)')
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)

print('Build model...')
model = Sequential()
model.add(Embedding(max_features, 128))
model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))

# try using different optimizers and different optimizer configs
model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

print('Train...')
model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=15,
          validation_data=(x_test, y_test))
score, acc = model.evaluate(x_test, y_test,
                            batch_size=batch_size)
print('Test score:', score)
print('Test accuracy:', acc)

运行结果,大概准确率在81%,感觉过拟合。。。:
keras lstm分析imdb_第1张图片

你可能感兴趣的:(python,深度学习)