forked from speechbrain/speechbrain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_hpopt.py
More file actions
67 lines (48 loc) · 1.73 KB
/
Copy pathtest_hpopt.py
File metadata and controls
67 lines (48 loc) · 1.73 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
57
58
59
60
61
62
63
64
65
66
67
import pytest
def test_hpopt_generic():
import json
from io import StringIO
from speechbrain.utils import hpopt as hp
output = StringIO()
reporter = hp.GenericHyperparameterOptimizationReporter(
objective_key="per", output=output
)
result = {"train_loss": 0.9, "valid_loss": 1.2, "per": 0.10}
reporter.report_objective(result)
output.seek(0)
output_result = json.load(output)
assert output_result["train_loss"] == pytest.approx(0.9)
assert output_result["valid_loss"] == pytest.approx(1.2)
assert output_result["per"] == pytest.approx(0.10)
assert output_result["objective"] == pytest.approx(0.10)
def test_hpopt_orion():
from speechbrain.utils import hpopt as hp
results = {}
class MockOrion:
def report_objective(self, value):
results["value"] = value
mock_orion = MockOrion()
reporter = hp.OrionHyperparameterOptimizationReporter(
objective_key="valid_loss"
)
reporter.orion_client = mock_orion
result = {"train_loss": 0.9, "valid_loss": 1.2, "per": 0.10}
reporter.report_objective(result)
assert results["value"] == pytest.approx(1.2)
def test_hpopt_context():
import json
from io import StringIO
from speechbrain.utils import hpopt as hp
output = StringIO()
reporter = hp.GenericHyperparameterOptimizationReporter(
objective_key="per", output=output
)
with hp.hyperparameter_optimization() as hp_ctx:
hp_ctx.reporter = reporter
result = {"per": 10, "loss": 1.2}
hp.report_result(result)
result = {"per": 3, "loss": 1.3}
hp.report_result(result)
output.seek(0)
output_result = json.load(output)
assert output_result["per"] == 3