def test_zero_length_target(etype): pytest.importorskip('torch') args = make_arg(etype=etype) import logging logging.basicConfig( level=logging.DEBUG, format='%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s' ) import e2e_asr_attctc as ch import e2e_asr_attctc_th as th ch_model = ch.E2E(40, 5, args) ch_model.cleargrads() th_model = th.E2E(40, 5, args) data = [("aaa", dict(feat=numpy.random.randn(200, 40).astype(numpy.float32), tokenid="1")), ("bbb", dict(feat=numpy.random.randn(100, 40).astype(numpy.float32), tokenid="")), ("cc", dict(feat=numpy.random.randn(100, 40).astype(numpy.float32), tokenid="1 2"))] ch_ctc, ch_att, ch_acc = ch_model(data) th_ctc, th_att, th_acc = th_model(data)
def test_loss_and_ctc_grad(etype): pytest.importorskip('torch') args = make_arg(etype=etype) import logging logging.basicConfig( level=logging.DEBUG, format='%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s') import e2e_asr_attctc as ch import e2e_asr_attctc_th as th ch_model = ch.E2E(40, 5, args) ch_model.cleargrads() th_model = th.E2E(40, 5, args) const = 1e-4 init_torch_weight_const(th_model, const) init_chainer_weight_const(ch_model, const) out_data = "1 2 3 4" data = [ ("aaa", dict(feat=numpy.random.randn(200, 40).astype( numpy.float32), tokenid=out_data)), ("bbb", dict(feat=numpy.random.randn(100, 40).astype( numpy.float32), tokenid=out_data)), ("cc", dict(feat=numpy.random.randn(100, 40).astype( numpy.float32), tokenid=out_data)) ] ch_ctc, ch_att, ch_acc = ch_model(data) th_ctc, th_att, th_acc = th_model(data) # test masking ch_ench = ch_model.att.pre_compute_enc_h.data th_ench = th_model.att.pre_compute_enc_h.data.numpy() numpy.testing.assert_equal(ch_ench == 0.0, th_ench == 0.0) # test loss with constant weights (1.0) and bias (0.0) except for foget-bias (1.0) numpy.testing.assert_allclose(ch_ctc.data, th_ctc.data.numpy()) numpy.testing.assert_allclose(ch_att.data, th_att.data.numpy()) # test ctc grads ch_ctc.backward() th_ctc.backward() numpy.testing.assert_allclose(ch_model.ctc.ctc_lo.W.grad, th_model.ctc.ctc_lo.weight.grad.data.numpy(), 1e-7, 1e-8) numpy.testing.assert_allclose(ch_model.ctc.ctc_lo.b.grad, th_model.ctc.ctc_lo.bias.grad.data.numpy(), 1e-5, 1e-6) # test cross-entropy grads ch_model.cleargrads() th_model.zero_grad() ch_ctc, ch_att, ch_acc = ch_model(data) th_ctc, th_att, th_acc = th_model(data) ch_att.backward() th_att.backward() numpy.testing.assert_allclose(ch_model.dec.output.W.grad, th_model.dec.output.weight.grad.data.numpy(), 1e-7, 1e-8) numpy.testing.assert_allclose(ch_model.dec.output.b.grad, th_model.dec.output.bias.grad.data.numpy(), 1e-5, 1e-6)
def _propagate(ctc_type): args = make_arg(ctc_type=ctc_type) numpy.random.seed(0) model = ch.E2E(40, 5, args) ch_ctc, _, _ = model(data) ch_ctc.backward() W_grad = model.ctc.ctc_lo.W.grad b_grad = model.ctc.ctc_lo.b.grad return ch_ctc.data, W_grad, b_grad