-
Notifications
You must be signed in to change notification settings - Fork 18
/
dataset.py
163 lines (149 loc) · 6 KB
/
dataset.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
import torch
from torch.utils.data import Dataset
from torch.nn.utils.rnn import pad_sequence
import pickle, pandas as pd
import json
import numpy as np
import random
from pandas import DataFrame
class IEMOCAPDataset(Dataset):
def __init__(self, dataset_name = 'IEMOCAP', split = 'train', speaker_vocab=None, label_vocab=None, args = None, tokenizer = None):
self.speaker_vocab = speaker_vocab
self.label_vocab = label_vocab
self.args = args
self.data = self.read(dataset_name, split, tokenizer)
print(len(self.data))
self.len = len(self.data)
def read(self, dataset_name, split, tokenizer):
with open('../data/%s/%s_data_roberta.json.feature'%(dataset_name, split), encoding='utf-8') as f:
raw_data = json.load(f)
# process dialogue
dialogs = []
# raw_data = sorted(raw_data, key=lambda x:len(x))
for d in raw_data:
# if len(d) < 5 or len(d) > 6:
# continue
utterances = []
labels = []
speakers = []
features = []
for i,u in enumerate(d):
utterances.append(u['text'])
labels.append(self.label_vocab['stoi'][u['label']] if 'label' in u.keys() else -1)
speakers.append(self.speaker_vocab['stoi'][u['speaker']])
features.append(u['cls'])
dialogs.append({
'utterances': utterances,
'labels': labels,
'speakers':speakers,
'features': features
})
random.shuffle(dialogs)
return dialogs
def __getitem__(self, index):
'''
:param index:
:return:
feature,
label
speaker
length
text
'''
return torch.FloatTensor(self.data[index]['features']), \
torch.LongTensor(self.data[index]['labels']),\
self.data[index]['speakers'], \
len(self.data[index]['labels']), \
self.data[index]['utterances']
def __len__(self):
return self.len
def get_adj(self, speakers, max_dialog_len):
'''
get adj matrix
:param speakers: (B, N)
:param max_dialog_len:
:return:
adj: (B, N, N) adj[:,i,:] means the direct predecessors of node i
'''
adj = []
for speaker in speakers:
a = torch.zeros(max_dialog_len, max_dialog_len)
for i,s in enumerate(speaker):
get_local_pred = False
get_global_pred = False
for j in range(i - 1, -1, -1):
if speaker[j] == s and not get_local_pred:
get_local_pred = True
a[i,j] = 1
elif speaker[j] != s and not get_global_pred:
get_global_pred = True
a[i,j] = 1
if get_global_pred and get_local_pred:
break
adj.append(a)
return torch.stack(adj)
def get_adj_v1(self, speakers, max_dialog_len):
'''
get adj matrix
:param speakers: (B, N)
:param max_dialog_len:
:return:
adj: (B, N, N) adj[:,i,:] means the direct predecessors of node i
'''
adj = []
for speaker in speakers:
a = torch.zeros(max_dialog_len, max_dialog_len)
for i,s in enumerate(speaker):
cnt = 0
for j in range(i - 1, -1, -1):
a[i,j] = 1
if speaker[j] == s:
cnt += 1
if cnt==self.args.windowp:
break
adj.append(a)
return torch.stack(adj)
def get_s_mask(self, speakers, max_dialog_len):
'''
:param speakers:
:param max_dialog_len:
:return:
s_mask: (B, N, N) s_mask[:,i,:] means the speaker informations for predecessors of node i, where 1 denotes the same speaker, 0 denotes the different speaker
s_mask_onehot (B, N, N, 2) onehot emcoding of s_mask
'''
s_mask = []
s_mask_onehot = []
for speaker in speakers:
s = torch.zeros(max_dialog_len, max_dialog_len, dtype = torch.long)
s_onehot = torch.zeros(max_dialog_len, max_dialog_len, 2)
for i in range(len(speaker)):
for j in range(len(speaker)):
if speaker[i] == speaker[j]:
s[i,j] = 1
s_onehot[i,j,1] = 1
else:
s_onehot[i,j,0] = 1
s_mask.append(s)
s_mask_onehot.append(s_onehot)
return torch.stack(s_mask), torch.stack(s_mask_onehot)
def collate_fn(self, data):
'''
:param data:
features, labels, speakers, length, utterances
:return:
features: (B, N, D) padded
labels: (B, N) padded
adj: (B, N, N) adj[:,i,:] means the direct predecessors of node i
s_mask: (B, N, N) s_mask[:,i,:] means the speaker informations for predecessors of node i, where 1 denotes the same speaker, 0 denotes the different speaker
lengths: (B, )
utterances: not a tensor
'''
max_dialog_len = max([d[3] for d in data])
feaures = pad_sequence([d[0] for d in data], batch_first = True) # (B, N, D)
labels = pad_sequence([d[1] for d in data], batch_first = True, padding_value = -1) # (B, N )
adj = self.get_adj_v1([d[2] for d in data], max_dialog_len)
s_mask, s_mask_onehot = self.get_s_mask([d[2] for d in data], max_dialog_len)
lengths = torch.LongTensor([d[3] for d in data])
speakers = pad_sequence([torch.LongTensor(d[2]) for d in data], batch_first = True, padding_value = -1)
utterances = [d[4] for d in data]
return feaures, labels, adj,s_mask, s_mask_onehot,lengths, speakers, utterances