recbole.utils.utils

recbole.utils.utils.calculate_valid_score(valid_result, valid_metric=None)[source]

return valid score from valid result

Parameters
  • valid_result (dict) – valid result

  • valid_metric (str, optional) – the selected metric in valid result for valid score

Returns

valid score

Return type

float

recbole.utils.utils.dict2str(result_dict)[source]

convert result dict to str

Parameters

result_dict (dict) – result dict

Returns

result str

Return type

str

recbole.utils.utils.early_stopping(value, best, cur_step, max_step, bigger=True)[source]

validation-based early stopping

Parameters
  • value (float) – current result

  • best (float) – best result

  • cur_step (int) – the number of consecutive steps that did not exceed the best result

  • max_step (int) – threshold steps for stopping

  • bigger (bool, optional) – whether the bigger the better

Returns

  • float, best result after this step

  • int, the number of consecutive steps that did not exceed the best result after this step

  • bool, whether to stop

  • bool, whether to update

Return type

tuple

recbole.utils.utils.ensure_dir(dir_path)[source]

Make sure the directory exists, if it does not exist, create it

Parameters

dir_path (str) – directory path

recbole.utils.utils.get_gpu_usage(device=None)[source]

Return the reserved memory and total memory of given device in a string. :param device: cuda.device. It is the device that the model run on.

Returns

it contains the info about reserved memory and total memory of given device.

Return type

str

recbole.utils.utils.get_local_time()[source]

Get current time

Returns

current time

Return type

str

recbole.utils.utils.get_model(model_name)[source]

Automatically select model class based on model name

Parameters

model_name (str) – model name

Returns

model class

Return type

Recommender

recbole.utils.utils.get_tensorboard(logger)[source]

Creates a SummaryWriter of Tensorboard that can log PyTorch models and metrics into a directory for visualization within the TensorBoard UI. For the convenience of the user, the naming rule of the SummaryWriter’s log_dir is the same as the logger.

Parameters

logger – its output filename is used to name the SummaryWriter’s log_dir. If the filename is not available, we will name the log_dir according to the current time.

Returns

it will write out events and summaries to the event file.

Return type

SummaryWriter

recbole.utils.utils.get_trainer(model_type, model_name)[source]

Automatically select trainer class based on model type and model name

Parameters
  • model_type (ModelType) – model type

  • model_name (str) – model name

Returns

trainer class

Return type

Trainer

recbole.utils.utils.init_seed(seed, reproducibility)[source]

init random seed for random functions in numpy, torch, cuda and cudnn

Parameters
  • seed (int) – random seed

  • reproducibility (bool) – Whether to require reproducibility