forked from probml/pyprobml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimdb_mlp_bow_tf.py
More file actions
162 lines (125 loc) · 4.56 KB
/
imdb_mlp_bow_tf.py
File metadata and controls
162 lines (125 loc) · 4.56 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Movie review classifier using keras. Based on
# https://www.tensorflow.org/tutorials/keras/basic_text_classification
from __future__ import absolute_import, division, print_function
import numpy as np
import matplotlib.pyplot as plt
import os
figdir = "../figures"
def save_fig(fname):
if figdir:
plt.savefig(os.path.join(figdir, fname))
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
np.random.seed(0)
imdb = keras.datasets.imdb
vocab_size = 10000
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=vocab_size)
print(np.shape(train_data)) # (25000)
print(len(train_data[0]))
print(len(train_data[1]))
print(train_data[0])
# [1, 14, 22, 16, 43, 530, 973, 1622, 1385, 65, 458, 4468, 66, 3941...]
word_index = imdb.get_word_index()
# The first indices are reserved
word_index = {k:(v+3) for k,v in word_index.items()}
word_index["<PAD>"] = 0
word_index["<START>"] = 1
word_index["<UNK>"] = 2 # unknown
word_index["<UNUSED>"] = 3
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
def decode_review(text):
return ' '.join([reverse_word_index.get(i, '?') for i in text])
decode_review(train_data[0])
"""
<START> this film was just brilliant casting location scenery story direction everyone's really suited the part
they played and you could just imagine being there robert <UNK> is an amazing actor and now the same being director <UNK>
father came from the same scottish island as myself ...
"""
train_data = keras.preprocessing.sequence.pad_sequences(
train_data, value=word_index["<PAD>"], padding='post', maxlen=256)
test_data = keras.preprocessing.sequence.pad_sequences(
test_data, value=word_index["<PAD>"], padding='post', maxlen=256)
print(train_data[0])
embed_size = 16
def make_model(embed_size):
tf.random.set_seed(42)
np.random.seed(42)
model = keras.Sequential()
model.add(keras.layers.Embedding(vocab_size, embed_size))
model.add(keras.layers.GlobalAveragePooling1D())
model.add(keras.layers.Dense(16, activation=tf.nn.relu))
model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid))
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['acc'])
return model
model = make_model(embed_size)
model.summary()
x_val = train_data[:10000]
x_train = train_data[10000:]
y_val = train_labels[:10000]
y_train = train_labels[10000:]
history = model.fit(x_train,
y_train,
epochs=50,
batch_size=512,
validation_data=(x_val, y_val),
verbose=1)
history_dict = history.history
print(history_dict.keys())
results = model.evaluate(test_data, test_labels)
print(results)
acc = history_dict['acc']
val_acc = history_dict['val_acc']
loss = history_dict['loss']
val_loss = history_dict['val_loss']
epochs = range(1, len(acc) + 1)
fig, ax = plt.subplots()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'r-', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
save_fig("imdb-loss.pdf")
plt.show()
fig, ax = plt.subplots()
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'r', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
save_fig("imdb-acc.pdf")
plt.show()
# Now turn on early stopping
# https://chrisalbon.com/deep_learning/keras/neural_network_early_stopping/
class PrintDot(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs):
if epoch % 100 == 0: print('')
print('.', end='')
callbacks = [PrintDot(),
keras.callbacks.EarlyStopping(monitor='val_acc', patience=2),
keras.callbacks.ModelCheckpoint(filepath='imdb_keras_best_model.ckpt',
monitor='val_acc', save_best_only=True)]
# Reset parameters to a new random state
model = make_model(embed_size)
history = model.fit(
x_train, y_train, epochs=50, batch_size=512,
validation_data=(x_val, y_val), verbose=0, callbacks=callbacks)
history_dict = history.history
acc = history_dict['acc']
val_acc = history_dict['val_acc']
loss = history_dict['loss']
val_loss = history_dict['val_loss']
epochs = range(1, len(acc) + 1)
fig, ax = plt.subplots()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'r-', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
save_fig("imdb-loss-early-stop.pdf")
plt.show()