-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex-8.py
More file actions
33 lines (24 loc) · 1.1 KB
/
Copy pathex-8.py
File metadata and controls
33 lines (24 loc) · 1.1 KB
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
'''Sentiment analysis example with IMDB from p. 211 of the book.'''
from keras.models import Sequential
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Embedding, LSTM, Dense
from keras.datasets import imdb
'Runs p. 211 sentinment analysis network.'
def main():
(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=5000)
vocab_size = max(max(X_train))
max_review_len = max([len(seq) for seq in X_train] + [len(seq) for seq in X_test])
X_train = pad_sequences(X_train, maxlen=max_review_len)
X_test = pad_sequences(X_test, maxlen=max_review_len)
model = Sequential()
model.add(Embedding(vocab_size, 100, input_length=max_review_len))
model.add(LSTM(256))
model.add(Dense(512))
model.add(Dense(512))
model.add(Dense(512))
model.add(Dense(1, activation='sigmoid'))
model.summary()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=64)
result = model.evaluate(X_test, y_test, verbose=0)
main()