Anjana-S commited on
Commit
b56c93a
·
1 Parent(s): ff8f3d4

Upload imdb_rnn.py

Browse files
Files changed (1) hide show
  1. imdb_rnn.py +65 -0
imdb_rnn.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import matplotlib.pyplot as plt
3
+ import seaborn as sns
4
+ from sklearn.model_selection import train_test_split
5
+ import tensorflow as tf
6
+
7
+
8
+ from numpy import argmax
9
+ from tensorflow.keras import Sequential
10
+ from tensorflow.keras.layers import Dense
11
+ from tensorflow.keras.optimizers import RMSprop, Adam
12
+ from tensorflow.keras.datasets import imdb
13
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
14
+ from sklearn.metrics import accuracy_score
15
+ import pickle
16
+
17
+ top_words = 5000
18
+ (X_train, y_train), (X_test,y_test) = imdb.load_data(num_words=top_words)
19
+
20
+ max_review_length = 500
21
+ X_train = pad_sequences(X_train, maxlen=max_review_length)
22
+ X_test = pad_sequences(X_test, maxlen=max_review_length)
23
+
24
+ model=tf.keras.models.Sequential([
25
+ tf.keras.layers.Embedding(input_dim=top_words,output_dim= 24, input_length=max_review_length),
26
+ tf.keras.layers.SimpleRNN(24, return_sequences=False),
27
+ tf.keras.layers.Dense(64, activation='relu'),
28
+ tf.keras.layers.Dense(32, activation='relu'),
29
+ tf.keras.layers.Dense(1, activation='sigmoid')
30
+ ])
31
+
32
+ # compile the model
33
+ model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
34
+
35
+ print("---------------------- -------------------------\n")
36
+
37
+ # summarize the model
38
+ print(model.summary())
39
+
40
+ print("---------------------- -------------------------\n")
41
+
42
+ early_stop = tf.keras.callbacks.EarlyStopping(monitor='accuracy', mode='min', patience=10)
43
+
44
+ print("---------------------- Training -------------------------\n")
45
+
46
+ # fit the model
47
+ model.fit(x=X_train,
48
+ y=y_train,
49
+ epochs=100,
50
+ validation_data=(X_test, y_test),
51
+ callbacks=[early_stop]
52
+ )
53
+ print("---------------------- -------------------------\n")
54
+
55
+
56
+ def acc_report(y_true, y_pred):
57
+ acc_sc = accuracy_score(y_true, y_pred)
58
+ print(f"Accuracy : {str(round(acc_sc,2)*100)}")
59
+ return acc_sc
60
+
61
+
62
+ preds = (model.predict(X_test) > 0.5).astype("int32")
63
+ print(acc_report(y_test, preds))
64
+
65
+ model.save(r'C:\Users\shahi\Desktop\My Projects\DeepPredictorHub\RN.keras')