forked from espnet/espnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_initialization.py
104 lines (92 loc) · 3.11 KB
/
test_initialization.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# coding: utf-8
# Copyright 2017 Shigeki Karita
# Apache 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
import argparse
import numpy
import os
import pytest
import random
args = argparse.Namespace(
elayers=4,
subsample="1_2_2_1_1",
etype="vggblstmp",
eunits=320,
eprojs=320,
dlayers=2,
dunits=300,
atype="location",
aconv_chans=10,
aconv_filts=100,
mtlalpha=0.5,
lsm_type="",
lsm_weight=0.0,
adim=320,
dropout_rate=0.0,
beam_size=3,
penalty=0.5,
maxlenratio=1.0,
minlenratio=0.0,
ctc_weight=0.2,
verbose=True,
char_list=[u"あ", u"い", u"う", u"え", u"お"],
outdir=None,
seed=1
)
def test_lecun_init_torch():
torch = pytest.importorskip("torch")
nseed = args.seed
random.seed(nseed)
torch.manual_seed(nseed)
numpy.random.seed(nseed)
os.environ["CHAINER_SEED"] = str(nseed)
import e2e_asr_attctc_th as m
model = m.Loss(m.E2E(40, 5, args), 0.5)
b = model.predictor.ctc.ctc_lo.bias.data.numpy()
assert numpy.all(b == 0.0)
w = model.predictor.ctc.ctc_lo.weight.data.numpy()
numpy.testing.assert_allclose(w.mean(), 0.0, 1e-2, 1e-2)
numpy.testing.assert_allclose(w.var(), 1.0 / w.shape[1], 1e-2, 1e-2)
for name, p in model.named_parameters():
print(name)
data = p.data.numpy()
if "embed" in name:
numpy.testing.assert_allclose(data.mean(), 0.0, 5e-2, 5e-2)
numpy.testing.assert_allclose(data.var(), 1.0, 5e-2, 5e-2)
elif "predictor.dec.decoder.0.bias_ih" in name:
assert data.sum() == data.size // 4
elif "predictor.dec.decoder.1.bias_ih" in name:
assert data.sum() == data.size // 4
elif data.ndim == 1:
assert numpy.all(data == 0.0)
else:
numpy.testing.assert_allclose(data.mean(), 0.0, 5e-2, 5e-2)
numpy.testing.assert_allclose(
data.var(), 1.0 / numpy.prod(data.shape[1:]), 5e-2, 5e-2)
def test_lecun_init_chainer():
nseed = args.seed
random.seed(nseed)
numpy.random.seed(nseed)
os.environ["CHAINER_SEED"] = str(nseed)
import e2e_asr_attctc as m
model = m.Loss(m.E2E(40, 5, args), 0.5)
b = model.predictor.ctc.ctc_lo.b.data
assert numpy.all(b == 0.0)
w = model.predictor.ctc.ctc_lo.W.data
numpy.testing.assert_allclose(w.mean(), 0.0, 1e-2, 1e-2)
numpy.testing.assert_allclose(w.var(), 1.0 / w.shape[1], 1e-2, 1e-2)
for name, p in model.namedparams():
print(name)
data = p.data
if "lstm0/upward/b" in name:
assert data.sum() == data.size // 4
elif "lstm1/upward/b" in name:
assert data.sum() == data.size // 4
elif "embed" in name:
numpy.testing.assert_allclose(data.mean(), 0.0, 5e-2, 5e-2)
numpy.testing.assert_allclose(data.var(), 1.0, 5e-2, 5e-2)
elif data.ndim == 1:
assert numpy.all(data == 0.0)
else:
numpy.testing.assert_allclose(data.mean(), 0.0, 5e-2, 5e-2)
numpy.testing.assert_allclose(
data.var(), 1.0 / numpy.prod(data.shape[1:]), 5e-2, 5e-2)