-
Notifications
You must be signed in to change notification settings - Fork 29
/
datasets.py
67 lines (49 loc) · 2.47 KB
/
datasets.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
from torchtext import data
from torchtext import datasets
def make_sst(batch_size, device=-1, fine_grained=False, vectors=None):
TEXT = data.Field(include_lengths=True, lower=True)
LABEL = data.LabelField()
filter_pred = lambda ex: ex.label != 'neutral' if not fine_grained else lambda ex: True
train, val, test = datasets.SST.splits(TEXT, LABEL,
fine_grained=fine_grained,
train_subtrees=False,
filter_pred=filter_pred
)
TEXT.build_vocab(train, test, val, vectors=vectors)
LABEL.build_vocab(train, test, val)
train_iter, val_iter, test_iter = data.BucketIterator.splits(
(train, val, test), batch_size=batch_size, device=device, repeat=False)
return (train_iter, val_iter, test_iter), TEXT, LABEL
def make_imdb(batch_size, device=-1, vectors=None):
TEXT = data.Field(include_lengths=True, lower=True)
LABEL = data.LabelField()
train, test = datasets.IMDB.splits(TEXT, LABEL)
TEXT.build_vocab(train, test, val, vectors=vectors, max_size=30000)
LABEL.build_vocab(train, test, val)
train_iter, test_iter = data.BucketIterator.splits(
(train, test), batch_size=batch_size, device=device, repeat=False)
return (train_iter, test_iter), TEXT, LABEL
def make_trec(batch_size, device=-1, vectors=None):
TEXT = data.Field(include_lengths=True, lower=True)
LABEL = data.LabelField()
train, test = datasets.TREC.splits(TEXT, LABEL)
TEXT.build_vocab(train, test, val, vectors=vectors)
LABEL.build_vocab(train, test, val)
train_iter, test_iter = data.BucketIterator.splits(
(train, test), batch_size=batch_size, device=device, repeat=False)
return (train_iter, test_iter), TEXT, LABEL
dataset_map = {
'SST' : make_sst,
'IMDB' : make_imdb,
'TREC' : make_trec
}
if __name__ == '__main__':
(tr, val, te), T, L = make_sst(20)
print("[SST] vocab: {} labels: {}".format(len(T.vocab), len(L.vocab)))
print("[SST] train: {} val: {} test {}".format(len(tr.dataset), len(val.dataset), len(te.dataset)))
(tr, te), T, L = make_imdb(20)
print("[IMDB] vocab: {} labels: {}".format(len(T.vocab), len(L.vocab)))
print("[IMDB] train: {} test {}".format(len(tr.dataset), len(te.dataset)))
(tr, te), T, L = make_trec(20)
print("[TREC] vocab: {} labels: {}".format(len(T.vocab), len(L.vocab)))
print("[TREC] train: {} test {}".format(len(tr.dataset), len(te.dataset)))