-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathtest_huggingface.py
71 lines (51 loc) · 1.73 KB
/
test_huggingface.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
import pytest
import datasets
from continuum.scenarios.hf import HuggingFaceFellowship, HuggingFaceContinual
class _Dataset:
def __init__(self, rows=None):
if rows:
self.rows = rows
else:
self.rows = [
{'genre': 'sf', 'b': i}
for i in range(10)
] + [
{'genre': 'fantasy', 'b': i}
for i in range(10)
] + [
{'genre': 'bio', 'b': i}
for i in range(10)
] + [
{'genre': 'history', 'b': i}
for i in range(10)
]
def __len__(self):
return len(self.rows)
def __getitem__(self, column_name):
return [row[column_name] for row in self.rows]
def filter(self, lbd):
return _Dataset(list(filter(lbd, self.rows)))
def mock_incremental_dataset(*args, **kwargs):
return _Dataset()
@pytest.mark.parametrize("increment", [1, 2, 4])
def test_hf_incremental(mocker, increment):
mocker.patch.object(datasets, "load_dataset", new=mock_incremental_dataset)
scenario = HuggingFaceContinual(
"foo", split_field="genre", increment=increment
)
assert scenario.nb_classes == 4
assert scenario.nb_samples == 40
assert len(scenario) == 4 // increment
for taskset in scenario:
classes = set([row["genre"] for row in taskset.rows])
assert len(classes) == increment
def test_hf_fellowship():
scenario = HuggingFaceFellowship(
[_Dataset(), _Dataset()]
)
with pytest.raises(NotImplementedError):
scenario.nb_classes
assert scenario.nb_samples == 80
assert len(scenario) == 2
for taskset in scenario:
assert len(taskset) == 40