API Reference
You can use the API to call the evaluation from a python script. For this, you need to load a dataset (see Data Files for how these should be structured) and then execute the evaluation function using your desired configuration.
Example (compare with src/lm_pub_quiz/cli/evaluate_model.py
):
from lm_pub_quiz import Dataset, Evaluator
# Load dataset
dataset = Dataset.from_name("BEAR")
# Create Evaluator (and load model)
evaluator = Evaluator.from_model("distilbert-base-cased")
# Run evaluation
result = evaluator.evaluate_dataset(dataset)
# Save result object
result.save("outputs/my_results")
Evaluator
lm_pub_quiz.Evaluator
Bases: BaseEvaluator
Perplexity-based evaluator base class.
score_answers(*, template, answers, reduction, subject=None)
abstractmethod
Score an answer given a template.
This function must be implemented by child-classes for each model-type.
lm_pub_quiz.MaskedLMEvaluator
Bases: Evaluator
score_answers(*, template, answers, reduction, subject=None)
Calculates sequence scores using the Masked Language Model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template |
str
|
The template to use (should contain a |
required |
answers |
List[str]
|
List of answers to calculate score for. |
required |
Returns:
Type | Description |
---|---|
Union[ReducedReturnFormat, EachTokenReturnFormat]
|
List[float]: List of suprisals scores per sequence |
lm_pub_quiz.CausalLMEvaluator
Bases: Evaluator
score_answers(*, template, answers, reduction, subject=None)
Calculates sequence scores using the Casual Language Model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
template |
str
|
The template to use (should contain a |
required |
answers |
List[str]
|
List of answers to calculate score for. |
required |
Returns:
Type | Description |
---|---|
Union[EachTokenReturnFormat, ReducedReturnFormat]
|
List[float]: List of suprisals scores per sequence |
Dataset Representation
There are two classes which are used to represent a dataset: Relation
and Dataset
(which is essentially a container for a number of relations).
lm_pub_quiz.Relation
Bases: RelationBase
Represents a relation within a dataset, including its code, answer space, templates, and an instance table.
Attributes:
Name | Type | Description |
---|---|---|
relation_code |
str
|
A unique code identifying the relation. |
answer_space |
List[str]
|
A list of possible answers for this relation. |
templates |
List[str]
|
Templates for generating instances of this relation. |
instance_table |
DataFrame
|
A pandas DataFrame containing instances of the relation. |
Methods:
Name | Description |
---|---|
__str__ |
Returns a string representation showing the first five instances in the relation. |
__repr__ |
Returns a string representation of the relation code. |
__len__ |
Returns the number of instances in the relation. |
subsample |
Randomly samples a subset of instances from the relation. |
load_from_file |
Class method to create a Relation instance from a JSONL file. |
from_path(path, *, relation_code=None, lazy=True, fmt=None)
classmethod
Loads a relation from a JSONL file and associated metadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
PathLike
|
The path to the dataset directory. |
required |
relation_code |
str
|
The specific code of the relation to load. |
None
|
lazy |
bool
|
If False, the instance table is loaded directly into memory. |
True
|
Returns:
Name | Type | Description |
---|---|---|
Relation |
Relation
|
An instance of the Relation class populated with data from the file. |
Raises:
Type | Description |
---|---|
Exception
|
If there is an error in loading the file or processing the data. |
subsample(n=10)
Returns only a subsampled version of the dataset of the size n.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
Size of the subsampled dataset |
10
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: Subsampled version of the dataset. |
lm_pub_quiz.Dataset
Bases: DatasetBase[Relation]
A collection of relations forming a multiple choice dataset.
Attributes:
Name | Type | Description |
---|---|---|
relations |
List[Relation]
|
A list of Relation instances in the dataset. |
dataset_name |
str
|
The name of the dataset. |
Methods:
Name | Description |
---|---|
load_from_path |
Class method to load a dataset from a specified path. |
from_name(name, *, lazy=True, base_path=None, chunk_size=10 * 1024, relation_info=None, **kwargs)
classmethod
Loads a dataset from the cache (if available) or the url which is specified in the internal dataset table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
The name of the dataset. |
required |
lazy |
bool
|
If False, the instance tables of all relations are directly loaded into memory. |
True
|
Returns:
Name | Type | Description |
---|---|---|
Dataset |
Dataset
|
An instance if Dataset loaded with the relations from the directory. |
Raises:
Type | Description |
---|---|
Exception
|
If there is an error in loading the dataset. |
from_path(path, *, lazy=True, fmt=None, relation_info=None, **kwargs)
classmethod
Loads a multiple choice dataset from a specified directory path.
This method scans the directory for relation files and assembles them into a MultipleChoiceDataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The directory path where the dataset is stored. |
required |
lazy |
bool
|
If False, the instance tables of all relations are directly loaded into memory. |
True
|
Returns:
Name | Type | Description |
---|---|---|
Dataset |
Dataset
|
An instance if Dataset loaded with the relations from the directory. |
Raises:
Type | Description |
---|---|
Exception
|
If there is an error in loading the dataset. |
Evaluation Result
Similar to the dataset representation, the results are also represented in two classes RelationResult
and the container DatasetResults
.
lm_pub_quiz.RelationResult
Bases: RelationBase
from_path(path, *, relation_code=None, metadata=None, lazy=True, fmt=None)
classmethod
Loads the evaluated relation from a JSONL file and associated metadata.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
PathLike
|
The path to the relations instance table. |
required |
Returns:
Name | Type | Description |
---|---|---|
RelationResult |
RelationResult
|
An instance of the RelationResult class populated with data from the file. |
Raises:
Type | Description |
---|---|
Exception
|
If there is an error in loading the file or processing the data. |
lm_pub_quiz.DatasetResults
Bases: DatasetBase[RelationResult]
Container for relation results.
from_path(path, *, lazy=True, fmt=None, relation_info=None, **kwargs)
classmethod
Loads a results from a specified directory path.
This method scans the directory for relation files and assembles them into a DatasetResults.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
str
|
The directory path where the dataset is stored. |
required |
Returns:
Name | Type | Description |
---|---|---|
DatasetResults |
DatasetResults
|
An instance of DatasetResults loaded with the results from the directory. |
Raises:
Type | Description |
---|---|
Exception
|
If there is an error in loading the dataset. |
get_metadata(key=None)
Return metadata from the relations. If no keys are passed, all consistent values are returned.
get_metrics(metrics, *, accumulate=False, divide_support=True)
Return the metrics for the relations in this dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
accumulate |
bool | str | None
|
Compute the metrics for groups of relations (e.g. over the domains) or
compute the overall scores for the complete dataset by setting |
False
|
divide_support |
bool
|
Set to true to divide the support (added by a relation to a group) by the number of
groups it adds to (only relevant if there are multiple groups per relation i.e. when |
True
|
Returns:
Type | Description |
---|---|
Union[DataFrame, Series]
|
pandas.DataFrame | pandas.Series: A Series or DataFrame with the selected metrics depending on whether all relations where accumulated. |
Data Base Clasess
The dataset representations as well as the evaluation results are based on common base classes.
lm_pub_quiz.data.base.RelationBase
Bases: DataBase
Base class for the representation of relations and relations results.
Source code in src/lm_pub_quiz/data/base.py
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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 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 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
|
activated()
Return self or a copy of self with the instance_table loaded (lazy loading disabled).
copy(**kw)
Create a copy of the isntance with specified fields replaced by new values.
Source code in src/lm_pub_quiz/data/base.py
get_metadata(key=None)
Get or set metadata.
Source code in src/lm_pub_quiz/data/base.py
relation_info(key=None, /, **kw)
Get or set additional relation information.
Source code in src/lm_pub_quiz/data/base.py
save(save_path, fmt=None)
Save results to a file and export meta_data
Source code in src/lm_pub_quiz/data/base.py
save_instance_table(instance_table, path, fmt=None)
classmethod
Save instance table with the format determined by the path suffix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance_table |
DataFrame
|
The instances to save. |
required |
path |
Path
|
Where to save the instance table. If format is not specified, the suffix is used to determined the format. |
required |
fmt |
str
|
Which to save the instances in. |
None
|
Source code in src/lm_pub_quiz/data/base.py
search_path(path, relation_code=None, fmt=None)
classmethod
Search path for instance files.
Source code in src/lm_pub_quiz/data/base.py
lm_pub_quiz.data.base.DatasetBase
Bases: DataBase
, Generic[RT]
Base class for a collection of relations or relations results.