Exemple #1
0
def test_trainer_prediction_1(data):
    model = deepcopy(data[0])
    trainer = Trainer(model=model,
                      optimizer=Adam(lr=0.1),
                      loss_func=MSELoss(),
                      epochs=200)
    trainer.extend(TensorConverter())
    trainer.fit(*data[1], *data[1])

    trainer = Trainer(model=model).extend(TensorConverter())
    y_p = trainer.predict(data[1][0])
    assert np.any(np.not_equal(y_p, data[1][1].numpy()))
    assert np.allclose(y_p, data[1][1].numpy(), rtol=0, atol=0.2)

    y_p, y_t = trainer.predict(*data[1])
    assert np.any(np.not_equal(y_p, y_t))
    assert np.allclose(y_p, y_t, rtol=0, atol=0.2)

    val_set = DataLoader(TensorDataset(*data[1]), batch_size=50)
    y_p, y_t = trainer.predict(dataset=val_set)
    assert np.any(np.not_equal(y_p, y_t))
    assert np.allclose(y_p, y_t, rtol=0, atol=0.2)

    with pytest.raises(
            RuntimeError,
            match='parameters <x_in> and <dataset> are mutually exclusive'):
        trainer.predict(*data[1], dataset='not none')
Exemple #2
0
def test_trainer_prediction_2():
    model = _Net(n_feature=2, n_hidden=10, n_output=2)

    n_data = np.ones((100, 2))
    x0 = np.random.normal(2 * n_data, 1)
    y0 = np.zeros(100)
    x1 = np.random.normal(-2 * n_data, 1)
    y1 = np.ones(100)

    x = np.vstack((x0, x1))
    y = np.concatenate((y0, y1))
    s = np.arange(x.shape[0])
    np.random.shuffle(s)
    x, y = x[s], y[s]

    trainer = Trainer(model=model, optimizer=Adam(lr=0.1), loss_func=CrossEntropyLoss(), epochs=200)
    trainer.extend(TensorConverter(x_dtype=torch.float32, y_dtype=torch.long, argmax=True))
    trainer.fit(x, y)

    y_p, y_t = trainer.predict(x, y)
    assert y_p.shape == (200,)
    assert np.all(y_p == y_t)

    # trainer.reset()
    val_set = DataLoader(ArrayDataset(x, y, dtypes=(torch.float, torch.long)), batch_size=20)
    trainer.extend(TensorConverter(x_dtype=torch.float32, y_dtype=torch.long, auto_reshape=False))
    y_p, y_t = trainer.predict(dataset=val_set)
    assert y_p.shape == (200, 2)

    y_p = np.argmax(y_p, 1)
    assert np.all(y_p == y_t)
Exemple #3
0
def test_trainer_fit_3(data):
    model = deepcopy(data[0])
    trainer = Trainer(model=model,
                      optimizer=Adam(),
                      loss_func=MSELoss(),
                      epochs=5)
    trainer.fit(*data[1])
    assert len(trainer.checkpoints.keys()) == 0

    trainer.reset()
    assert trainer.total_iterations == 0
    assert trainer.total_epochs == 0
    assert len(trainer.get_checkpoint()) == 0

    trainer.fit(*data[1], checkpoint=True)
    assert len(trainer.get_checkpoint()) == 5
    assert isinstance(trainer.get_checkpoint(2), trainer.checkpoint_tuple)
    assert isinstance(trainer.get_checkpoint('cp_2'), trainer.checkpoint_tuple)

    with pytest.raises(TypeError, match='parameter <cp> must be str or int'):
        trainer.get_checkpoint([])

    trainer.reset(to=3, remove_checkpoints=False)
    assert len(trainer.get_checkpoint()) == 5
    assert isinstance(trainer.get_checkpoint(2), trainer.checkpoint_tuple)
    assert isinstance(trainer.get_checkpoint('cp_2'), trainer.checkpoint_tuple)

    trainer.reset(to='cp_3')
    assert trainer.total_iterations == 0
    assert trainer.total_epochs == 0
    assert len(trainer.get_checkpoint()) == 0

    with pytest.raises(
            TypeError,
            match='parameter <to> must be torch.nnModule, int, or str'):
        trainer.reset(to=[])

    # todo: need a real testing
    trainer.fit(*data[1], checkpoint=True)
    trainer.predict(*data[1], checkpoint=3)

    trainer.reset()
    trainer.fit(*data[1], checkpoint=lambda i: (True, f'new:{i}'))
    assert len(trainer.get_checkpoint()) == 5
    assert trainer.get_checkpoint() == list([f'new:{i + 1}' for i in range(5)])