-
Notifications
You must be signed in to change notification settings - Fork 0
/
pickle_to_csv_for_R.py
33 lines (27 loc) · 1.14 KB
/
pickle_to_csv_for_R.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
import pandas as pd
import numpy as np
import os
def to_csv(Item, relation_dict):
print(f'{Item}')
ItemPath = f'{Root}/attentions/{Item}'
Files = os.listdir(f'{ItemPath}')
for File in Files:
FilePath = f'{ItemPath}/{File}'
print(f'{FilePath}')
df = pd.read_pickle(FilePath)
for relation in relation_dict.keys():
layer = relation_dict[relation][0]
head = relation_dict[relation][1]
df[relation] = np.array([atts[(layer-1)][(head-1)] for atts in df.Attentions])
try:
df.to_csv(f"c:/R/bertmap/{Item}.csv")
except Exception as ex:
print(ex)
os.makedirs('c:/R/bertmap/')
print("Created a new directory")
df.to_csv(f"c:/R/bertmap/{Item}.csv")
Root = f'C:/python/bertmap' # path for root folder
Items = os.listdir(f'{Root}/attentions') # path where pickles for attention weights are saved
relation_dict = {'dobj': (8, 10), 'nsubj': (8, 2)} # {relation: (layer, head)
for Item in Items:
to_csv(Item, relation_dict)