Language Model¤
The language_model
module is used to train, evaluate, and package language models. It consists of a LanguageModel
class, a Timer
class, and three debugging functions.
lexos.language_model.LanguageModel
¤
Create a LanguageModel object.
Source code in lexos\language_model\__init__.py
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 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 |
|
__init__(model_dir='language_model', config_file='config.cfg', training_file='train.conllu', dev_file='dev.conllu', test_file='test.conllu', gpu=-1, lang='xx', package_name='model_sm', package_version='1.0.0', components=['tagger'], optimize='efficiency', exclusive_classes=True, force=False, recipe=None)
¤
Initialise the LanguageModel object.
Source code in lexos\language_model\__init__.py
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 |
|
convert_assets(n_sents=10, merge_subtokens=True)
¤
Convert CONLLU assets to spaCy DocBins.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n_sents |
int
|
The number of sentences per doc (0 to disable). |
10
|
merge_subtokens |
bool
|
Whether to merge CoNLL-U subtokens. |
True
|
Source code in lexos\language_model\__init__.py
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 |
|
copy_assets(training_file=None, dev_file=None, test_file=None)
¤
Copy assets to the assets folder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
training_file |
str
|
The path to the training file to copy. |
None
|
dev_file |
str
|
The path to the dev file to copy. |
None
|
test_file |
str
|
The path to the test file to copy. |
None
|
Source code in lexos\language_model\__init__.py
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
|
evaluate(model=None, testfile=None, output=None, use_gpu=-1, gold_preproc=False, displacy_path=None, displacy_limit=25, silent=False)
¤
Evaluate a spaCy model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
str
|
The path to the model to evaluate. |
None
|
testfile |
Union[Path, str]
|
The path to the test file to evaluate. |
None
|
output |
Path
|
The path to the output file. |
None
|
use_gpu |
int
|
The GPU to use. |
-1
|
gold_preproc |
bool
|
Whether to use gold preprocessing. |
False
|
displacy_path |
Optional[Path]
|
The path to the displacy package. |
None
|
displacy_limit |
int
|
The number of tokens to display. |
25
|
silent |
bool
|
Whether to suppress output. |
False
|
Returns:
Type | Description |
---|---|
None
|
Dict[str, Any]: The evaluation results. |
Source code in lexos\language_model\__init__.py
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 |
|
fill_config(path, output_file=None, pretraining=False, diff=False, code_path=None)
¤
Fill partial config file with default values.
Adds all missing settings from the current config and creates all objects,
checks the registered functions for their default values, and updates the
config file. Although the LanguageModel
class automatically generates a
full config file, this method may be useful for debugging.
DOCS: https://spacy.io/api/cli#init-fill-config
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Union[str, Path]
|
Path to the config file to fill. |
required |
output_file |
Union[str, Path]
|
Path to output .cfg file. |
None
|
pretraining |
bool
|
Include config for pretraining (with "spacy pretrain"). |
False
|
diff |
bool
|
Print a visual diff highlighting the changes. |
False
|
code_path |
Union[str, Path]
|
Path to Python file with additional code (registered functions) to be imported. |
None
|
Source code in lexos\language_model\__init__.py
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 |
|
load_config(filepath=None)
¤
Load the config from the config file.
Use this method if you wish to load the config from a different file.
But use with caution, as the old config file will be overwritten, and
the new config file may have different paths or a different pipeline
from those you used to instantiate the LanguageModel
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
Path
|
The path to the config file. |
None
|
Source code in lexos\language_model\__init__.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
|
package(input_dir=None, output_dir=None, meta_path=None, code_paths=[], name=None, version=None, create_meta=False, create_sdist=True, create_wheel=False, force=False, silent=False)
¤
Package the model so that it can be installed.
Source code in lexos\language_model\__init__.py
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 |
|
save_config(filepath=None)
¤
Save the config from the config file.
Use this method to save a config file after making modifications.
But use with caution, as the old config file will be overwritten, and
the new config file may have different paths or a different pipeline
from those you used to instantiate the LanguageModel
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
Path
|
The path to a config file (for saving a copy). |
None
|
Source code in lexos\language_model\__init__.py
373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
|
train()
¤
Train the corpus.
Source code in lexos\language_model\__init__.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
|
lexos.language_model.Timer
¤
Create a timer object.
Source code in lexos\language_model\__init__.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
__init__()
¤
Initialise the timer object.
Source code in lexos\language_model\__init__.py
31 32 33 |
|
get_time_elapsed()
¤
Get the elapsed time and format it as hours, minutes, and seconds.
Source code in lexos\language_model\__init__.py
35 36 37 38 39 40 41 |
|
lexos.language_model.debug_config(config_path, overrides={}, code_path=None, show_funcs=False, show_vars=False)
¤
Debug a config file and show validation errors.
The function will create all objects in the tree and validate them. Note that
some config validation errors are blocking and will prevent the rest of the
config from being resolved. This means that you may not see all validation errors
at once and some issues are only shown once previous errors have been fixed.
As with the 'train' command, you can override settings from the config by passing
arguments in the overrides
dict.
DOCS: https://spacy.io/api/cli#debug-config
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_path |
Union[str, Path]
|
Path to the configuration file. |
required |
overrides |
dict
|
A dictionary of config overrides. |
{}
|
code_path |
Union[str, Path]
|
Path to Python file with additional code (registered functions) to be imported. |
None
|
show_funcs |
bool
|
Show an overview of all registered functions used in the config and where they come from (modules, files etc.). |
False
|
show_vars |
bool
|
Show an overview of all variables referenced in the config and their values. This will also reflect variables overwritten in the function call. |
False
|
Source code in lexos\language_model\__init__.py
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 |
|
lexos.language_model.debug_data(config_path, overrides={}, code_path=None, ignore_warnings=False, verbose=False, no_format=False)
¤
Analyze, debug and validate your training and development data.
Outputs useful stats, and can help you find problems like invalid entity annotations, cyclic dependencies, low data labels and more. DOCS: https://spacy.io/api/cli#debug-data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_path |
Union[str, Path]
|
Path to the configuration file. |
required |
overrides |
dict
|
A dictionary of config overrides. |
{}
|
code_path |
Union[str, Path]
|
Path to Python file with additional code (registered functions) to be imported. |
None
|
ignore_warnings |
bool
|
Ignore warnings, only show stats and errors. |
False
|
verbose |
bool
|
Print additional information and explanations. |
False
|
no_format |
bool
|
Don't pretty-print the results. |
False
|
Note
The only way to avoid the SystemExit: 1
error is to make a copy of the module and
remove the sys.exit()
call at the end.
Source code in lexos\language_model\__init__.py
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 |
|
lexos.language_model.debug_model(config_path, config_overrides={}, component='tagger', layers=[], dimensions=False, parameters=False, gradients=False, attributes=False, P0=False, P1=False, P2=False, P3=False, use_gpu=-1)
¤
Debug a trained model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config_path |
Union[str, Path]
|
Path to the config file. |
required |
config_overrides |
dict
|
A dictionary of config overrides. |
{}
|
component |
str
|
Name of the pipeline component of which the model should be analysed |
'tagger'
|
layers |
str
|
List of layer IDs to print. |
[]
|
dimensions |
bool
|
Whether to show dimensions. |
False
|
parameters |
bool
|
Whether to show parameters. |
False
|
gradients |
bool
|
Whether to show gradients. |
False
|
attributes |
bool
|
Whether to show attributes. |
False
|
P0 |
bool
|
Whether to print the model before training. |
False
|
P1 |
bool
|
Whether to print the model after initialization. |
False
|
P2 |
bool
|
Whether to print the model after training. |
False
|
P3 |
bool
|
Whether to print final predictions. |
False
|
use_gpu |
int
|
GPU ID or -1 for CPU |
-1
|
Source code in lexos\language_model\__init__.py
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
|