deepcarskit.utils¶
deepcarskit.utils.logger¶
- class deepcarskit.utils.logger.RemoveColorFilter(name='')¶
Bases:
logging.Filter
- filter(record)¶
Determine if the specified record is to be logged.
Returns True if the record should be logged, or False otherwise. If deemed appropriate, the record may be modified in-place.
- deepcarskit.utils.logger.init_logger(config)¶
A logger that can show a message on standard output and write it into the file named filename simultaneously. All the message that you want to log MUST be str.
- Args:
config (Config): An instance object of Config, used to record parameter information.
- Example:
>>> logger = logging.getLogger(config) >>> logger.debug(train_state) >>> logger.info(train_result)
- deepcarskit.utils.logger.set_color(log, color, highlight=True)¶
deepcarskit.utils.utils¶
- deepcarskit.utils.utils.get_model(model_name)¶
Automatically select model class based on model name
- Args:
model_name (str): model name
- Returns:
Recommender: model class
- deepcarskit.utils.utils.get_trainer(model_type, model_name)¶
Automatically select trainer class based on model type and model name
- Args:
model_type (ModelType): model type model_name (str): model name
- Returns:
Trainer: trainer class
- class deepcarskit.utils.Enum(value)¶
Bases:
object
Generic enumeration.
Derive from this class to define new enumerations.
- name¶
The name of the Enum member.
- value¶
The value of the Enum member.
- class deepcarskit.utils.EvaluatorType(value)¶
Bases:
enum.Enum
Type for evaluation metrics.
RANKING
: Ranking-based metrics like NDCG, Recall, etc.VALUE
: Value-based metrics like AUC, etc.
- RANKING = 1¶
- VALUE = 2¶
- class deepcarskit.utils.FeatureSource(value)¶
Bases:
enum.Enum
Source of features.
INTERACTION
: Features from.inter
(other thanuser_id
anditem_id
).USER
: Features from.user
(other thanuser_id
).ITEM
: Features from.item
(other thanitem_id
).USER_ID
:user_id
feature ininter_feat
anduser_feat
.ITEM_ID
:item_id
feature ininter_feat
anditem_feat
.KG
: Features from.kg
.NET
: Features from.net
.
- INTERACTION = 'inter'¶
- ITEM = 'item'¶
- ITEM_ID = 'item_id'¶
- KG = 'kg'¶
- NET = 'net'¶
- USER = 'user'¶
- USER_ID = 'user_id'¶
- class deepcarskit.utils.FeatureType(value)¶
Bases:
enum.Enum
Type of features.
TOKEN
: Token features like user_id and item_id.FLOAT
: Float features like rating and timestamp.TOKEN_SEQ
: Token sequence features like review.FLOAT_SEQ
: Float sequence features like pretrained vector.
- FLOAT = 'float'¶
- FLOAT_SEQ = 'float_seq'¶
- TOKEN = 'token'¶
- TOKEN_SEQ = 'token_seq'¶
- class deepcarskit.utils.InputType(value)¶
Bases:
enum.Enum
Type of Models’ input.
POINTWISE
: Point-wise input, likeuid, iid, label
.PAIRWISE
: Pair-wise input, likeuid, pos_iid, neg_iid
.
- LISTWISE = 3¶
- PAIRWISE = 2¶
- POINTWISE = 1¶
- class deepcarskit.utils.KGDataLoaderState(value)¶
Bases:
enum.Enum
States for Knowledge-based DataLoader.
RSKG
: Return both knowledge graph information and user-item interaction information.RS
: Only return the user-item interaction.KG
: Only return the triplets with negative examples in a knowledge graph.
- KG = 3¶
- RS = 2¶
- RSKG = 1¶
- class deepcarskit.utils.ModelType(value)¶
Bases:
enum.Enum
Type of models.
GENERAL
: General RecommendationSEQUENTIAL
: Sequential RecommendationCONTEXT
: Context-aware RecommendationKNOWLEDGE
: Knowledge-based Recommendation
- CONTEXT = 3¶
- DECISIONTREE = 6¶
- GENERAL = 1¶
- KNOWLEDGE = 4¶
- SEQUENTIAL = 2¶
- TRADITIONAL = 5¶
- deepcarskit.utils.calculate_valid_score(valid_result, valid_metric=None)¶
return valid score from valid result
- Args:
valid_result (dict): valid result valid_metric (str, optional): the selected metric in valid result for valid score
- Returns:
float: valid score
- deepcarskit.utils.dict2str(result_dict)¶
convert result dict to str
- Args:
result_dict (dict): result dict
- Returns:
str: result str
- deepcarskit.utils.early_stopping(value, best, cur_step, max_step, bigger=True)¶
validation-based early stopping
- Args:
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:
tuple: - 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
- deepcarskit.utils.ensure_dir(dir_path)¶
Make sure the directory exists, if it does not exist, create it
- Args:
dir_path (str): directory path
- deepcarskit.utils.get_gpu_usage(device=None)¶
Return the reserved memory and total memory of given device in a string. Args:
device: cuda.device. It is the device that the model run on.
- Returns:
str: it contains the info about reserved memory and total memory of given device.
- deepcarskit.utils.get_local_time()¶
Get current time
- Returns:
str: current time
- deepcarskit.utils.get_model(model_name)¶
Automatically select model class based on model name
- Args:
model_name (str): model name
- Returns:
Recommender: model class
- deepcarskit.utils.get_tensorboard(logger)¶
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.
- Args:
- 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:
SummaryWriter: it will write out events and summaries to the event file.
- deepcarskit.utils.get_trainer(model_type, model_name)¶
Automatically select trainer class based on model type and model name
- Args:
model_type (ModelType): model type model_name (str): model name
- Returns:
Trainer: trainer class
- deepcarskit.utils.init_logger(config)¶
A logger that can show a message on standard output and write it into the file named filename simultaneously. All the message that you want to log MUST be str.
- Args:
config (Config): An instance object of Config, used to record parameter information.
- Example:
>>> logger = logging.getLogger(config) >>> logger.debug(train_state) >>> logger.info(train_result)
- deepcarskit.utils.init_seed(seed, reproducibility)¶
init random seed for random functions in numpy, torch, cuda and cudnn
- Args:
seed (int): random seed reproducibility (bool): Whether to require reproducibility
- deepcarskit.utils.set_color(log, color, highlight=True)¶