keras实战: 用lstm对IMDB情感分析



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
# cut texts after this number of words (among top max_features most common words)
maxlen = 80
batch_size = 32

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

#'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)

#'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

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









©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页