-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
56 lines (47 loc) · 2.05 KB
/
Copy pathutils.py
File metadata and controls
56 lines (47 loc) · 2.05 KB
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
from torchtext.data import Field
from torchtext.data import Iterator,BucketIterator
from torchtext.vocab import Vectors
import torch
from torchtext.data import TabularDataset
def generate_data(config):
## 不同字段的操作定义
tokenizer = lambda x: [one for one in x]
TEXT = Field(sequential=True, tokenize=tokenizer,fix_length=config.sen_max_length)##截断句长直接影响acc!!!
LABEL = Field(sequential=False, use_vocab=False) ## 如果标签是数值型的话
datafields = [("context", TEXT), ("label_id", LABEL)] ## TEXT field, LABEL field
test_field = [("context", TEXT), ("label_id", LABEL)]
train_file, valid_file = TabularDataset.splits(
path=config.data_ori,
train=config.train_path,
validation=config.valid_path,
format="csv",
skip_header=True,
fields=datafields
)
test_file = TabularDataset(
path=config.data_ori+config.test_path,
format="csv",
skip_header=True,
fields=test_field
)
## 构建词典
vectors=Vectors(name=config.data_ori+config.embedding_path,cache="./")
TEXT.build_vocab(train_file,max_size=config.vocab_maxsize, min_freq=config.vocab_minfreq, vectors=vectors)
TEXT.vocab.set_vectors(vectors.stoi, vectors.vectors, vectors.dim)
train_iter, val_iter = BucketIterator.splits(
(train_file, valid_file),
batch_sizes=(config.batch_size, config.batch_size),
device=config.device,
sort_key=lambda x: len(x.context),
sort_within_batch=True,
# 当要使用pack_padded_sequence时,需要将sort_within_batch设置为True,同时会将paded sequence 转为PackedSequence对象
repeat=False
)
test_iter = Iterator(test_file, batch_size=config.batch_size, device=config.device, sort=False, sort_within_batch=False, repeat=False)
return train_iter, val_iter, test_iter, TEXT
if __name__=="__main__":
print("test data")
#train_iter, valid_iter, test_iter=generate_data(file_path)
#a=list(train_iter)
#print(a[0])
#print(a[0].context)