forked from A-Rain/EmpDialogue_RecEC
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_utils.py
366 lines (321 loc) · 14.8 KB
/
data_utils.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
from typing import List, Optional, Tuple, Iterator
import numpy as np
import torch
import pandas as pd
import csv
import texar.torch as tx
from texar.torch.hyperparams import HParams
import random
import json
Example = Tuple[np.ndarray, np.ndarray]
from nltk.tokenize import word_tokenize
import math
import numpy as np
from modules import EmotionVocab
def get_lr_multiplier(step: int, warmup_steps: int) -> float:
r"""Calculate the learning rate multiplier given current step and the number
of warm-up steps. The learning rate schedule follows a linear warm-up and
square-root decay.
"""
multiplier = (min(1.0, step / warmup_steps) *
(1 / math.sqrt(max(step, warmup_steps))))
return multiplier
class CustomBatchingStrategy(tx.data.BatchingStrategy[Example]):
r"""Create dynamically-sized batches for paired text data so that the total
number of source and target tokens (including padding) inside each batch is
constrained.
Args:
max_tokens (int): The maximum number of source or target tokens inside
each batch.
"""
max_src_len: int
max_tgt_len: int
cur_batch_size: int
def __init__(self, max_tokens: int):
self.max_tokens = max_tokens
def reset_batch(self) -> None:
self.max_src_len = 0
self.max_tgt_len = 0
self.cur_batch_size = 0
def add_example(self, ex: Example) -> bool:
max_src_len = max(self.max_src_len, len(ex['src_ids']))
max_tgt_len = max(self.max_tgt_len, len(ex['tgt_ids']))
if ((self.cur_batch_size + 1) *
max(max_src_len, max_tgt_len) > self.max_tokens):
return False
self.max_src_len = max_src_len
self.max_tgt_len = max_tgt_len
self.cur_batch_size += 1
return True
class TextLineDataSource(tx.data.TextLineDataSource):
def __iter__(self) -> Iterator[List[str]]:
for path in self._file_paths:
with self._open_file(path) as f:
for line in f:
example = json.loads(line.strip())
for i in range(len(example['utters'])//2):
j = i*2
src_length = sum([len(u) for u in example['utters'][:j+1]])
if src_length > 512 or len(example['utters'])<2:
print(f"long sentence: {src_length}")
continue
yield {'utters': example['utters'][:j+2], 'emotion': example['context'], 'emotion_cause': example['emotion_cause']}
class EvalDataSource(tx.data.TextLineDataSource):
def __iter__(self) -> Iterator[List[str]]:
for path in self._file_paths:
with self._open_file(path) as f:
for line in f:
example = json.loads(line.strip())
if len(example['utters']) % 2 != 0:
example['utters'] = example['utters'][:-1]
src_length = sum([len(u) for u in example['utters']])
if src_length > 512 or len(example['utters']) < 2:
print(f"long sentence: {src_length}")
continue
yield {'utters': example['utters'], 'emotion': example['context'], 'emotion_cause': example['emotion_cause']}
class TrainData(tx.data.DatasetBase[Example, Example]):
def __init__(self, hparams=None,
device: Optional[torch.device] = None):
self._hparams = HParams(hparams, self.default_hparams())
data_source = TextLineDataSource(
self._hparams.dataset.files,
compression_type=self._hparams.dataset.compression_type)
self._vocab = tx.data.Vocab(self._hparams.dataset.vocab_file)
self._emotion_vocab = EmotionVocab(self._hparams.dataset.emotion_file)
super().__init__(data_source, hparams, device=device)
@staticmethod
def default_hparams():
return {
**tx.data.DatasetBase.default_hparams(),
'dataset': { 'files': 'data.txt',
'compression_type':None,
'vocab_file':None,
'emotion_file':None},
}
def process(self, raw_example):
utters = raw_example['utters']
emotion = raw_example['emotion']
emotion_cause = raw_example['emotion_cause']
src = []
cause_ids = [0.0]
user_ids = [0]
for idx,u in enumerate(utters[:-1]):
# if idx in emotion_cause or idx == len(utters)-2:
# cause_ids += [1.0] * (len(u)+1)
# else:
# cause_ids += [0.0] * (len(u)+1)
# if idx % 2 == 0:
# user_ids += [1] * (len(u)+1)
# else:
# user_ids += [2] * (len(u)+1)
# src.append(' '.join(u + ['<SEP>']))
if idx in emotion_cause or idx == len(utters)-2:
cause_ids += [1.0] * (len(u)+1)
else:
cause_ids += [0.0] * (len(u)+1)
if idx % 2 == 0:
user_ids += [1] * (len(u)+1)
else:
user_ids += [2] * (len(u)+1)
src.append(' '.join(u+['<SEP>']))
# src = [' '.join(u + ['<SEP>']) for u in utters[:-1]]
src = ' '.join(['<CLS>'] + src)
tgt = ' '.join(['<BOS>']+utters[-1]+['<EOS>'])
return {
"src_text": src,
"src_ids": self._vocab.map_tokens_to_ids_py(src.split(' ')),
"tgt_text": tgt,
"tgt_ids": self._vocab.map_tokens_to_ids_py(tgt.split(' ')),
"emotion_text": emotion,
"emotion_id": [self._emotion_vocab.token_to_id_map_py[emotion]],
"cause_ids": np.array(cause_ids),
"user_ids": np.array(user_ids)
}
def collate(self, examples: List[Example]) -> tx.data.Batch:
src_text = [ex["src_text"] for ex in examples]
src_ids, src_lengths = tx.data.padded_batch(
[ex["src_ids"] for ex in examples], pad_value=self._vocab.pad_token_id)
tgt_text = [ex["tgt_text"] for ex in examples]
tgt_ids, tgt_lengths = tx.data.padded_batch(
[ex["tgt_ids"] for ex in examples], pad_value=self._vocab.pad_token_id)
cause_ids, cause_lengths = tx.data.padded_batch(
[ex["cause_ids"] for ex in examples], pad_value=0.0)
user_ids, user_lengths = tx.data.padded_batch(
[ex["user_ids"] for ex in examples], pad_value=0)
emotion_text = [ex["emotion_text"] for ex in examples]
emotion_id = np.array([ex["emotion_id"] for ex in examples])
return tx.data.Batch(
len(examples),
src_text=src_text,
src_text_ids=torch.from_numpy(src_ids),
src_lengths=torch.tensor(src_lengths),
tgt_text=tgt_text,
tgt_text_ids=torch.from_numpy(tgt_ids),
tgt_lengths=torch.tensor(tgt_lengths),
emotion_text=emotion_text,
emotion_id=torch.from_numpy(emotion_id),
cause_ids=torch.from_numpy(cause_ids),
user_ids=torch.from_numpy(user_ids),
)
@property
def vocab(self):
r"""The vocabulary, an instance of :class:`~texar.torch.data.Vocab`.
"""
return self._vocab
class CustomData(tx.data.DatasetBase[Example, Example]):
def __init__(self, hparams=None,
device: Optional[torch.device] = None):
self._hparams = HParams(hparams, self.default_hparams())
data_source = self._load_csv(self._hparams.dataset.files)
self._vocab = tx.data.Vocab(self._hparams.dataset.vocab_file)
self._emotion_vocab = EmotionVocab(self._hparams.dataset.emotion_file)
super().__init__(data_source, hparams, device=device)
@staticmethod
def default_hparams():
return {
**tx.data.DatasetBase.default_hparams(),
'dataset': { 'files': 'data.txt',
'compression_type':None,
'vocab_file':None,
'emotion_file':None},
}
def _load_csv(self, file_path):
# Read the CSV and create a data source
df = pd.read_csv(file_path)
examples = []
for _, row in df.iterrows():
seeker_post = row['seeker_post']
response_post = row['response_post']
# Store the raw text
examples.append({"utters": [seeker_post, response_post], "emotion": "unknown", "emotion_cause": []})
return examples
def process(self, raw_example):
utters = raw_example['utters']
src = []
user_ids = [0] # Initializing with a default value for compatibility
# Preprocess utterances with special tokens
for idx, u in enumerate(utters):
# Simple alternating scheme for user IDs if needed
user_ids += [(idx % 2) + 1] * (len(u.split()) + 1)
src.append(' '.join(u.split() + ['<SEP>']))
src = ' '.join(['<CLS>'] + src[:-1]) # Exclude the last <SEP> token
tgt = ' '.join(['<BOS>'] + utters[-1].split() + ['<EOS>'])
return {
"src_text": src,
"src_ids": self._vocab.map_tokens_to_ids_py(src.split()),
"tgt_text": tgt,
"tgt_ids": self._vocab.map_tokens_to_ids_py(tgt.split()),
# Emotion and cause information might be included if available
"emotion_text": "unknown", # Placeholder
"emotion_id": [0], # Placeholder; assuming 'unknown' maps to 0
"cause_ids": np.array([0.0]), # Placeholder
"user_ids": np.array(user_ids[:-1]) # Exclude the last user ID addition for the <SEP> token
}
def collate(self, examples: List[Example]) -> tx.data.Batch:
src_texts = [ex["src_text"] for ex in examples]
src_ids, src_lengths = tx.data.padded_batch(
[ex["src_ids"] for ex in examples], pad_value=self._vocab.pad_token_id)
tgt_texts = [ex["tgt_text"] for ex in examples]
tgt_ids, tgt_lengths = tx.data.padded_batch(
[ex["tgt_ids"] for ex in examples], pad_value=self._vocab.pad_token_id)
user_ids, _ = tx.data.padded_batch(
[ex["user_ids"] for ex in examples], pad_value=0)
# Assuming placeholders for emotion and cause, these would be handled here if real data was available
# emotion_ids = torch.tensor([ex["emotion_id"] for ex in examples])
return tx.data.Batch(
batch_size=len(examples),
src_text=src_texts,
src_text_ids=torch.LongTensor(src_ids),
src_lengths=torch.LongTensor(src_lengths),
tgt_text=tgt_texts,
tgt_text_ids=torch.LongTensor(tgt_ids),
tgt_lengths=torch.LongTensor(tgt_lengths),
emotion_text=None,
emotion_id=None,
cause_ids=None,
user_ids=torch.LongTensor(user_ids),
# Additional placeholders if needed
)
class EvalData(tx.data.DatasetBase[Example, Example]):
def __init__(self, hparams=None,
device: Optional[torch.device] = None):
self._hparams = HParams(hparams, self.default_hparams())
data_source = EvalDataSource(
self._hparams.dataset.files,
compression_type=self._hparams.dataset.compression_type)
self._vocab = tx.data.Vocab(self._hparams.dataset.vocab_file)
self._emotion_vocab = EmotionVocab(self._hparams.dataset.emotion_file)
super().__init__(data_source, hparams, device=device)
@staticmethod
def default_hparams():
return {
**tx.data.DatasetBase.default_hparams(),
'dataset': { 'files': 'data.txt',
'compression_type':None,
'vocab_file':None,
'emotion_file':None},
}
def process(self, raw_example):
utters = raw_example['utters']
emotion = raw_example['emotion']
emotion_cause = raw_example['emotion_cause']
src = []
cause_ids = [1]
for idx,u in enumerate(utters[:-1]):
if idx in emotion_cause:
cause_ids += [2] * (len(u)+1)
else:
cause_ids += [1] * (len(u)+1)
src.append(' '.join(u + ['<SEP>']))
src = [' '.join(u + ['<SEP>']) for u in utters[:-1]]
src = ' '.join(['<CLS>'] + src)
tgt = ' '.join(['<BOS>']+utters[-1]+['<EOS>'])
return {
"src_text": src,
"src_ids": self._vocab.map_tokens_to_ids_py(src.split(' ')),
"tgt_text": tgt,
"tgt_ids": self._vocab.map_tokens_to_ids_py(tgt.split(' ')),
"emotion_text": emotion,
"emotion_id": [self._emotion_vocab.token_to_id_map_py[emotion]],
"cause_ids": np.array(cause_ids)
}
def collate(self, examples: List[Example]) -> tx.data.Batch:
src_text = [ex["src_text"] for ex in examples]
src_ids, src_lengths = tx.data.padded_batch(
[ex["src_ids"] for ex in examples], pad_value=self._vocab.pad_token_id)
tgt_text = [ex["tgt_text"] for ex in examples]
tgt_ids, tgt_lengths = tx.data.padded_batch(
[ex["tgt_ids"] for ex in examples], pad_value=self._vocab.pad_token_id)
cause_ids, cause_lengths = tx.data.padded_batch(
[ex["cause_ids"] for ex in examples], pad_value=0.0)
emotion_text = [ex["emotion_text"] for ex in examples]
emotion_id = np.array([ex["emotion_id"] for ex in examples])
return tx.data.Batch(
len(examples),
src_text=src_text,
src_text_ids=torch.from_numpy(src_ids),
src_lengths=torch.tensor(src_lengths),
tgt_text=tgt_text,
tgt_text_ids=torch.from_numpy(tgt_ids),
tgt_lengths=torch.tensor(tgt_lengths),
emotion_text=emotion_text,
emotion_id=torch.from_numpy(emotion_id),
cause_ids=torch.from_numpy(cause_ids),
)
@property
def vocab(self):
r"""The vocabulary, an instance of :class:`~texar.torch.data.Vocab`.
"""
return self._vocab
if __name__ == "__main__":
hparams={
'dataset': { 'files': 'train.json','vocab_file':'vocab.txt',},
'batch_size': 10,
# 'lazy_strategy': 'all',
# 'num_parallel_calls': 10,
'shuffle': False
}
data = TrainData(hparams)
iterator = tx.data.DataIterator(data)
for batch in iterator:
print(batch)