Metric Interfaces - API Reference¶
warprec.evaluation.metrics.base_metric.BaseMetric
¶
Bases: Metric, ABC
The base definition of a metric using Torchmetrics.
Source code in warprec/evaluation/metrics/base_metric.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
components
property
¶
The required components to compute the metric.
name
property
¶
The name of the metric.
binary_relevance(target)
classmethod
¶
Compute the binary relevance tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Tensor
|
The target tensor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The binary relevance tensor. |
Source code in warprec/evaluation/metrics/base_metric.py
compute_area_stats(preds, target, num_items, k=None)
¶
Computes the Area per user and the Number of Positives per user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
Tensor
|
Predictions tensor. |
required |
target
|
Tensor
|
Binary relevance tensor. |
required |
num_items
|
int
|
Total number of items. |
required |
k
|
Optional[int]
|
Cutoff for top-k evaluation. If None, considers all items. |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
Tuple[Tensor, Tensor]: - Tensor: Area per user. - Tensor: Number of positives per user. |
Source code in warprec/evaluation/metrics/base_metric.py
compute_head_tail(item_interactions, pop_ratio=0.8)
¶
Compute popularity as tensors of the short head and long tail.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_interactions
|
Tensor
|
The counts for item interactions in training set. |
required |
pop_ratio
|
float
|
The percentile considered popular. |
0.8
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
Tuple[Tensor, Tensor]: - Tensor: The tensor containing indices of short head items. - Tensor: The tensor containing indices of long tail items. |
Source code in warprec/evaluation/metrics/base_metric.py
compute_novelty_profile(item_interactions, num_users, log_discount=False)
¶
Compute the novelty profile based on the count of interactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_interactions
|
Tensor
|
The counts for item interactions in training set. |
required |
num_users
|
int
|
Number of users in the training set. |
required |
log_discount
|
bool
|
Whether or not to compute the discounted novelty. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
A tensor that contains the novelty score for each item. |
Source code in warprec/evaluation/metrics/base_metric.py
compute_popularity(item_interactions)
¶
Compute popularity tensor based on the interactions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
item_interactions
|
Tensor
|
The counts for item interactions in training set. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The interaction count for each item. |
Source code in warprec/evaluation/metrics/base_metric.py
discounted_relevance(target)
classmethod
¶
Compute the discounted relevance tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Tensor
|
The target tensor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The discounted relevance tensor. |
Source code in warprec/evaluation/metrics/base_metric.py
top_k_relevance(preds, target, k)
classmethod
¶
Compute the top k relevance tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
Tensor
|
The prediction tensor |
required |
target
|
Tensor
|
The target tensor. |
required |
k
|
int
|
The value of cutoff. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The top k relevance tensor. |
Source code in warprec/evaluation/metrics/base_metric.py
top_k_relevance_from_indices(target, top_k_indices)
classmethod
¶
Compute the top k relevance tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Tensor
|
The target tensor. |
required |
top_k_indices
|
Tensor
|
The top k indices. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The top k relevance tensor. |
Source code in warprec/evaluation/metrics/base_metric.py
top_k_values_indices(preds, k)
classmethod
¶
Compute the top k indices and values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
Tensor
|
The prediction tensor |
required |
k
|
int
|
The value of cutoff. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
Tuple[Tensor, Tensor]: - Tensor: The values tensor. - Tensor: The indices tensor |
Source code in warprec/evaluation/metrics/base_metric.py
valid_users(target)
classmethod
¶
Compute the number of valid users.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Tensor
|
The target tensor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
A Tensor containing 1 if a user is valid or 0 otherwise. |
Source code in warprec/evaluation/metrics/base_metric.py
warprec.evaluation.metrics.base_metric.RatingMetric
¶
Bases: BaseMetric
The definition of Rating Metric.
Attributes:
| Name | Type | Description |
|---|---|---|
error_sum |
Tensor
|
The tensor to store per-user error sum. |
total_count |
Tensor
|
The tensor to store per-user count of ratings. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_users
|
int
|
Number of users in the training set. |
required |
dist_sync_on_step
|
bool
|
Torchmetrics parameter. |
False
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the parent class. |
{}
|
Source code in warprec/evaluation/metrics/base_metric.py
compute()
¶
Computes the final metric value.
Source code in warprec/evaluation/metrics/base_metric.py
update(preds, user_indices, **kwargs)
¶
Unified update logic using index_add_.
Source code in warprec/evaluation/metrics/base_metric.py
warprec.evaluation.metrics.base_metric.TopKMetric
¶
Bases: BaseMetric
The definition of a Top-K metric.
Attributes:
| Name | Type | Description |
|---|---|---|
k |
int
|
The cutoff value. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
The cutoff for recommendations. |
required |
dist_sync_on_step
|
bool
|
Torchmetrics parameter. |
False
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the parent class. |
{}
|
Source code in warprec/evaluation/metrics/base_metric.py
dcg(rel)
¶
The Discounted Cumulative Gain definition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rel
|
Tensor
|
The relevance tensor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The discounted tensor. |
Source code in warprec/evaluation/metrics/base_metric.py
discounted_sum(k)
¶
Computes the discounted sum for k values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
The length of the tensor to discount. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The sum of the discounts for k values. |
Source code in warprec/evaluation/metrics/base_metric.py
remap_indices(top_k_indices, item_indices)
¶
Remap local batch indices to global item IDs if item_indices is provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
top_k_indices
|
Tensor
|
The top k indices tensor. |
required |
item_indices
|
Tensor
|
The global item indices tensor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The remapped top k indices tensor. |
Source code in warprec/evaluation/metrics/base_metric.py
warprec.evaluation.metrics.base_metric.UserAverageTopKMetric
¶
Bases: TopKMetric
The definition of a User Average Top-K metric.
Attributes:
| Name | Type | Description |
|---|---|---|
scores |
Tensor
|
The tensor to store metric values. |
user_interactions |
Tensor
|
The tensor to store number of interactions per user. |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
The cutoff. |
required |
num_users
|
int
|
Number of users in the training set. |
required |
*args
|
Any
|
The argument list. |
()
|
dist_sync_on_step
|
bool
|
Torchmetrics parameter. |
False
|
**kwargs
|
Any
|
Additional keyword arguments to pass to the parent class. |
{}
|
Source code in warprec/evaluation/metrics/base_metric.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
compute()
¶
Computes the final metric value.
Source code in warprec/evaluation/metrics/base_metric.py
compute_scores(preds, target, top_k_rel, **kwargs)
abstractmethod
¶
Math formula for the specific metric.
Metrics must implement this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
Tensor
|
The prediction tensor. |
required |
target
|
Tensor
|
The target tensor. |
required |
top_k_rel
|
Tensor
|
The top-k relevance tensor. |
required |
**kwargs
|
Any
|
The keyword argument dictionary. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The computed metric values per user. |
Source code in warprec/evaluation/metrics/base_metric.py
unpack_inputs(preds, **kwargs)
¶
Default unpacking method used by most metrics.
Retrieves the binary relevance, valid users and top-k binary relevance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
Tensor
|
The prediction tensor. |
required |
**kwargs
|
Any
|
The keyword argument dictionary. |
{}
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor, Tensor]
|
Tuple[Tensor, Tensor, Tensor]: - Tensor: The target tensor. - Tensor: The valid users tensor. - Tensor: The top-k relevance tensor. |
Source code in warprec/evaluation/metrics/base_metric.py
update(preds, user_indices, **kwargs)
¶
Unified update logic.