-
Notifications
You must be signed in to change notification settings - Fork 2
/
cnn_deep_german.py
184 lines (138 loc) · 4.79 KB
/
cnn_deep_german.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import sys
import tensorflow as tf
from datetime import datetime
from read_data import read_data_sets
from cnn_word_model import CNNWordModel
MAX_WORD_LEN = 31
ALPHABET_SIZE = 31
NUM_GENDERS = 3
EPOCHS = 50
BATCH_SIZE = 128 # -batch 128, 256, or 512
NUM_LAYERS = 3 # -layers 0, 1, 2, or 3
DROPOUT_RATE = 0.5 # -dropout 0.0 or 0.5
LEARNING_RATE = 1e-3 # -learning 1e-2, 1e-3, or 1e-4
WINDOW_SIZE = 7 # -window 3, 5, 7, or 9
CONV_FILTERS = [32, 64]
NUM_HIDDEN = [512, 256, 128]
def echo(*args):
print("[{0}] ".format(datetime.now()), end="")
print(*args)
def log(file, message=""):
file.write(message + "\n")
if message != "":
echo(message)
else:
print()
while len(sys.argv) > 1:
option = sys.argv[1]; del sys.argv[1]
if option == "-batch":
BATCH_SIZE = int(sys.argv[1]); del sys.argv[1]
elif option == "-window":
WINDOW_SIZE = int(sys.argv[1]); del sys.argv[1]
elif option == "-dropout":
DROPOUT_RATE = float(sys.argv[1]); del sys.argv[1]
elif option == "-learning":
LEARNING_RATE = float(sys.argv[1]); del sys.argv[1]
else:
print(sys.argv[0], ": invalid option", option)
sys.exit(1)
model_name = "{0}_{1}_{2}_{3}_{4}_{5}".format(
"CNN", NUM_LAYERS, WINDOW_SIZE,
LEARNING_RATE, DROPOUT_RATE, BATCH_SIZE
)
log_path = "./results/logs/" + model_name + ".txt"
model_path = "./results/models/" + model_name + ".ckpt"
log_file = open(log_path, "w")
print("hidden layers:", NUM_LAYERS)
print("hidden units:", NUM_HIDDEN[:NUM_LAYERS])
print("conv. filters:", CONV_FILTERS)
print("window size:", WINDOW_SIZE)
print("learning rate:", LEARNING_RATE)
print("dropout rate:", DROPOUT_RATE)
print("batch size:", BATCH_SIZE)
print()
# BUILDING GRAPH
echo("Creating placeholders...")
xs = tf.placeholder(tf.float32, [None, MAX_WORD_LEN, ALPHABET_SIZE])
ys = tf.placeholder(tf.float32, [None, NUM_GENDERS])
dropout = tf.placeholder(tf.float32)
echo("Creating model...")
model = CNNWordModel(
xs, ys, dropout,
CONV_FILTERS, WINDOW_SIZE, NUM_LAYERS, NUM_HIDDEN,
tf.train.AdamOptimizer(LEARNING_RATE)
)
# PREPARING DATA
echo("Preparing data...")
# preparing words dataset
dataset = read_data_sets()
print()
echo("Training set:", dataset.train.words.shape[0])
echo("Validation set:", dataset.validation.words.shape[0])
echo("Testing set:", dataset.test.words.shape[0])
print()
# EXECUTING THE GRAPH
best_epoch = 0
best_val_error = 1.0
saver = tf.train.Saver()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
echo("Initializing variables...")
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
echo("Training...")
print()
steps_per_epoch = dataset.train.words.shape[0] // BATCH_SIZE
for epoch in range(1, EPOCHS+1):
for step in range(steps_per_epoch):
batch_xs, batch_ys, seq_len = dataset.train.next_batch(BATCH_SIZE)
sess.run(
model.training,
feed_dict={
xs: batch_xs,
ys: batch_ys,
dropout: DROPOUT_RATE
}
)
val_loss, val_error = 0, 0
val_batches = dataset.validation.words.shape[0] // 1024
for i in range(val_batches):
b_val_loss, b_val_error = sess.run(
[model.loss, model.error],
feed_dict={
xs: dataset.validation.words[1024 * i:1024 * (i + 1)],
ys: dataset.validation.genders[1024 * i:1024 * (i + 1)],
dropout: 0.0
}
)
val_loss += b_val_loss / val_batches
val_error += b_val_error / val_batches
if val_error < best_val_error:
best_epoch = epoch
best_val_error = val_error
saver.save(sess, model_path)
log(log_file, "Epoch {:2d}: error {:3.2f}% loss {:.4f}".format(
epoch, 100 * val_error, val_loss
))
saver.restore(sess, model_path)
test_loss, test_error = 0, 0
test_batches = dataset.test.words.shape[0] // 1024
for i in range(test_batches):
b_test_loss, b_test_error = sess.run(
[model.loss, model.error],
feed_dict={
xs: dataset.test.words[1024 * i:1024 * (i + 1)],
ys: dataset.test.genders[1024 * i:1024 * (i + 1)],
dropout: 0.0
}
)
test_loss += b_test_loss / test_batches
test_error += b_test_error / test_batches
log(log_file)
log(log_file, "Best epoch: {0}".format(best_epoch))
log(log_file)
log(log_file, "Test Set: error {:3.2f}% loss {:.4f}".format(
100 * test_error, test_loss
))
log_file.close()