-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeta_learner_ffs.py
172 lines (155 loc) · 7.46 KB
/
meta_learner_ffs.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
import numpy as np
import analysis_utils
from sklearn.model_selection import LeaveOneOut
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import ElasticNet
from scipy.stats import pearsonr, spearmanr
import lang2vec.lang2vec as l2v
from sklearn import preprocessing
from itertools import combinations, chain
import pandas as pd
from sklearn.metrics import mean_absolute_error
def powerset(iterable):
"""
powerset([1,2,3]) --> (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)
not a true powerset as empty is not returned
:param iterable:
:return:
"""
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(1, len(s)+1))
def _get_features(task, features, model, similarity_strategy=None):
"""
Returns a feature vector for features given a certain task, model and similarity strategy
:param task:
:param features:
:param model:
:param similarity_strategy:
:return:
"""
X = []
langs = analysis_utils.get_langs_for_task(task)
for feature in features:
if feature != "size":
# this is a nested array
X_feature = analysis_utils.load_lang2vec_vectors(task=task, features=feature)
if X_feature is None:
#continue
return None
if similarity_strategy != "-":
# We start with similarities to english
X_feature = [[sim] for sim in analysis_utils.compute_similarities_of_lang_vecs(X_feature, strategy=similarity_strategy)]
elif feature == "size" and model == "xlmr":
# this is an array, we put it in a list
X_feature = [[size] for size in analysis_utils.xlmr_input_corpus_sizes(langs)]
elif feature == "size" and model == "mbert":
X_feature = [[size] for size in analysis_utils.mbert_input_corpus_sizes(langs)]
else:
raise ValueError()
# we now have a feature vector for a single feature or feature set
if len(X) == 0:
X = np.array(X_feature)
else:
X = np.concatenate((X,np.array(X_feature)), axis=1)
if len(X) == 0:
return None
return np.array(X, dtype=float)
def _get_labels(task, model, sampling_strategy, k, aggregation_strategy):
if aggregation_strategy == "plain":
scores = analysis_utils.get_experiment_scores(task, model, sampling_strategy, k)
elif aggregation_strategy == "delta" and len(k) == 2:
scores_a = analysis_utils.get_experiment_scores(task, model, sampling_strategy, k[0])
scores_b = analysis_utils.get_experiment_scores(task, model, sampling_strategy, k[1])
scores = [scores_a[i]-scores_b[i] for i in range(len(scores_a))]
return scores
def _run_regression(task, features, model, sampling_strategy, k, similarity_strategy, aggregation_strategy):
# TODO: Maybe I need to do feature normalization
X = _get_features(task, features, model, similarity_strategy)
if X is not None:
X = preprocessing.scale(X)
y = np.array(_get_labels(task, model, sampling_strategy, k, aggregation_strategy))
assert len(X) == len(y)
# set up leave-one-out cv
all_preds = []
all_ys = []
coeffs = []
for train_index, test_index in LeaveOneOut().split(X):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
regressor = LinearRegression() # ElasticNet()
regressor.fit(X_train, y_train) # training the algorithm
coeffs.append(regressor.coef_)
y_pred = regressor.predict(X_test)
all_preds.append(y_pred)
all_ys.append(y_test)
all_preds = np.array(all_preds).flatten()
all_ys = np.array(all_ys).flatten()
avg_coeffs = np.average(np.array(coeffs), axis=0)
return pearsonr(all_ys, all_preds), spearmanr(all_ys, all_preds), avg_coeffs, mean_absolute_error(all_preds, all_ys)
else:
return None
def output_result(result, task, features, model, sampling_strategy, k, aggregation_strategy, similarity_strategy):
if result is not None:
pearson = result[0][0]
pearsonp = result[0][1]
spearman = result[1][0]
spearmanp = result[1][1]
coeffs = result[2]
mae = result[3]
if similarity_strategy == "to_en":
print("\t".join(
[aggregation_strategy, task, model, sampling_strategy, str(k), str(features), similarity_strategy,
str(pearson), str(pearsonp), str(spearman), str(spearmanp), str(coeffs), str(mae)]))
else:
print("\t".join(
[aggregation_strategy, task, model, sampling_strategy, str(k), str(features), similarity_strategy,
str(pearson), str(pearsonp), str(spearman), str(spearmanp), str(mae)]))
def run_regression(task, features, model, sampling_strategy, k, aggregation_strategy, similarity_strategy):
result = _run_regression(task, features, model, sampling_strategy, k, similarity_strategy, aggregation_strategy)
return result
#print("\t".join(
# [aggregation_strategy, task, model, sampling_strategy, str(k), str(features), similarity_strategy, "", "", "", ""]))
def run_regression_ffs(task, features, model, sampling_strategy, k, aggregation_strategy, similarity_strategy):
initial_features = features
best_features = []
best_result = None
old_max_pearson = 0.0
while (len(initial_features)>0):
remaining_features = list(set(initial_features)-set(best_features))
new_pearson = pd.Series(index=remaining_features)
results = pd.Series(index=remaining_features)
for new_column in remaining_features:
result = run_regression(task, best_features + [new_column],
model,
sampling_strategy,
k,
aggregation_strategy,
similarity_strategy)
if result is not None:
new_pearson[new_column] = result[0][0]
results[new_column] = result
else:
continue
max_pearson = new_pearson.max()
if(max_pearson > old_max_pearson):
best_features.append(new_pearson.idxmax())
old_max_pearson = max_pearson
best_result = results[new_pearson.idxmax()]
else:
break
print("The best features are " + str(best_features))
output_result(best_result, task, features, model, sampling_strategy, k, aggregation_strategy, similarity_strategy)
return best_features
if __name__ == "__main__":
#'syntax_wals', 'syntax_sswl', 'syntax_knn',
import warnings
warnings.filterwarnings("ignore")
all_features = list(l2v.FEATURE_SETS) + ["size"]
for similarity_strat in ["to_en"]:
run_regression_ffs("pos", all_features, "mbert", "RANDOM", 0, "plain", similarity_strat)
#for comb in powerset(all_features):
# result = run_regression("xnli", comb, "xlmr", "k_first", 0, "plain", similarity_strat) #"-",
# for ner its RANDOM
#result = run_correlation_analysis("dep", feature, "mbert?", "LONGEST", 0, "plain", "to_en")
#result = run_correlation_analysis("xquad", "sizes", "mbert", "k_first", 6, "plain", "-")
#result = run_correlation_analysis("ner", "sizes", "mbert", "RANDOM", 0, "plain", "-")