-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.py
42 lines (32 loc) · 1.37 KB
/
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
from util import *
from model import load_model
from keras.preprocessing.sequence import pad_sequences
from keras.preprocessing.text import Tokenizer
from keras.utils.np_utils import to_categorical
from visualize import plot_stats
def run():
args = prepare_argparser()
config, args = assert_and_compile_args(args)
print('Loading Dataset')
texts, labels_to_int, n_classes, labels = load_dataset(args.src)
print('Found {} lines and {} labels'.format(len(texts), n_classes))
tokenizer = Tokenizer(num_words=config.NB_WORDS)
tokenizer.fit_on_texts(texts)
sequences = tokenizer.texts_to_sequences(texts)
word_index = tokenizer.word_index
print('Found {} unique tokens.'.format(len(word_index)))
data = pad_sequences(sequences, maxlen=config.SEQUENCE_LEN)
# Transform labels to be categorical variables
labels = np.array(to_categorical(np.asarray(labels)), dtype=np.float32)
print('Shape of data tensor:', data.shape)
print('Shape of label tensor:', labels.shape)
print('Compiling Embedding Matrix')
config.add_embedding_matrix(word_index)
print('Loading Model')
model = load_model(config, n_classes)
print('Start Training...')
hist = model.fit((data, labels), validation_split=0.1,
epochs=config.NUM_EPOCHS, batch_size=config.BATCH_SIZE)
plot_stats(hist)
if __name__ == '__main__':
run()