CoolFace
Apppublic

KavyaK/DL-Models

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
simplelstm.py31 linesDownload Raw Back to root
1# LSTM for sequence classification in the IMDB dataset2import tensorflow as tf3from tensorflow.keras.datasets import imdb4from tensorflow.keras.models import Sequential5from tensorflow.keras.layers import Dense6from tensorflow.keras.layers import LSTM7from tensorflow.keras.layers import Embedding8from tensorflow.keras.preprocessing import sequence9# fix random seed for reproducibility10tf.random.set_seed(7)11# load the dataset but only keep the top n words, zero the rest12top_words = 500013(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words)14# truncate and pad input sequences15max_review_length = 50016X_train = sequence.pad_sequences(X_train, maxlen=max_review_length)17X_test = sequence.pad_sequences(X_test, maxlen=max_review_length)18# create the model19embedding_vecor_length = 3220model = Sequential()21model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length))22model.add(LSTM(100))23model.add(Dense(1, activation='sigmoid'))24model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])25print(model.summary())26model.fit(X_train, y_train, epochs=3, batch_size=64)27# Final evaluation of the model28scores = model.evaluate(X_test, y_test, verbose=0)29print("Accuracy: %.2f%%" % (scores[1]*100))30 31model.save('lstm.h5')