def test_validator_init_bad_objectives():
    with pytest.raises(TypeError,
                       match='Argument: objectives must' + ' be a list.'):
        Validator(model, dataloader, metrics, 'objectives')
    with pytest.raises(TypeError,
                       match='All elements of argument: objectives' +
                       ' must be of type Loss.'):
        Validator(model, dataloader, metrics, ['objective', VAELoss()])
test_output_data_path = os.path.join(dir_path, 'movielens_small_test_test.npy')
products_data_path = os.path.join(dir_path, 'movielens_products_data.npy')

data_handler = AEDataHandler('MovieLensSmall', train_data_path,
                             validation_input_data_path,
                             validation_output_data_path, test_input_data_path,
                             test_output_data_path)

input_dim = data_handler.get_input_dim()
output_dim = data_handler.get_output_dim()

products_data_np = np.load(products_data_path)
products_data_torch = torch.tensor(products_data_np,
                                   dtype=torch.float32).to(device)

# create model
model = MultiVAE(params='yaml_files/params_multi_VAE_training.yaml')

correctness_loss = VAELoss()
revenue_loss = VAELoss(weighted_vector=products_data_torch)
losses = [correctness_loss, revenue_loss]

recallAtK = RecallAtK(k=10)
revenueAtK = RevenueAtK(k=10, revenue=products_data_np)
validation_metrics = [recallAtK, revenueAtK]

trainer = Trainer(data_handler, model, losses, validation_metrics,
                  save_to_path)
trainer.train()
print(trainer.pareto_manager._pareto_front)
from loss.vae_loss import VAELoss
from loss.mse_loss import MSELoss
import pytest
import torch

# variables
y_pred = torch.tensor([1., 0.]).view(1, 2)
y_true = torch.tensor([1., 1.]).view(1, 2)
y_pred_error = torch.tensor([1, 1, 0, 0, 1]).view(1, 5)
mean = torch.tensor([0., 0.]).view(1, 2)
logvar = torch.tensor([0., 0.]).view(1, 2)
price_vector = torch.tensor([1., 2.31]).view(1, 2)
price_vector_error = torch.tensor([1., 2.31, 1.]).view(1, 3)

# vae loss for price
vae_loss_price = VAELoss(weighted_vector=price_vector)
# vae loss for relevance
vae_loss_relevance = VAELoss()
# vae loss with an explicit error
vae_loss_price_error = VAELoss(weighted_vector=price_vector_error)
# output of vae model
output_model = (y_pred, mean, logvar)
# output of vae model with an explicit error
output_model_error = (y_pred_error, mean, logvar)

# length of prediction and groundtruth are the same


def test_length_pred_gt():
    with pytest.raises(ValueError):
        vae_loss_price.compute_loss(y_pred, output_model_error)
np.save(test_input_data_path, np.random.rand(2000, 8936).astype('float32'))
np.save(test_output_data_path, np.random.rand(2000, 8936).astype('float32'))

# Variables
dataset = MamoDataset(np.load(test_input_data_path),
                      np.load(test_output_data_path))

model = MultiVAE(params='yaml_files/params_multi_VAE.yaml')
model.initialize_model()
dataloader = DataLoader(dataset,
                        batch_size=data_info['batch_size'],
                        shuffle=True,
                        drop_last=True)
metrics = [RecallAtK(10)]
objectives = [VAELoss()]

obj_results = [0.4, 0.5, 0.7]
alphas = [0.5, 0.2, 0.3]
max_normalization = [1, 0.5, 2]


# A Validator object cannot be created without a model.
def test_validator_init_no_model():
    with pytest.raises(TypeError, match='Argument: model must be set.'):
        Validator(None, dataloader, metrics, objectives)


# A Validator object cannot be created without a dataloader.
def test_validator_init_no_dataloader():
    with pytest.raises(TypeError, match='Argument: dataloader must be set.'):
Пример #5
0
test_input_data_path = os.path.join(dir_path, 'movielens_small_test_input.npy')
test_output_data_path = os.path.join(dir_path, 'movielens_small_test_test.npy')
products_data_path = os.path.join(dir_path, 'movielens_products_data.npy')

data_handler = AEDataHandler('MovieLensSmall', train_data_path,
                             validation_input_data_path,
                             validation_output_data_path, test_input_data_path,
                             test_output_data_path)

input_dim = data_handler.get_input_dim()
output_dim = data_handler.get_output_dim()

products_data_np = np.load(products_data_path)
products_data_torch = torch.tensor(products_data_np,
                                   dtype=torch.float32).to(device)

# create model
model = MultiVAE(params='yaml_files/params_multi_VAE_training.yaml')

# correctnes loss
loss = VAELoss()

recallAtK = RecallAtK(k=10)
revenueAtK = RevenueAtK(k=10, revenue=products_data_np)
validation_metrics = [recallAtK, revenueAtK]

trainer = SingleObjectiveTrainer(data_handler, model, loss, validation_metrics,
                                 save_to_path)
trainer.train()
print(trainer.pareto_manager._pareto_front)