-
Notifications
You must be signed in to change notification settings - Fork 1
/
rnn_binary_train.py
128 lines (102 loc) · 4.19 KB
/
rnn_binary_train.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
import os
import random
random.seed(961)
import argparse
import numpy as np
import pickle as pkl
import tensorflow as tf
from os.path import join
from keras.models import Input, Model
from keras.layers import Dense, Dropout, Bidirectional
from keras.layers import GRU, CuDNNGRU, LSTM, CuDNNLSTM
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint, Callback
from helpers import *
from rnn_binary_data_generator import DataGenerator
def build_model(embeddings_size):
tokens_embeddings_input = Input(shape=(None, embeddings_size,))
lstm = Bidirectional(
LSTM(
units=128,
dropout=args.dropout_rate,
return_sequences=True,
kernel_initializer='he_normal'
)
)(tokens_embeddings_input)
lstm = Bidirectional(
LSTM(
units=128,
dropout=args.dropout_rate,
kernel_initializer='he_normal'
)
)(lstm)
dense = Dropout(args.dropout_rate)(
Dense(units=256, activation='relu', kernel_initializer='he_normal')(lstm)
)
dense = Dropout(args.dropout_rate)(
Dense(units=128, activation='relu', kernel_initializer='he_normal')(dense)
)
output = Dense(units=1, activation='sigmoid', kernel_initializer='he_normal')(dense)
model = Model(tokens_embeddings_input, output)
model.compile(optimizer=Adam(lr=0.001), loss='binary_crossentropy', metrics=['accuracy', f1])
model.summary()
return model
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data-dir', default='data_dir')
parser.add_argument('--epochs', default=5, type=int)
parser.add_argument('--batch-size', default=128, type=int)
parser.add_argument('--dropout-rate', default=0.2, type=float)
parser.add_argument('--dev-split', default=0, type=float)
parser.add_argument('--bert-model-type', default='uncased', choices=['uncased', 'cased'])
args = parser.parse_args()
# shut up tensorflow and keras
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
with open(join(args.data_dir, 'train-data-from-bert-%s.pkl' % args.bert_model_type), 'rb') as file:
train_data = pkl.load(file)
train_embeddings = list()
train_labels = list()
for example in train_data:
if len(train_data[example][0]) == 2: continue
train_embeddings.append([item.numpy() for item in train_data[example][0]])
train_labels.append(int(train_data[example][1]))
train_data = list(zip(train_embeddings, train_labels))
random.shuffle(train_data)
if args.dev_split != 0:
split_point = int(len(train_embeddings) * args.dev_split)
dev_data = train_data[:split_point]
train_data = train_data[split_point:]
train_data = sorted(train_data, key=lambda item: len(item[0]))
dev_data = sorted(dev_data, key=lambda item: len(item[0]))
train_embeddings, train_labels = zip(*train_data)
train_embeddings = np.array(train_embeddings)
train_labels = np.array(train_labels)
dev_embeddings, dev_labels = zip(*dev_data)
dev_embeddings = np.array(dev_embeddings)
dev_labels = np.array(dev_labels)
else:
train_data = sorted(train_data, key=lambda item: len(item[0]))
train_embeddings, train_labels = zip(*train_data)
train_embeddings = np.array(train_embeddings)
train_labels = np.array(train_labels)
model = build_model(len(train_embeddings[0][0]))
train_generator = DataGenerator(train_embeddings, train_labels, args.batch_size)
if args.dev_split != 0:
dev_generator = DataGenerator(dev_embeddings, dev_labels, args.batch_size)
checkpoint_cb = ModelCheckpoint(
filepath='checkpoints/rnn-binary-bert-%s-epoch{epoch:02d}.h5' % args.bert_model_type
)
if args.dev_split != 0:
model.fit_generator(
generator=train_generator,
validation_data=dev_generator,
epochs=args.epochs,
callbacks=[checkpoint_cb]
)
else:
model.fit_generator(
generator=train_generator,
epochs=args.epochs,
callbacks=[checkpoint_cb]
)