-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrnn.py
More file actions
31 lines (25 loc) · 948 Bytes
/
rnn.py
File metadata and controls
31 lines (25 loc) · 948 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
29
30
31
#import neccesary libraries
import tensorflow as tf
from keras import layers, models
from keras.datasets import imdb
from keras.utils import pad_sequences
from keras.preprocessing import sequence
#load and preprocess 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 RNN
model = models.Sequential([
layers.Embedding(max_features, 32, input_length=max_len),
layers.SimpleRNN(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)