forked from atnikos/cognitive_nlp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compos.py
115 lines (100 loc) · 3.75 KB
/
compos.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
import os
import pickle
import sys
import utils
import numpy as np
from scipy.stats import pearsonr
from scipy.stats import spearmanr
EXP1_PARTS = ["M01", "M02", "M03", "M04", "M05", "M06", "M07", "M08", "M09",
"M10", "M13", "M14", "M15", "M16", "M17", "P01"]
EXP2_PARTS = ["M02", "M04", "M07", "M08", "M09", "M14", "M15", "P01"]
EXP3_PARTS = ["M02", "M03", "M04", "M07", "M15", "P01"]
data_dir = sys.argv[1]
exp = sys.argv[2]
voxel_type = sys.argv[3]
assert exp in ['exp2', 'exp3']
assert voxel_type in ['average', 'pictures', 'sentences', 'wordclouds']
glove_embeddings = utils.load_pickle('./stimuli/word2vec.pkl')
if exp == 'exp2':
sentences_file = 'examples_384sentences.pkl'
EXP_PARTS = EXP2_PARTS
elif exp == 'exp3':
sentences_file = 'examples_243sentences.pkl'
EXP_PARTS = EXP3_PARTS
else:
print('Invalid experiment {}'.format(exp))
sys.exit(1)
participant_results = {}
for part in EXP_PARTS:
print("Participant ID : ", part)
weights_dir = os.path.join(data_dir, 'exp1', part, 'weights')
weights_file = os.path.join(weights_dir,
'weights_{}.npy'.format(voxel_type))
weights = np.load(weights_file)
sentence_embeddings_file = os.path.join(data_dir, exp, part, sentences_file)
sentence_embeddings = utils.load_pickle(sentence_embeddings_file)
result_list = []
avg_sp_voxels = 0
avg_pear_voxels = 0
num_elem = 0
for sentence, sentence_from_mri in sentence_embeddings.items():
sentence_from_glove = utils.extract_sent_embed(
sentence.strip(), glove_embeddings=glove_embeddings)
sentence_from_voxels = utils.extract_sent_embed1(
sentence.strip(), glove_embeddings=glove_embeddings, weights=weights)
# np.dot(weights, sentence_from_glove)[:-1]
sp_voxels = spearmanr(sentence_from_mri, sentence_from_voxels)
avg_sp_voxels += sp_voxels[0]
pear_voxels = pearsonr(sentence_from_mri, sentence_from_voxels)
pear_voxels += pear_voxels[0]
d = {
'sentence': sentence,
'sentence_mri': sentence_from_mri,
'sentence_glove': sentence_from_glove,
'sentence_voxels': sentence_from_voxels,
'spearman_voxels_mri': sp_voxels,
'pearson_voxels_mri': pear_voxels
}
num_elem += 1
result_list.append(d)
participant_results[part] = {
'all': result_list,
'avg_spearman_voxels_mri': avg_sp_voxels / float(num_elem),
'avg_pearson_voxels_mri': avg_pear_voxels / float(num_elem)
}
print('{} voxels - sentence Spearman {}'.format(
part,
participant_results[part]['avg_spearman_voxels_mri']))
print('{} voxels - sentence Pearson {}'.format(
part,
participant_results[part]['avg_pearson_voxels_mri']))
with open('{}_{}_composisionality_results'.format(exp, voxel_type), 'wb') as fd:
pickle.dump(participant_results, fd)
"""
data_dir = sys.argv[1]
for part in ["M01" ,"M02", "M03", "M04" ,"M05", "M06" ,"M07", "M08" ,"M09" ,"M10", "M13" ,"M14" ,"M15" ,"M16" ,"M17" ,"P01"]:
weights_lst = glob.glob(data_dir + '/' + part + '/weights/*')
print("Participant ID : ",part)
for wt in weights_lst:
weights_extracted=np.load(wt)
'''
load embeddings
use extract_sent_embed from utils.py
it also filters the sentence
'''
'''
construct representation of sentences
1.load weights
2.multiply weights with word vector
3.average embeddings or other measures i.e.weighted average or multiplicative
'''
'''
4.correlate with real
'''
'''
baseline (do the same with glove)
'''
'''
3-4 compositionality measures
'''
"""