Exemplo n.º 1
0
def test_to_variable():
    x = torch.Tensor([0.0])
    var_x = to_variable(x)
    assert isinstance(var_x, Variable)

    x = (torch.Tensor([0.0]), torch.Tensor([0.0]))
    var_x = to_variable(x)
    assert isinstance(var_x, list) and isinstance(var_x[0], Variable) and isinstance(var_x[1], Variable)

    x = {'a': torch.Tensor([0.0]), 'b': torch.Tensor([0.0])}
    var_x = to_variable(x)
    assert isinstance(var_x, dict) and isinstance(var_x['a'], Variable) and isinstance(var_x['b'], Variable)

    with pytest.raises(TypeError):
        to_variable(12345)
Exemplo n.º 2
0
 def _prepare_batch(batch):
     x, y = batch
     x = to_variable(x, cuda=cuda)
     y = to_variable(y, cuda=cuda)
     return x, y
Exemplo n.º 3
0
 def _prepare_batch(batch):
     x, y = batch
     x = to_variable(x, cuda=cuda, volatile=True)
     y = to_variable(y, cuda=cuda, volatile=True)
     return x, y
Exemplo n.º 4
0
def _prepare_batch(batch, cuda, volatile=False):
    x, y = batch
    x = to_variable(x, cuda=cuda, volatile=volatile)
    y = to_variable(y, cuda=cuda, volatile=volatile)
    return x, y