Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_trainer_2(data):
trainer = Trainer()
with pytest.raises(RuntimeError, match='no model for training'):
trainer.fit(*data[1])
with pytest.raises(TypeError, match='parameter `m` must be a instance of '):
trainer.model = {}
trainer.model = data[0]
assert isinstance(trainer.model, torch.nn.Module)
with pytest.raises(RuntimeError, match='no loss function for training'):
trainer.fit(*data[1])
trainer.loss_func = MSELoss()
assert trainer.loss_type == 'train_mse_loss'
assert trainer.loss_func.__class__ == MSELoss
with pytest.raises(RuntimeError, match='no optimizer for training'):
trainer.fit(*data[1])
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 and are mutually exclusive'):
trainer.predict(*data[1], dataset='not none')
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)
def test_trainer_1(data):
trainer = Trainer()
assert trainer.device == torch.device('cpu')
assert trainer.model is None
assert trainer.optimizer is None
assert trainer.lr_scheduler is None
assert trainer.x_val is None
assert trainer.y_val is None
assert trainer.validate_dataset is None
assert trainer._init_states is None
assert trainer._optimizer_state is None
assert trainer.total_epochs == 0
assert trainer.total_iterations == 0
assert trainer.training_info is None
assert trainer.loss_type is None
assert trainer.loss_func is None
trainer = Trainer(optimizer=Adam(),
def test_trainer_fit_4(data):
model = deepcopy(data[0])
trainer = Trainer(model=model,
optimizer=Adam(),
loss_func=MSELoss(),
clip_grad=ClipValue(0.1),
lr_scheduler=ReduceLROnPlateau(),
epochs=10)
count = 1
for i in trainer(*data[1]):
assert isinstance(i, dict)
assert i['i_epoch'] == count
if count == 3:
trainer.early_stop('stop')
count += 1
assert trainer.total_epochs == 3
assert trainer._early_stopping == (True, 'stop')
assert trainer.device == torch.device('cpu')
assert trainer.model is None
assert trainer.optimizer is None
assert trainer.lr_scheduler is None
assert trainer.x_val is None
assert trainer.y_val is None
assert trainer.validate_dataset is None
assert trainer._init_states is None
assert trainer._optimizer_state is None
assert trainer.total_epochs == 0
assert trainer.total_iterations == 0
assert trainer.training_info is None
assert trainer.loss_type is None
assert trainer.loss_func is None
trainer = Trainer(optimizer=Adam(),
loss_func=MSELoss(),
lr_scheduler=ExponentialLR(gamma=0.99),
clip_grad=ClipValue(clip_value=0.1))
assert isinstance(trainer._scheduler, ExponentialLR)
assert isinstance(trainer._optim, Adam)
assert isinstance(trainer.clip_grad, ClipValue)
assert isinstance(trainer.loss_func, MSELoss)