forked from pyannote/pyannote-audio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprobe_util_test.py
78 lines (66 loc) · 1.47 KB
/
probe_util_test.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
import torch
import torch.nn as nn
from pyannote.audio.utils.probe import probe
class Trunk(nn.Module):
def __init__(self):
super().__init__()
self.layer1 = nn.Linear(1, 2)
self.layer2 = nn.Linear(2, 3)
self.layer3 = nn.Linear(3, 4)
def forward(self, x):
return self.layer3(self.layer2(self.layer1(x)))
def test_probe_dict():
trunk = Trunk()
probe(trunk, {"probe1": "layer1"})
out = trunk(
torch.ones(
1,
)
)
assert isinstance(out, dict)
assert len(out.keys()) == 1
assert isinstance(out["probe1"], torch.Tensor)
def test_probe_output():
trunk = Trunk()
probe(trunk, {"probe1": "layer3"})
out = trunk(
torch.ones(
1,
)
)
out = out["probe1"]
tout = trunk.layer3(
trunk.layer2(
trunk.layer1(
torch.ones(
1,
)
)
)
)
assert torch.equal(tout, out)
def test_probe_revert():
trunk = Trunk()
revert = probe(trunk, {"probe1": "layer3"})
out = trunk(
torch.ones(
1,
)
)
assert isinstance(out, dict)
revert()
out = trunk(
torch.ones(
1,
)
)
assert isinstance(out, torch.Tensor)
def test_probe_array():
trunk = Trunk()
probe(trunk, ["layer3"])
out = trunk(
torch.ones(
1,
)
)
assert isinstance(out, dict)