This repository has been archived by the owner on Apr 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
145 lines (119 loc) · 3.48 KB
/
training.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
import argparse
from argparse import ArgumentParser
from .argtypes import (
positive_integer,
rate,
filter_configuration,
word2vec_model
)
from .util import parse_tweets
from .cnn import CNN
# TODO Do these belong here even?
DEFAULT_BATCH_SIZE = 50
DEFAULT_EPOCHS = 1
def parse_args():
parser = ArgumentParser(description='Train a CNN')
# TODO More validations for these parameters?
parser.add_argument(
'-t', '--dataset', required=True,
type=argparse.FileType('r'),
)
# TODO Making a subcommand mandatory
# currently requires setting a `dest`
# and setting the `required` property.
# I don't think this is intended behavior
subparsers = parser.add_subparsers(dest='command')
subparsers.required = True
load_subparser = subparsers.add_parser('load')
load_subparser.add_argument('model') # TODO Validation
new_subparser = subparsers.add_parser('new')
new_subparser.add_argument(
'-e', '--embeddings', required=True,
type=word2vec_model,
)
new_subparser.add_argument(
'-f', '--filters', required=True,
type=filter_configuration,
)
new_subparser.add_argument(
'-v', '--vocabulary-size',
type=positive_integer
)
new_subparser.add_argument(
'-d', '--dropout-rate',
type=rate
)
# TODO Validate this
new_subparser.add_argument(
'-a', '--activation',
default='linear',
help='default: %(default)s'
)
parser.add_argument(
'-c', '--epochs',
type=positive_integer,
default=DEFAULT_EPOCHS,
help='default: %(default)s'
)
parser.add_argument(
'-b', '--batch',
type=positive_integer,
default=DEFAULT_BATCH_SIZE,
help='default: %(default)s'
)
# TODO We should ensure that this directory exists or create it
parser.add_argument('-o', '--output', required=True)
return parser.parse_args()
def train(
dataset,
embeddings,
vocabulary_size,
filters,
dropout_rate,
activation,
epochs,
batch_size,
model = None
):
tweet_count = sum(1 for tweet in parse_tweets(dataset))
cnn = CNN()
if model:
print('loading preexisting model')
cnn.load(model)
else:
print('building network')
# TODO This call looks very strange ...
cnn.build_network(
embeddings,
filters,
vocabulary_size=vocabulary_size,
dropout_rate=dropout_rate,
classes=3
)
print('training')
# We have to read the file here, again, possibly multiple times
# the previous iterator does not work anymore
cnn.fit_generator(
lambda: parse_tweets(dataset), # TODO This is ugly
nb_epoch=epochs,
batch_size=batch_size,
samples_per_epoch=tweet_count
)
return cnn
def main():
args = parse_args()
cnn = train(
args.dataset.name,
# TODO Wow, what a hack. O_o
args.embeddings if args.command == 'new' else None,
args.vocabulary_size if args.command == 'new' else None,
args.filters if args.command == 'new' else None,
args.dropout_rate if args.command == 'new' else None,
args.activation if args.command == 'new' else None,
args.epochs,
args.batch,
args.model if args.command == 'load' else None
)
cnn.save(args.output)
if __name__ == '__main__':
main()