-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlstm-sentiment.py
38 lines (31 loc) · 1.22 KB
/
lstm-sentiment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from __future__ import print_function
import numpy as np
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Activation, Embedding
from keras.layers import LSTM
from keras.datasets import imdb
np.random.seed(108)
max_features = 30000 # Vocabulary size
maxlen = 512 # max words in a review
batch_size = 32
# Read data
(X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=max_features)
print("train size-{}".format(len(X_train)))
print("test size-{}".format(len(X_test)))
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('Build model')
model = Sequential()
model.add(Embedding(max_features, 128, dropout=0.2))
model.add(LSTM(128, dropout_W=0.2, dropout_U=0.2))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['precision', 'recall', 'accuracy', 'fmeasure'])
print('Train...')
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=3,
validation_data=(X_test, y_test))
score = model.evaluate(X_test, y_test, batch_size=batch_size)
print('Test score: {}'.format(score))