-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathveritas.py
266 lines (208 loc) · 10.8 KB
/
veritas.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
'''search.py
This file may be used for kicking off the model, reading in data and preprocessing
'''
import argparse
import sys
#import models.keras_lstm as k
from models.attention import train_lstm_attention_model
from models.gru_attention import train_gru_attention_model
from models.du_attn_classifier import train_du_attention_model
from models.du_attn_classifier_lstm import train_du_attention_model_lstm
from utils import *
from gutenberg_data import *
from spooky_authorship import spooky_authorship_data
# sys.path.append("./models")
from models.baseline import *
# from models.sklearn_baselines import sklearn_train
from models.LSTM import *
# from models.attention import train_lstm_attention_model
# sys.path.append("./models")
from models.baseline import *
from models.sentence_wise_classification import *
from models.sklearn_baselines import sklearn_train
from reuters_data import create_reuters_data
from spooky_authorship import spooky_authorship_data
from models.vae import *
# Read in command line arguments to the system
def arg_parse():
parser = argparse.ArgumentParser(description='trainer.py')
parser.add_argument('--model', type=str, default='BASELINE', help="Model to run")
parser.add_argument('--train_type', type=str, default="GUTENBERG", help="Data type - Gutenberg or custom")
parser.add_argument('--train_path', type=str, default='data/british/', help='Path to the training set')
parser.add_argument('--test_path', type=str, default='data/gut-test/british', help='Path to the test set')
parser.add_argument('--train_options', type=str, default='', help="Extra train options, eg pos tags embeddings")
parser.add_argument('--sentencewise', type=bool, default=False, help="")
parser.add_argument('--kaggle', type=bool, default=False, help="")
parser.add_argument('--plot', type=bool, default=False, help="")
# Encoder-Decoder, VAE-RNN args
parser.add_argument('--reverse_input', type=bool, default=False)
parser.add_argument("--batch_size", type=int, default=1, help="batch size for encoder training")
parser.add_argument('--emb_dropout', type=float, default=0.2, help="dropout for embedding layer")
parser.add_argument('--rnn_dropout', type=float, default=0.2, help="dropout for RNN")
parser.add_argument('--bidirectional', type=bool, default=True, help="lstm birectional or not")
parser.add_argument('--hidden_size', type=int, default=300, help='hidden state dimensionality')
parser.add_argument('--word_vecs_path_input', type=str, default='data/glove.6B.300d.txt',
help='path to word vectors file')
parser.add_argument('--word_vecs_path', type=str, default='data/glove.6B.300d-relativized.txt',
help='path to word vectors file')
parser.add_argument('--embedding_size', type=int, default=300, help='Embedding size')
parser.add_argument('--epochs', type=int, default=8, help='Number of epochs')
parser.add_argument('--lr', type=float, default=1e-4 * 5, help='Learning rate')
parser.add_argument('--z_dim', type=int, default=50, help='Size of latent space')
args = parser.parse_args()
return args
def get_data(args):
assert args.train_type in ["GUTENBERG", "SPOOKY", "REUTERS"]
if args.train_type == 'GUTENBERG':
data = gutenberg_dataset(args.train_path, args.test_path, args=args)
elif args.train_type == "SPOOKY":
data = spooky_authorship_data(args=args)
elif args.train_type == "REUTERS":
data = create_reuters_data(args=args)
if args.sentencewise:
data = make_sentencewise_data(data)
return data
"""
\dhruv{
We compared these results with those we get by replacing the LSTM cells with GRU cells in this instead of LSTM recurrent cells. It seems that the results do not differ too much, but the LSTM model does better across datasets. The GRU model achieves 76.6\% accuracy on the Spooky dataset, 30.8\% on the Gutenberg dataset, and 94.6\% on the Reuters dataset.
}
"""
if __name__ == "__main__":
args = arg_parse()
print(args)
if args.model == 'BASELINE':
# Get books from train path and call baselineb model train function
train_data, test_data, authors = get_data(args)
print("training baseline model")
baseline_model = train_baseline(train_data)
print("testing baseline")
baseline_model.evaluate(test_data, args)
elif args.model == 'LSTM':
data = get_data(args)
train_data, test_data, authors = data
flattened_test_data = list(itertools.chain(*test_data)) if args.sentencewise else test_data
word_indexer = Indexer()
add_dataset_features(train_data, word_indexer)
add_dataset_features(flattened_test_data, word_indexer)
if args.train_options == 'POS':
pretrained = False
word_indexer.get_index(PAD_SYMBOL)
word_indexer.get_index(UNK_SYMBOL)
word_vectors = WordEmbeddings(word_indexer, None)
else:
pretrained = True
relativize(args.word_vecs_path_input, args.word_vecs_path, word_indexer)
word_vectors = read_word_embeddings(args.word_vecs_path)
print("Finished extracting embeddings")
print("training")
trained_model: AuthorshipModel = train_lstm_model(train_data, flattened_test_data, authors, word_vectors, args,
pretrained=pretrained)
trained_model.evaluate(test_data, args)
elif args.model == "LSTM_ATTN":
data = get_data(args)
train_data, test_data, authors = data
flattened_test_data = list(itertools.chain(*test_data)) if args.sentencewise else test_data
word_indexer = Indexer()
add_dataset_features(train_data, word_indexer)
add_dataset_features(flattened_test_data, word_indexer)
if args.train_options == 'POS':
pretrained = False
word_indexer.get_index(PAD_SYMBOL)
word_indexer.get_index(UNK_SYMBOL)
word_vectors = WordEmbeddings(word_indexer, None)
else:
pretrained = True
relativize(args.word_vecs_path_input, args.word_vecs_path, word_indexer)
word_vectors = read_word_embeddings(args.word_vecs_path)
print("Finished extracting embeddings")
print("training")
trained_model = train_lstm_attention_model(train_data, flattened_test_data, authors, word_vectors, args, pretrained=pretrained)
print("testing")
trained_model.evaluate(test_data, args)
elif args.model == "GRU_ATTN":
data = get_data(args)
train_data, test_data, authors = data
flattened_test_data = list(itertools.chain(*test_data)) if args.sentencewise else test_data
word_indexer = Indexer()
add_dataset_features(train_data, word_indexer)
add_dataset_features(flattened_test_data, word_indexer)
if args.train_options == 'POS':
pretrained = False
word_indexer.get_index(PAD_SYMBOL)
word_indexer.get_index(UNK_SYMBOL)
word_vectors = WordEmbeddings(word_indexer, None)
else:
pretrained = True
relativize(args.word_vecs_path_input, args.word_vecs_path, word_indexer)
word_vectors = read_word_embeddings(args.word_vecs_path)
print("Finished extracting embeddings")
print("training")
trained_model = train_gru_attention_model(train_data, flattened_test_data, authors, word_vectors, args, pretrained=pretrained)
print("testing")
trained_model.evaluate(test_data, args)
elif args.model == "DU_ATTN":
data = get_data(args)
train_data, test_data, authors = data
flattened_test_data = list(itertools.chain(*test_data)) if args.sentencewise else test_data
word_indexer = Indexer()
add_dataset_features(train_data, word_indexer)
add_dataset_features(flattened_test_data, word_indexer)
if args.train_options == 'POS':
print("USING POS EMBEDDINGS")
pretrained = False
word_indexer.get_index(PAD_SYMBOL)
word_indexer.get_index(UNK_SYMBOL)
word_vectors = WordEmbeddings(word_indexer, None)
else:
pretrained = True
relativize(args.word_vecs_path_input, args.word_vecs_path, word_indexer)
word_vectors = read_word_embeddings(args.word_vecs_path)
print("Finished extracting embeddings")
print("training")
trained_model = train_du_attention_model(train_data, flattened_test_data, authors, word_vectors, args, pretrained=pretrained)
print("testing")
trained_model.evaluate(test_data, args)
elif args.model == "DU_ATTN_LSTM":
data = get_data(args)
train_data, test_data, authors = data
flattened_test_data = list(itertools.chain(*test_data)) if args.sentencewise else test_data
word_indexer = Indexer()
add_dataset_features(train_data, word_indexer)
add_dataset_features(flattened_test_data, word_indexer)
if args.train_options == 'POS':
print("USING POS EMBEDDINGS")
pretrained = False
word_indexer.get_index(PAD_SYMBOL)
word_indexer.get_index(UNK_SYMBOL)
word_vectors = WordEmbeddings(word_indexer, None)
else:
pretrained = True
relativize(args.word_vecs_path_input, args.word_vecs_path, word_indexer)
word_vectors = read_word_embeddings(args.word_vecs_path)
print("Finished extracting embeddings")
print("training")
trained_model = train_du_attention_model_lstm(train_data, flattened_test_data, authors, word_vectors, args, pretrained=pretrained)
print("testing")
trained_model.evaluate(test_data, args)
elif args.model == "KERAS":
train_data, test_data, authors = get_data(args)
embeddings = read_word_embeddings(args.word_vecs_path)
k.train_keras_model(embeddings, train_data, test_data, authors)
elif args.model == "SKLEARN":
train_data, test_data, authors = data = get_data(args)
sklearn_train(train_data, test_data, authors, args)
elif args.model == 'VAE':
train_data, test_data, authors = spooky_authorship_data(args)
word_indexer = Indexer()
add_dataset_features(train_data, word_indexer)
add_dataset_features(test_data, word_indexer)
pretrained = True
relativize(args.word_vecs_path_input, args.word_vecs_path, word_indexer)
word_vectors = read_word_embeddings(args.word_vecs_path)
print("Finished extracting embeddings")
print("training")
trained_model = train_vae(train_data, test_data, authors, word_vectors, args, pretrained=pretrained)
print("testing")
trained_model.evaluate(test_data, args)
else:
raise Exception("Please select appropriate model")