Exemple #1
0
def test_tensor_converter_2():

    class _Trainer(BaseRunner):

        def __init__(self):
            super().__init__()
            self.non_blocking = False

        def predict(self, x_, y_):
            return x_, y_

    trainer = _Trainer()
    converter = TensorConverter()
    np_ = np.asarray([[1, 2, 3], [4, 5, 6]])
    pd_ = pd.DataFrame(np_)
    tensor_ = torch.Tensor(np_)  # noqa

    x, y = converter.input_proc(np_, np_[0], trainer=trainer)  # noqa
    assert isinstance(y, torch.Tensor)
    assert y.shape == (3, 1)
    assert torch.equal(y, tensor_[0].unsqueeze(-1))

    x, y = converter.input_proc(pd_, pd_.iloc[0], trainer=trainer)  # noqa
    assert isinstance(y, torch.Tensor)
    assert y.shape == (3, 1)
    assert torch.equal(y, tensor_[0].unsqueeze(-1))

    x, y = converter.input_proc(tensor_, tensor_[0], trainer=trainer)  # noqa
    assert isinstance(y, torch.Tensor)
    assert y.shape == (3,)
    assert torch.equal(y, tensor_[0])
Exemple #2
0
def test_tensor_converter_1():
    class _Trainer(BaseRunner):
        def __init__(self):
            super().__init__()
            self.non_blocking = False

        def predict(self, x_, y_):
            return x_, y_

    trainer = _Trainer()
    converter = TensorConverter()
    np_ = np.asarray([[1, 2, 3], [4, 5, 6]])
    pd_ = pd.DataFrame(np_)
    tensor_ = torch.Tensor(np_)

    x, y = converter.input_proc(np_, None, trainer=trainer)
    assert isinstance(x, torch.Tensor)
    assert x.shape == (2, 3)
    assert torch.equal(x, tensor_)
    assert y is None

    x, y = converter.input_proc(pd_, None, trainer=trainer)
    assert isinstance(x, torch.Tensor)
    assert x.shape == (2, 3)
    assert torch.equal(x, tensor_)
    assert y is None

    x, y = converter.input_proc(tensor_, None, trainer=trainer)
    assert isinstance(x, torch.Tensor)
    assert x.shape == (2, 3)
    assert torch.equal(x, tensor_)
    assert y is None

    x, y = converter.input_proc(np_, np_, trainer=trainer)
    assert isinstance(x, torch.Tensor)
    assert x.shape == (2, 3)
    assert torch.equal(x, tensor_)
    assert torch.equal(y, tensor_)

    x, y = converter.input_proc(pd_, pd_, trainer=trainer)
    assert isinstance(x, torch.Tensor)
    assert x.shape == (2, 3)
    assert torch.equal(x, tensor_)
    assert torch.equal(y, tensor_)

    x, y = converter.input_proc(tensor_, tensor_, trainer=trainer)
    assert isinstance(x, torch.Tensor)
    assert x.shape == (2, 3)
    assert torch.equal(x, tensor_)
    assert torch.equal(y, tensor_)
Exemple #3
0
def test_tensor_converter_1():

    class _Trainer(BaseRunner):

        def __init__(self):
            super().__init__()
            self.non_blocking = False

        def predict(self, x_, y_):  # noqa
            return x_, y_

    trainer = _Trainer()
    arr_1 = [1, 2, 3]
    np_1 = np.asarray(arr_1)
    se_1 = pd.Series(arr_1)
    pd_1 = pd.DataFrame(arr_1)
    np_ = np.asarray([arr_1, arr_1])
    pd_ = pd.DataFrame(np_)
    tensor_ = torch.Tensor(np_)

    # test auto reshape; #189
    converter = TensorConverter(auto_reshape=False)
    x, y = converter.input_proc(np_1, None, trainer=trainer)  # noqa
    assert isinstance(x, torch.Tensor)
    assert x.shape == (3,)

    x, y = converter.input_proc(se_1, None, trainer=trainer)  # noqa
    assert isinstance(x, torch.Tensor)
    assert x.shape == (3,)

    x, y = converter.input_proc(pd_1, None, trainer=trainer)  # noqa
    assert isinstance(x, torch.Tensor)
    assert x.shape == (3, 1)

    converter = TensorConverter()
    x, y = converter.input_proc(np_1, None, trainer=trainer)  # noqa
    assert isinstance(x, torch.Tensor)
    assert x.shape == (3, 1)

    x, y = converter.input_proc(se_1, None, trainer=trainer)  # noqa
    assert isinstance(x, torch.Tensor)
    assert x.shape == (3, 1)

    x, y = converter.input_proc(pd_1, None, trainer=trainer)  # noqa
    assert isinstance(x, torch.Tensor)
    assert x.shape == (3, 1)

    # normal tests
    x, y = converter.input_proc(np_, None, trainer=trainer)  # noqa
    assert isinstance(x, torch.Tensor)
    assert x.shape == (2, 3)
    assert torch.equal(x, tensor_)
    assert y is None

    x, y = converter.input_proc(pd_, None, trainer=trainer)  # noqa
    assert isinstance(x, torch.Tensor)
    assert x.shape == (2, 3)
    assert torch.equal(x, tensor_)
    assert y is None

    x, y = converter.input_proc(tensor_, None, trainer=trainer)  # noqa
    assert isinstance(x, torch.Tensor)
    assert x.shape == (2, 3)
    assert torch.equal(x, tensor_)
    assert y is None

    x, y = converter.input_proc(np_, np_, trainer=trainer)  # noqa
    assert isinstance(x, torch.Tensor)
    assert x.shape == (2, 3)
    assert torch.equal(x, tensor_)
    assert torch.equal(y, tensor_)

    x, y = converter.input_proc(pd_, pd_, trainer=trainer)  # noqa
    assert isinstance(x, torch.Tensor)
    assert x.shape == (2, 3)
    assert torch.equal(x, tensor_)
    assert torch.equal(y, tensor_)

    x, y = converter.input_proc(tensor_, tensor_, trainer=trainer)  # noqa
    assert isinstance(x, torch.Tensor)
    assert x.shape == (2, 3)
    assert torch.equal(x, tensor_)
    assert torch.equal(y, tensor_)

    converter = TensorConverter(x_dtype=torch.long)
    x, y = converter.input_proc((np_, np_), np_, trainer=trainer)  # noqa
    assert isinstance(x, tuple)
    assert len(x) == 2
    assert x[0].dtype == torch.long
    assert x[1].dtype == torch.long

    converter = TensorConverter(x_dtype=(torch.long, torch.float32), y_dtype=torch.long)
    x, y = converter.input_proc((np_, np_), np_, trainer=trainer)  # noqa
    assert isinstance(x, tuple)
    assert len(x) == 2
    assert x[0].dtype == torch.long
    assert x[1].dtype == torch.float32
    assert y.dtype == torch.long

    converter = TensorConverter(x_dtype=(torch.long, torch.float32))
    x, y = converter.input_proc((pd_, pd_), pd_, trainer=trainer)  # noqa
    assert isinstance(x, tuple)
    assert len(x) == 2
    assert x[0].dtype == torch.long
    assert x[1].dtype == torch.float32

    # for tensor input, dtype change will never be executed
    converter = TensorConverter(x_dtype=(torch.long, torch.long))
    x, y = converter.input_proc((tensor_, tensor_), tensor_, trainer=trainer)  # noqa
    assert isinstance(x, tuple)
    assert len(x) == 2
    assert x[0].dtype == torch.float32
    assert x[1].dtype == torch.float32