In [0]:
!pip install -q tf-nightly-gpu-2.0-preview
In [2]:
import tensorflow as tf
print(tf.__version__)
In [0]:
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf
In [0]:
# https://keras.io/datasets/#imdb-movie-reviews-sentiment-classification
max_features = 10000 # number of words to consider as features
maxlen = 500 # cut texts after this number of words (among top max_features most common words)
# each review is encoded as a sequence of word indexes
# indexed by overall frequency in the dataset
# output is 0 (negative) or 1 (positive)
imdb = tf.keras.datasets.imdb.load_data(num_words=max_features)
(raw_input_train, y_train), (raw_input_test, y_test) = imdb
# https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/sequence/pad_sequences
input_train = tf.keras.preprocessing.sequence.pad_sequences(raw_input_train, maxlen=maxlen)
input_test = tf.keras.preprocessing.sequence.pad_sequences(raw_input_test, maxlen=maxlen)
In [5]:
from tensorflow.keras.layers import GRU, Embedding, BatchNormalization, Dropout
from tensorflow.keras.models import Sequential, Model
embedding_dim = 32
dropout = 0.7
model = Sequential()
# encoder
model.add(Embedding(input_dim=max_features, output_dim=embedding_dim, input_length=maxlen))
# return_sequences passes all outputs of all timesteps (not only the last one) to the next layer
model.add(GRU(name='gru1', units=32, return_sequences=True))
# for embedding: 32*2 (“standard deviation” parameter (gamma), “mean” parameter (beta)) trainable parameters
# and 32*2 (moving_mean and moving_variance) non-trainable parameters
model.add(BatchNormalization())
model.add(Dropout(dropout))
# stack recurrent layers like with fc
model.add(GRU(name='gru2', units=32))
model.add(BatchNormalization())
model.add(Dropout(dropout))
# latent space
model.add(tf.keras.layers.Dense(name='fc', units=32, activation='relu'))
# binary classifier as decoder
model.add(tf.keras.layers.Dense(name='classifier', units=1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.summary()
In [6]:
batch_size = 1000
%time history = model.fit(input_train, y_train, epochs=15, batch_size=batch_size, validation_split=0.4)
In [7]:
import matplotlib.pyplot as plt
plt.ylabel("accuracy")
plt.xlabel("epochs")
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.legend(["Accuracy", "Valdation Accuracy"])
Out[7]:
In [8]:
train_loss, train_accuracy = model.evaluate(input_train, y_train, batch_size=batch_size)
train_accuracy
Out[8]:
In [9]:
test_loss, test_accuracy = model.evaluate(input_test, y_test, batch_size=batch_size)
test_accuracy
Out[9]:
In [20]:
# precition
model.predict(input_test[0:5])
Out[20]:
In [11]:
# ground truth
y_test[0:5]
Out[11]: