-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlstm.py
More file actions
29 lines (23 loc) · 878 Bytes
/
lstm.py
File metadata and controls
29 lines (23 loc) · 878 Bytes
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
import tensorflow as tf
from keras import layers,models
from keras.datasets import imdb
from keras.preprocessing import sequence
#load and process the imdb dataset
max_features = 10000
max_len = 500
(x_train,y_train),(x_test,y_test) = imdb.load_data(num_words = max_features)
x_train = sequence.pad_sequences(x_train,maxlen=max_len)
x_test = sequence.pad_sequences(x_test,maxlen=max_len)
#define the LSTM model
model = models.Sequential([
layers.Embedding(max_features, 32, input_length=max_len),
layers.LSTM(32),
layers.Dense(1,activation='sigmoid')
])
#compile the model
model.compile(optimizer='adam', loss ='binary_crossentropy', metrics=['accuracy'])
#train the model
model.fit(x_train, y_train, epochs=5, batch_size=64,validation_split=0.2)
#evaluate the model
test_loss,test_acc = model.evaluate(x_test,y_test)
print("Test Accuracy:",test_acc)