コード例 #1
0
ファイル: test_hred.py プロジェクト: yushu-liu/quick-nlp
def test_hred_training_parameters(model, hredmodel):
    *xs, y = next(iter(hredmodel.trn_dl))
    xs = V(xs)
    y = V(y)
    optimizer = Adam(model.parameters())
    output = model(*xs)
    optimizer.zero_grad()
    loss = decoder_loss(input=output[0], target=y, pad_idx=hredmodel.pad_idx)
    loss.backward()
    model_parameters = get_trainable_parameters(model)
    grad_flow_parameters = get_trainable_parameters(model, grad=True)
    assert set(model_parameters) == set(grad_flow_parameters)
コード例 #2
0
ファイル: test_cvae.py プロジェクト: zhangjiekui/quick-nlp
def test_cvae_training_parameters(model, hredmodel, tchebycheff, sigmoid):
    *xs, y = next(iter(hredmodel.trn_dl))
    xs = V(xs)
    y = V(y)
    optimizer = Adam(model.parameters())
    output = model(*xs)
    optimizer.zero_grad()
    cvae_loss = get_cvae_loss(pad_idx=hredmodel.pad_idx,
                              tchebycheff=tchebycheff,
                              sigmoid=sigmoid)
    loss = cvae_loss(input=output[0], target=y)
    loss.backward()
    model_parameters = get_trainable_parameters(model)
    grad_flow_parameters = get_trainable_parameters(model, grad=True)
    assert set(model_parameters) == set(grad_flow_parameters)
コード例 #3
0
def test_model(s2smodel):
    ntoken = [s2smodel.nt[name] for name in s2smodel.trn_dl.source_names]
    model = Transformer(ntoken=ntoken,
                        max_tokens=5,
                        eos_token=s2smodel.eos_idx)
    model = to_gpu(model)
    *xs, y = next(iter(s2smodel.trn_dl))
    xs = V(xs)
    y = V(y)
    optimizer = Adam(model.parameters())
    output = model(*xs)
    optimizer.zero_grad()
    loss = decoder_loss(input=output[0], target=y, pad_idx=s2smodel.pad_idx)
    loss.backward()
    model_parameters = get_trainable_parameters(model)
    grad_flow_parameters = get_trainable_parameters(model, grad=True)
    assert set(model_parameters) == set(grad_flow_parameters)