/
Test.py
296 lines (232 loc) · 11.6 KB
/
Test.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
__author__ = 'eric_rincon'
import numpy
import theano
import copy
import os
import sys
from Feature_Engineer import Feature_Engineer
from sklearn.linear_model import Perceptron
from os import listdir
from os.path import isfile, join
from DataLoader import DataLoader
from NeuralNet import NeuralNet
from SVM import SVM
from scipy import sparse
from csv import DictWriter
import csv
def main():
transfer_learning()
def transfer_learning(print_output=True):
path = 'datasets/'
data_loader = DataLoader(path)
names = {1: 'title', 4: 'abstract', 5: 'mesh', 'y': 6}
transformed_data_sets = []
path = 'datasets/'
files = [f for f in listdir(path) if isfile(join(path,f))]
files.pop(0)
data_loader = DataLoader(path)
domains = data_loader.csv_files
all_domains = copy.deepcopy(domains)
training_domains = data_loader.csv_files
all_domains_svm_wda_metrics_list = []
all_domains_svm_metrics_list = []
all_domains_svm_bow_mlp_list = []
all_domains_mlp_fold_scores = []
for i, held_out_domain in enumerate(domains):
training_domains.pop(i)
names = {1: 'title', 4: 'abstract', 5: 'mesh', 'y': 6}
svm_wda_metrics_list = []
svm_metrics_list = []
svm_bow_mlp_list = []
folder_name = '/' + files[i]
domain_name = files[i].__str__()
domain_name = domain_name.split('.')[0]
folder_name = 'output' + '/' + domain_name
output = "Dataset: {}".format(files[i])
if print_output:
print(output)
#shuffle(data_loader.csv_files)
data_loader.csv_files = training_domains
data_sets = data_loader.csv_files
domains = data_loader.get_feature_matrix(names)
#Get one file out of the csv files in the dataloader use this as the held out domain
#Get the feature representation of the held out data
held_out_x, held_out_y = data_loader.get_feature_matrix(names, held_out_domain)
#Create the folds for the held out data in this case the default 5
folds = data_loader.cross_fold_valdation(held_out_x, held_out_y)
#Get the total number of domains i.e., the number of files with documents
n_source_domains = len(data_sets)
os.makedirs(folder_name)
#Must convert the data type of the matrix for theano
feature_engineer = Feature_Engineer()
#Start the 5 fold cross validation
for n_fold, fold in enumerate(folds):
output = "Fold {}: \n".format(n_fold)
if print_output:
print(output)
output = '{}/{}/fold_{}.csv'.format(os.getcwd(), folder_name, (n_fold + 1))
file = open(output, 'w')
csv_writer = csv.writer(file)
#Each sample is a list that contains the x and y for the classifier
#Typically fold[0] would be the train sample but because it is switched for
#testing the effectiveness of the domain adaptation
train_sample = fold[1]
test_sample = fold[0]
#These are the original copies to be copied over the augmented feature matrix
#Each sample contains the text and y labels from the data before it is put into the sklearn count vectorizer
train_x, train_y = train_sample
test_x, test_y = test_sample
train_y[train_y == 0] = 2
train_y[train_y == 1] = 3
test_y[test_y == 0] = 2
test_y[test_y == 1] = 3
#Get the bag of words representation of the small 20% target source data and transform the other 80%
#of the data.
train_x = data_loader.get_transformed_features(train_x, True, False, True)
test_x = data_loader.transform(test_x, True, True)
transformed_domains = []
#Transform the domains with respect to the training data
for domain in domains:
domain_x, domain_y = domain
transformed_domain_x = data_loader.transform(domain_x, True, True)
transformed_domain_x, domain_y = data_loader.underSample(transformed_domain_x, domain_y)
transformed_domains.append([transformed_domain_x, domain_y])
augmented_feature_matrix_train, augmented_y_train = feature_engineer.augmented_feature_matrix(transformed_domains,
[train_x, train_y])
augmented_feature_matrix_test, augmented_y_test = feature_engineer.augmented_feature_matrix(held_out_domain=[test_x, test_y],
train_or_test=False,
n_source_domains=len(transformed_domains))
augmented_y_test[augmented_y_test == 2] = 0
augmented_y_test[augmented_y_test == 3] = 1
#SVM with the augmented feature matrix for domain adaptation
svm_wda = SVM()
svm_wda.train(augmented_feature_matrix_train, augmented_y_train)
svm_wda.test(augmented_feature_matrix_test, augmented_y_test)
output = "\nSVM with domain adaptation metrics:"
csv_writer.writerow([output])
if print_output:
print(output)
print(svm_wda)
print("\n")
svm_wda_metrics_list.append(svm_wda.metrics)
classifier = NeuralNet(n_hidden_units=[250], output_size=4, batch_size=20, n_epochs=200, dropout=True,
activation_function='relu', learning_rate=.3, momentum=True, momentum_term=.5)
write_to_csv(svm_wda.metrics, csv_writer)
y_for_mlp = []
#Set up the x and y data for the MLP
for p, domain in enumerate(transformed_domains):
domain_x, domain_y = domain
domain_x = domain_x.todense()
y_for_mlp.append(domain_y)
if p == 0:
neural_net_x_train = domain_x
neural_net_y_train = domain_y
else:
neural_net_x_train = numpy.vstack((neural_net_x_train, domain_x))
neural_net_y_train = numpy.hstack((neural_net_y_train, domain_y))
neural_net_x_train = numpy.float_(neural_net_x_train)
classifier.train(neural_net_x_train, neural_net_y_train)
test_y[test_y == 2] = 0
test_y[test_y == 3] = 1
svm_y_train = neural_net_y_train
svm_y_train[svm_y_train == 2] = 0
svm_y_train[svm_y_train == 3] = 1
#SVM without the domain adaptation
svm = SVM()
svm.train(sparse.coo_matrix(neural_net_x_train), svm_y_train)
svm.test(test_x, test_y)
output = "\nSVM without domain adaptation"
if print_output:
print(output)
print(svm)
print("\n")
csv_writer.writerow([output])
svm_metrics_list.append(svm.metrics)
write_to_csv(svm.metrics, csv_writer)
#Transform the feature vectors of the held out data to the learned hidden layer features of the previous
#MLP trained with all n-1 datasets
perceptron_train_x = theano.shared(neural_net_x_train)
perceptron_test_x = theano.shared(test_x.todense())
transformed_perceptron_train_x = classifier.transfer_learned_weights(perceptron_train_x)
transformed_perceptron_test_x = classifier.transfer_learned_weights(perceptron_test_x)
modified_transformed_perceptron_train_x = numpy.hstack((transformed_perceptron_train_x,
neural_net_x_train))
modified_transformed_perceptron_test_x = numpy.hstack((transformed_perceptron_test_x,
test_x.todense()))
output = "\nSVM with BoW and transformed features"
csv_writer.writerow([output])
if print_output:
print(output)
svm_mlp_bow = SVM()
svm_mlp_bow.train(sparse.coo_matrix(modified_transformed_perceptron_train_x), svm_y_train)
svm_mlp_bow.test(sparse.coo_matrix(modified_transformed_perceptron_test_x), test_y)
write_to_csv(svm_mlp_bow.metrics, csv_writer)
if print_output:
print(svm_mlp_bow)
svm_bow_mlp_list.append(svm_mlp_bow.metrics)
output = "*********** End of fold {} ***********".format(n_fold)
if print_output:
print(output)
training_domains = copy.deepcopy(all_domains)
file_name = '{}/{}/fold_averages.csv'.format(os.getcwd(), folder_name)
file = open(file_name, 'w+')
csv_writer = csv.writer(file)
if print_output:
output = "----------------------------------------------------------------------------------------" \
"\nFold Scores\n " \
"SVM with domain adaptation"
print_write_output(output, svm_wda_metrics_list, all_domains_svm_wda_metrics_list, csv_writer)
output = "\nSVM without domain adaptation"
print_write_output(output, svm_metrics_list, all_domains_svm_metrics_list, csv_writer)
output = "SVM with BoW and transformed features"
print_write_output(output, svm_bow_mlp_list, all_domains_svm_bow_mlp_list, csv_writer)
file_name = '{}/output/all_fold_averages.csv'.format(os.getcwd())
file = open(file_name, 'w+')
csv_writer = csv.writer(file)
if print_output:
output = "*******************************************************************************************" \
"\nAll domain macro metric scores\n " \
"SVM with domain adaptation"
print_macro_scores("SVM with domain adaptation", all_domains_svm_wda_metrics_list, csv_writer)
output = "\nSVM without domain adaptation"
print_macro_scores(output, all_domains_svm_metrics_list, csv_writer)
output = "SVM with BoW and transformed features"
print_macro_scores(output, all_domains_svm_bow_mlp_list, csv_writer)
def print_write_output(title, classifier_metrics, all_domain_metrics, csv_writer):
csv_writer.writerow([title])
print(title)
metrics = calculate_fold_scores(classifier_metrics)
all_domain_metrics.append(metrics)
output_classifer = print_classifier_scores(metrics)
print(output_classifer)
write_to_csv(metrics, csv_writer)
def print_macro_scores(output, classifier_metrics, csv_writer):
csv_writer.writerow([output])
print(output)
metrics = calculate_fold_scores(classifier_metrics)
output_classifer = print_classifier_scores(metrics)
print(output_classifer)
write_to_csv(metrics, csv_writer)
def calculate_fold_scores(metrics_list):
keys = metrics_list[0].keys()
temp_metrics_dict = {}
for key in keys:
temp_metrics_dict.update({key: 0})
for metrics_dict in metrics_list:
for key in keys:
temp_metrics_dict[key] = temp_metrics_dict[key] + metrics_dict[key]
for key in temp_metrics_dict:
temp_metrics_dict[key] = temp_metrics_dict[key]/len(metrics_list)
return temp_metrics_dict
def print_classifier_scores(metrics):
string = ''
keys = metrics.keys()
for key in keys:
output = "{}: {}\n".format(key, metrics[key])
string+=output
return string
def write_to_csv(dict, csv_writer):
for key, value in dict.items():
csv_writer.writerow([key, value])
if __name__ == "__main__":
main()