Anjana-S commited on
Commit
e254969
·
1 Parent(s): 529c0f4

Upload 2 files

Browse files
Files changed (1) hide show
  1. simplelstm.py +32 -0
simplelstm.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # LSTM for sequence classification in the IMDB dataset
2
+ import tensorflow as tf
3
+ from tensorflow.keras.datasets import imdb
4
+ from tensorflow.keras.models import Sequential
5
+ from tensorflow.keras.layers import Dense
6
+ from tensorflow.keras.layers import LSTM
7
+ from tensorflow.keras.layers import Embedding
8
+ from tensorflow.keras.preprocessing import sequence
9
+
10
+ # fix random seed for reproducibility
11
+ tf.random.set_seed(7)
12
+ # load the dataset but only keep the top n words, zero the rest
13
+ top_words = 5000
14
+ (X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words)
15
+ # truncate and pad input sequences
16
+ max_review_length = 500
17
+ X_train = sequence.pad_sequences(X_train, maxlen=max_review_length)
18
+ X_test = sequence.pad_sequences(X_test, maxlen=max_review_length)
19
+ # create the model
20
+ embedding_vecor_length = 32
21
+ model = Sequential()
22
+ model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length))
23
+ model.add(LSTM(200))
24
+ model.add(Dense(1, activation='sigmoid'))
25
+ model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
26
+ print(model.summary())
27
+ model.fit(X_train, y_train, epochs=20, batch_size=64)
28
+ # Final evaluation of the model
29
+ scores = model.evaluate(X_test, y_test, verbose=0)
30
+ print("Accuracy: %.2f%%" % (scores[1]*100))
31
+
32
+ model.save(r'C:\Users\shahi\Desktop\My Projects\DeepPredictorHub\LS.keras')