### Example class docstring implementation Source: https://github.com/allenai/allennlp/blob/main/CONTRIBUTING.md A complete example showing how to document a class and its methods using AllenNLP docstring conventions. ```python class SentenceClassifier(Model): """ A model for classifying sentences. This is based on [this paper](link-to-paper). The input is a sentence and the output is a score for each target label. # Parameters vocab : `Vocabulary` text_field_embedder : `TextFieldEmbedder` The text field embedder that will be used to create a representation of the source tokens. seq2vec_encoder : `Seq2VeqEncoder` This encoder will take the embeddings from the `text_field_embedder` and encode them into a vector which represents the un-normalized scores for the target labels. dropout : `Optional[float]`, optional (default = `None`) Optional dropout to apply to the text field embeddings before passing through the `seq2vec_encoder`. """ def __init__( self, vocab: Vocabulary, text_field_embedder: TextFieldEmbedder, seq2vec_encoder: Seq2SeqEncoder, dropout: Optional[float] = None, ) -> None: pass def forward( self, tokens: TextFieldTensors, labels: Optional[Tensor] = None, ) -> Dict[str, Tensor]: """ Runs a forward pass of the model, computing the predicted logits and also the loss when `labels` is provided. # Parameters tokens : `TextFieldTensors` The tokens corresponding to the source sequence. labels : `Optional[Tensor]`, optional (default = `None`) The target labels. # Returns `Dict[str, Tensor]` An output dictionary with keys for the `loss` and `logits`. """ pass ``` -------------------------------- ### Install AllenNLP from Source Source: https://github.com/allenai/allennlp/blob/main/README.md Commands to clone the repository and install the package in editable mode with development dependencies. ```bash git clone https://github.com/allenai/allennlp.git ``` ```bash pip install -U pip setuptools wheel pip install --editable .[dev,all] ``` -------------------------------- ### Install AllenNLP with Pip Source: https://github.com/allenai/allennlp/blob/main/README.md Installs the AllenNLP library using pip. It is recommended to install PyTorch first by following instructions on pytorch.org. ```bash pip install allennlp ``` -------------------------------- ### Install dependencies in editable mode Source: https://github.com/allenai/allennlp/blob/main/CONTRIBUTING.md Install the package in editable mode so that local changes are immediately reflected in the virtual environment. ```bash pip install -U pip setuptools wheel pip install -e .[dev,all] ``` -------------------------------- ### Install All Optional AllenNLP Packages via Pip Source: https://github.com/allenai/allennlp/blob/main/README.md Installs AllenNLP with all available optional dependencies using pip. This provides a comprehensive installation including models, semparse, server, and optuna. ```bash pip install allennlp[all] ``` -------------------------------- ### Install AllenNLP Models Package Source: https://github.com/allenai/allennlp/blob/main/README.md Installs the 'allennlp-models' package, which contains pre-trained NLP models and components for training and running official AllenNLP models. ```bash pip install allennlp-models ``` -------------------------------- ### Install All Optional AllenNLP Packages via Conda Source: https://github.com/allenai/allennlp/blob/main/README.md Installs AllenNLP and all its associated optional packages (models, semparse, server, optuna) using conda-forge. This is a convenient way to get a comprehensive AllenNLP setup. ```bash conda install -c conda-forge allennlp-models allennlp-semparse allennlp-server allennlp-optuna ``` -------------------------------- ### Install AllenNLP with Optional Checklist Package via Pip Source: https://github.com/allenai/allennlp/blob/main/README.md Installs AllenNLP along with the 'checklist' optional dependencies using pip. The '[checklist]' syntax specifies the extra requirements to install. ```bash pip install allennlp[checklist] ``` -------------------------------- ### Method with Alternative Return Section in Python Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Example of a Python method that returns an integer. The documentation uses an alternative return section format. ```python class SomeClass: | ... | def method_with_alternative_return_section(self) -> int ``` -------------------------------- ### Install AllenNLP with Conda Source: https://github.com/allenai/allennlp/blob/main/README.md Installs AllenNLP and Python 3.8 using conda-forge. Ensure you have conda installed and a compatible Python version. ```bash conda install -c conda-forge python=3.8 allennlp ``` -------------------------------- ### Install AllenNLP with Optional Checklist Package via Conda Source: https://github.com/allenai/allennlp/blob/main/README.md Installs AllenNLP along with the 'checklist' optional package using conda-forge. For a full list of optional packages, refer to the documentation. ```bash conda install -c conda-forge allennlp-checklist ``` -------------------------------- ### Document Class-Level Variable in Python Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Example of documenting a class-level variable within a Python class. This variable is assigned an integer value. ```python class SomeClass: | ... | some_class_level_variable = 1 ``` -------------------------------- ### Define Global Variable in Python Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Example of defining a global variable in Python. Used for storing simple values accessible throughout the module. ```python SOME_GLOBAL_VAR = "Ahhhh I'm a global var!!" ``` -------------------------------- ### Build and serve documentation Source: https://github.com/allenai/allennlp/blob/main/CONTRIBUTING.md Commands to build the API documentation or serve it locally for preview. ```bash make build-docs ``` ```bash make serve-docs ``` -------------------------------- ### Configure and Train with GradientDescentTrainer Source: https://context7.com/allenai/allennlp/llms.txt Sets up and runs the GradientDescentTrainer with specified optimizer, data loaders, epochs, and regularization. ```python from allennlp.training import GradientDescentTrainer from allennlp.training.optimizers import AdamOptimizer from allennlp.training.learning_rate_schedulers import LinearWithWarmup from allennlp.data import DataLoader # Create trainer trainer = GradientDescentTrainer( model=model, optimizer=AdamOptimizer(model.parameters(), lr=1e-4), data_loader=train_loader, validation_data_loader=val_loader, num_epochs=10, patience=3, # Early stopping patience validation_metric="+accuracy", # Maximize accuracy serialization_dir="output/model", cuda_device=0, grad_norm=1.0, # Gradient clipping learning_rate_scheduler=LinearWithWarmup( optimizer=optimizer, num_epochs=10, num_steps_per_epoch=len(train_loader), warmup_steps=100 ) ) # Train metrics = trainer.train() print(f"Best validation accuracy: {metrics['best_validation_accuracy']}") ``` -------------------------------- ### Create Archive from Serialization Directory Source: https://context7.com/allenai/allennlp/llms.txt Package model weights and configuration into a .tar.gz archive file for distribution or evaluation. ```python archive_model( serialization_dir="output/model", weights="best.th", # Which weights file to include archive_path="model.tar.gz" # Output path ) ``` -------------------------------- ### Register and instantiate components Source: https://context7.com/allenai/allennlp/llms.txt Use the Registrable system to register custom classes for instantiation via configuration parameters. ```python from allennlp.common import Registrable from allennlp.common.from_params import FromParams # Register a custom component @Registrable.register("my_custom_encoder") class MyCustomEncoder(Registrable): def __init__(self, input_dim: int, hidden_dim: int, dropout: float = 0.1): self.input_dim = input_dim self.hidden_dim = hidden_dim self.dropout = dropout def forward(self, inputs): # Implementation pass # List available implementations available = MyCustomEncoder.list_available() print(available) # ['my_custom_encoder'] # Instantiate by name encoder = MyCustomEncoder.by_name("my_custom_encoder")( input_dim=300, hidden_dim=256 ) # Or from params from allennlp.common import Params params = Params({ "type": "my_custom_encoder", "input_dim": 300, "hidden_dim": 256 }) encoder = MyCustomEncoder.from_params(params) ``` -------------------------------- ### Run AllenNLP in Docker Source: https://github.com/allenai/allennlp/blob/main/README.md Commands to initialize the local directory and execute the AllenNLP Docker container with GPU support. ```bash mkdir -p $HOME/.allennlp/ docker run --rm --gpus all -v $HOME/.allennlp:/root/.allennlp allennlp/allennlp:latest ``` ```bash docker run --rm --gpus all -v $HOME/.allennlp:/root/.allennlp allennlp/allennlp:latest test-install ``` -------------------------------- ### List Docker Images Source: https://github.com/allenai/allennlp/blob/main/README.md Command to verify the presence of the built AllenNLP Docker image. ```text REPOSITORY TAG IMAGE ID CREATED SIZE allennlp/allennlp latest b66aee6cb593 5 minutes ago 2.38GB ``` -------------------------------- ### Define Class with Decorator in Python Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Example of defining a Python class decorated with `@dataclass`. This is a common pattern for creating data-holding classes. ```python @dataclass class ClassWithDecorator ``` -------------------------------- ### Check remote configuration Source: https://github.com/allenai/allennlp/blob/main/CONTRIBUTING.md Verify the current remote repositories configured for your local clone. ```bash git remote -v ``` -------------------------------- ### Verify Current Version Source: https://github.com/allenai/allennlp/blob/main/RELEASE_PROCESS.md Check the current version configuration against the TAG environment variable. ```bash python scripts/get_version.py current ``` -------------------------------- ### Run code quality and testing tools Source: https://github.com/allenai/allennlp/blob/main/CONTRIBUTING.md Execute formatting, linting, and type-checking tools locally before submitting a pull request. ```bash black . ``` ```bash flake8 . ``` ```bash make typecheck ``` -------------------------------- ### Generate Release Notes Source: https://github.com/allenai/allennlp/blob/main/RELEASE_PROCESS.md Generate the release notes content to be copied into the GitHub release description. ```bash python scripts/release_notes.py ``` -------------------------------- ### Model.forward_on_instances Source: https://context7.com/allenai/allennlp/llms.txt Runs the model on a list of instances and returns human-readable outputs. ```APIDOC ## Model.forward_on_instances ### Description Runs the model on a list of instances and returns human-readable outputs. ### Parameters #### Request Body - **instances** (List[Instance]) - Required - A list of instances to process. ### Response #### Success Response (200) - **outputs** (List[Dict]) - A list of dictionaries containing model predictions (e.g., logits, probs). ``` -------------------------------- ### Create Sequence Labeling Instance (NER) Source: https://context7.com/allenai/allennlp/llms.txt Build an Instance for sequence labeling tasks like Named Entity Recognition, associating tags with tokens. ```python from allennlp.data import Instance from allennlp.data.fields import TextField, SequenceLabelField from allennlp.data.tokenizers import Token # Sequence labeling instance (NER) tokens = [Token("John"), Token("lives"), Token("in"), Token("NYC")] text_field = TextField(tokens, {"tokens": token_indexer}) tags = ["B-PER", "O", "O", "B-LOC"] tags_field = SequenceLabelField(tags, text_field, "tags") instance = Instance({"tokens": text_field, "tags": tags_field}) ``` -------------------------------- ### Clone the repository Source: https://github.com/allenai/allennlp/blob/main/CONTRIBUTING.md Use these commands to clone your fork of the AllenNLP repository locally. ```bash git clone https://github.com/USERNAME/allennlp.git ``` ```bash git clone git@github.com:USERNAME/allennlp.git ``` -------------------------------- ### Train Model with AllenNLP CLI Source: https://context7.com/allenai/allennlp/llms.txt Use the `allennlp train` command to train a model from a configuration file. Options include parameter overrides, resuming training, forcing directory overwrite, and dry runs. ```bash # Basic training allennlp train config.jsonnet -s output_dir ``` ```bash # Training with parameter overrides allennlp train config.jsonnet -s output_dir --overrides '{"trainer.num_epochs": 10}' ``` ```bash # Resume training from checkpoint allennlp train config.jsonnet -s output_dir --recover ``` ```bash # Force overwrite existing output directory allennlp train config.jsonnet -s output_dir --force ``` ```bash # Dry run to validate config and show dataset statistics allennlp train config.jsonnet -s output_dir --dry-run ``` -------------------------------- ### Tag and Push Release Source: https://github.com/allenai/allennlp/blob/main/RELEASE_PROCESS.md Create a git tag for the release and push it to the remote repository. ```bash git tag $TAG -m "Release $TAG" && git push --tags ``` -------------------------------- ### Train Model Programmatically (File) Source: https://context7.com/allenai/allennlp/llms.txt Trains a model using `train_model_from_file` with a configuration file. Allows specifying overrides, forcing directory overwrite, and returning the model. ```python from allennlp.commands.train import train_model_from_file # Train a model from a config file model = train_model_from_file( parameter_filename="experiments/text_classifier.jsonnet", serialization_dir="output/text_classifier", overrides='{"trainer.num_epochs": 5}', force=True, # Overwrite existing directory return_model=True ) ``` ```python # Training with recovery from checkpoint model = train_model_from_file( parameter_filename="experiments/my_model.jsonnet", serialization_dir="output/my_model", recover=True # Resume from existing checkpoint ) ``` -------------------------------- ### Build and Use Vocabulary Source: https://context7.com/allenai/allennlp/llms.txt Builds a vocabulary from instances, allowing for minimum count and maximum size constraints. Supports saving, loading, and creation from pretrained transformers. ```python from allennlp.data import Vocabulary, Instance from allennlp.data.fields import TextField, LabelField # Build vocabulary from instances instances = [instance1, instance2, instance3] vocab = Vocabulary.from_instances( instances, min_count={"tokens": 2}, # Minimum count for tokens max_vocab_size={"tokens": 10000} # Maximum vocabulary size ) # Check vocabulary size print(f"Token vocab size: {vocab.get_vocab_size('tokens')}") print(f"Label vocab size: {vocab.get_vocab_size('labels')}") # Convert tokens to indices token_index = vocab.get_token_index("hello", namespace="tokens") token = vocab.get_token_from_index(token_index, namespace="tokens") # Save and load vocabulary vocab.save_to_files("vocab_dir") loaded_vocab = Vocabulary.from_files("vocab_dir") # Create vocabulary from pretrained transformer vocab = Vocabulary.from_pretrained_transformer("bert-base-uncased", namespace="tokens") ``` -------------------------------- ### Python API: train_model_from_file Source: https://context7.com/allenai/allennlp/llms.txt Trains a model from a configuration file and saves results to the specified directory. ```APIDOC ## train_model_from_file ### Description Trains a model from a configuration file and saves results to the specified directory. ### Method ```python from allennlp.commands.train import train_model_from_file # Train a model from a config file model = train_model_from_file( parameter_filename="experiments/text_classifier.jsonnet", serialization_dir="output/text_classifier", overrides='{"trainer.num_epochs": 5}', force=True, # Overwrite existing directory return_model=True ) # Training with recovery from checkpoint model = train_model_from_file( parameter_filename="experiments/my_model.jsonnet", serialization_dir="output/my_model", recover=True # Resume from existing checkpoint ) ``` ``` -------------------------------- ### Format parameter docstrings Source: https://github.com/allenai/allennlp/blob/main/CONTRIBUTING.md Standard syntax for documenting parameters and optional parameters in docstrings. ```text name : `type` Description of the parameter, indented by four spaces. ``` ```text name : `type`, optional (default = `default_value`) Description of the parameter, indented by four spaces. ``` -------------------------------- ### Create MultiProcessDataLoader Source: https://context7.com/allenai/allennlp/llms.txt Initializes a MultiProcessDataLoader for efficient, multiprocessing-based data loading with configurable batch size, shuffling, and worker count. ```python from allennlp.data import DataLoader, Vocabulary from allennlp.data.data_loaders import MultiProcessDataLoader from allennlp.data.samplers import BucketBatchSampler # Create data loader data_loader = MultiProcessDataLoader( reader=reader, data_path="data/train.jsonl", batch_size=32, shuffle=True, num_workers=4, # Number of data loading processes max_instances_in_memory=10000 # For memory-efficient loading ) # Index with vocabulary data_loader.index_with(vocab) # Iterate over batches for batch in data_loader: tokens = batch["tokens"] labels = batch["label"] # tokens is a dict of tensors ready for the model ``` -------------------------------- ### Create Instance with Metadata Source: https://context7.com/allenai/allennlp/llms.txt Augment an AllenNLP Instance with additional metadata, such as original text or document identifiers. ```python from allennlp.data import Instance from allennlp.data.fields import TextField, SequenceLabelField, MetadataField from allennlp.data.tokenizers import Token # With metadata metadata = {"original_text": "John lives in NYC", "doc_id": 123} instance = Instance({ "tokens": text_field, # Assuming text_field is defined from previous example "tags": tags_field, # Assuming tags_field is defined from previous example "metadata": MetadataField(metadata) }) ``` -------------------------------- ### Instantiate DatasetReader Source: https://context7.com/allenai/allennlp/llms.txt Instantiate a DatasetReader by its registered name. Configure parameters like `max_tokens` during initialization. This reader will be used to load and process data into AllenNLP Instances. ```python from allennlp.data import DatasetReader # Instantiate reader reader = DatasetReader.by_name("text_classification")( max_tokens=512 ) ``` -------------------------------- ### Index Instance and Convert to Tensors Source: https://context7.com/allenai/allennlp/llms.txt Index the fields of an AllenNLP Instance using a vocabulary and convert it into a tensor dictionary for model input. ```python # Index instance with vocabulary instance.index_fields(vocab) tensor_dict = instance.as_tensor_dict() ``` -------------------------------- ### Define experiments with Jsonnet Source: https://context7.com/allenai/allennlp/llms.txt Use Jsonnet files to define model architecture, data loading, and training parameters. ```jsonnet // config.jsonnet local embedding_dim = 300; local hidden_dim = 256; { "dataset_reader": { "type": "text_classification", "tokenizer": { "type": "whitespace" }, "token_indexers": { "tokens": { "type": "single_id", "lowercase_tokens": true } } }, "train_data_path": "data/train.jsonl", "validation_data_path": "data/dev.jsonl", "model": { "type": "text_classifier", "embedder": { "type": "basic", "token_embedders": { "tokens": { "type": "embedding", "embedding_dim": embedding_dim, "trainable": true } } }, "encoder": { "type": "lstm", "input_size": embedding_dim, "hidden_size": hidden_dim, "bidirectional": true } }, "data_loader": { "batch_size": 32, "shuffle": true }, "trainer": { "num_epochs": 20, "patience": 5, "cuda_device": 0, "optimizer": { "type": "adam", "lr": 0.001 }, "validation_metric": "+accuracy" } } ``` -------------------------------- ### Sync fork with upstream Source: https://github.com/allenai/allennlp/blob/main/CONTRIBUTING.md Update your local main branch with the latest changes from the upstream repository. ```bash git checkout main # if not already on main git pull --rebase upstream main git push ``` -------------------------------- ### Create a new feature branch Source: https://github.com/allenai/allennlp/blob/main/CONTRIBUTING.md Create and push a new branch for your specific contribution. ```bash # replace BRANCH with whatever name you want to give it git checkout -b BRANCH git push -u origin BRANCH ``` -------------------------------- ### Load model archives Source: https://context7.com/allenai/allennlp/llms.txt Load saved model archives with optional configuration overrides. ```python from allennlp.models.archival import load_archive, archive_model # Load an archive archive = load_archive( "model.tar.gz", cuda_device=0, overrides='{"model.dropout": 0.5}' ) ``` -------------------------------- ### Function: func_with_args Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Documentation for the func_with_args function, which takes integer arguments. ```APIDOC ## func_with_args ### Description This function has some args. ### Method ```python def func_with_args(a: int, b: int, c: int = 3) -> int ``` ### Parameters #### Path Parameters - __a__ (int) - Required - A number. - __b__ (int) - Required - Another number. - __c__ (int) - Optional - Yet another number. (default = 3) ### Notes These are some notes. ### Returns - int - The result of `a + b * c`. ``` -------------------------------- ### Define Class with Constructor in Python Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Illustrates the definition of a Python class with an initializer method. The constructor takes a float argument. ```python class SomeClass: | def __init__(self) -> None ``` -------------------------------- ### Set Release Tag Environment Variable Source: https://github.com/allenai/allennlp/blob/main/RELEASE_PROCESS.md Set the TAG environment variable to the desired version string before proceeding with the release. ```bash export TAG='v1.0.0' ``` ```fish set -x TAG 'v1.0.0' ``` -------------------------------- ### Run Model on Multiple Instances Source: https://context7.com/allenai/allennlp/llms.txt Process a list of instances using a loaded AllenNLP model. The model should be in evaluation mode. Outputs are dictionaries containing numpy arrays, useful for batch predictions. ```python from allennlp.models import Model from allennlp.models.archival import load_archive # Load model archive = load_archive("model.tar.gz", cuda_device=0) model = archive.model model.eval() # Run on multiple instances instances = [instance1, instance2, instance3] outputs = model.forward_on_instances(instances) # Each output is a dict with numpy arrays for output in outputs: predicted_label = output["probs"].argmax() confidence = output["probs"].max() print(f"Predicted: {predicted_label}, Confidence: {confidence:.3f}") ``` -------------------------------- ### Define Function with Arguments in Python Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Shows how to define a Python function with typed arguments, including an optional argument with a default value. The function returns an integer. ```python def func_with_args(a: int, b: int, c: int = 3) -> int ``` -------------------------------- ### Method with Named Return Value in Python Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Illustrates a Python method returning an integer, with the return value documented using a specific name. This is useful for clarity when multiple values could be returned. ```python class SomeClass: | ... | def method_with_alternative_return_section3(self) -> int ``` -------------------------------- ### Create Text Classification Instance Source: https://context7.com/allenai/allennlp/llms.txt Construct an AllenNLP Instance for text classification, including tokenized text and a label. ```python from allennlp.data import Instance from allennlp.data.fields import TextField, LabelField from allennlp.data.tokenizers import Token # Text classification instance tokens = [Token("This"), Token("is"), Token("great")] text_field = TextField(tokens, {"tokens": token_indexer}) label_field = LabelField("positive") instance = Instance({"tokens": text_field, "label": label_field}) ``` -------------------------------- ### Class: SomeClass Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Documentation for the SomeClass class. ```APIDOC ## SomeClass ### Description I'm a class! ### Constructor ```python class SomeClass: | def __init__(self) -> None ``` ### Parameters #### Path Parameters - __x__ (float) - Required - This attribute is called `x`. ### Class Variables #### some_class_level_variable ```python class SomeClass: | ... | some_class_level_variable = 1 ``` This is how you document a class-level variable. #### some_class_level_var_with_type ```python class SomeClass: | ... | some_class_level_var_with_type: int = 1 ``` ### Methods #### some_method ```python class SomeClass: | ... | def some_method(self) -> None ``` I'm a method! But I don't do anything. ##### Returns - None #### method_with_alternative_return_section ```python class SomeClass: | ... | def method_with_alternative_return_section(self) -> int ``` Another method. ##### Returns - A completely arbitrary number. #### method_with_alternative_return_section3 ```python class SomeClass: | ... | def method_with_alternative_return_section3(self) -> int ``` Another method. ##### Returns - number (int) - A completely arbitrary number. ``` -------------------------------- ### Implement TextClassificationReader Source: https://context7.com/allenai/allennlp/llms.txt Create a custom DatasetReader for text classification tasks. Implement `_read` to parse data files and `text_to_instance` to convert text and labels into AllenNLP Instances. Supports tokenization and max token limits. ```python from typing import Iterable, Dict from allennlp.data import DatasetReader, Instance from allennlp.data.fields import TextField, LabelField from allennlp.data.tokenizers import Tokenizer, WhitespaceTokenizer from allennlp.data.token_indexers import TokenIndexer, SingleIdTokenIndexer @DatasetReader.register("text_classification") class TextClassificationReader(DatasetReader): def __init__( self, tokenizer: Tokenizer = None, token_indexers: Dict[str, TokenIndexer] = None, max_tokens: int = None, **kwargs, ) -> None: super().__init__(**kwargs) self.tokenizer = tokenizer or WhitespaceTokenizer() self.token_indexers = token_indexers or {"tokens": SingleIdTokenIndexer()} self.max_tokens = max_tokens def _read(self, file_path: str) -> Iterable[Instance]: import json with open(file_path) as f: for line in f: data = json.loads(line) text = data["text"] label = data.get("label") yield self.text_to_instance(text, label) def text_to_instance( self, text: str, label: str = None, ) -> Instance: tokens = self.tokenizer.tokenize(text) if self.max_tokens: tokens = tokens[:self.max_tokens] fields = {"tokens": TextField(tokens, self.token_indexers)} if label is not None: fields["label"] = LabelField(label) return Instance(fields) ``` -------------------------------- ### Commit and Push Release Changes Source: https://github.com/allenai/allennlp/blob/main/RELEASE_PROCESS.md Commit version updates and push them to the remote repository. ```bash git commit -a -m "Prepare for release $TAG" && git push ``` -------------------------------- ### Define Function with No Arguments in Python Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Demonstrates defining a Python function that takes no arguments. Useful for simple utility functions. ```python def func_with_no_args() ``` -------------------------------- ### Add upstream remote Source: https://github.com/allenai/allennlp/blob/main/CONTRIBUTING.md Configure the main AllenNLP repository as an upstream remote to track changes. ```bash git remote add upstream https://github.com/allenai/allennlp.git ``` -------------------------------- ### AllenNLP CLI: train command Source: https://context7.com/allenai/allennlp/llms.txt The `train` command trains a model using a configuration file specifying the model architecture, dataset readers, and training parameters. ```APIDOC ## allennlp train ### Description Trains a model using a configuration file specifying the model architecture, dataset readers, and training parameters. ### Usage ```bash # Basic training allennlp train config.jsonnet -s output_dir # Training with parameter overrides allennlp train config.jsonnet -s output_dir --overrides '{"trainer.num_epochs": 10}' # Resume training from checkpoint allennlp train config.jsonnet -s output_dir --recover # Force overwrite existing output directory allennlp train config.jsonnet -s output_dir --force # Dry run to validate config and show dataset statistics allennlp train config.jsonnet -s output_dir --dry-run ``` ``` -------------------------------- ### Model.load Source: https://context7.com/allenai/allennlp/llms.txt Loads a trained model from a serialization directory or an archive file. ```APIDOC ## Model.load ### Description Loads a trained model from a serialization directory or archive file. ### Parameters #### Request Body - **config** (Params) - Required - Configuration parameters for the model. - **serialization_dir** (str) - Required - Path to the directory containing model files. - **cuda_device** (int) - Optional - Device ID to load the model onto (e.g., 0 for GPU). ### Request Example { "config": "config.json", "serialization_dir": "output/model", "cuda_device": 0 } ``` -------------------------------- ### Class: AnotherClassWithReallyLongConstructor Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Documentation for the AnotherClassWithReallyLongConstructor class, highlighting its long constructor arguments. ```APIDOC ## AnotherClassWithReallyLongConstructor ### Constructor ```python class AnotherClassWithReallyLongConstructor: | def __init__( | self, | a_really_long_argument_name: int = 0, | another_long_name: float = 2, | these_variable_names_are_terrible: str = "yea I know", | **kwargs | ) -> None ``` ``` -------------------------------- ### Train Model Programmatically (Params) Source: https://context7.com/allenai/allennlp/llms.txt Trains a model using `train_model` with a `Params` object for programmatic configuration. Allows modifying parameters before training. ```python from allennlp.commands.train import train_model from allennlp.common import Params # Load params and train params = Params.from_file("config.jsonnet") params["trainer"]["num_epochs"] = 10 model = train_model( params=params, serialization_dir="output/model", force=True, return_model=True ) ``` -------------------------------- ### Implement TextClassifier Model Source: https://context7.com/allenai/allennlp/llms.txt Implement a text classification model by extending the abstract Model class. Ensure the forward method returns a dictionary with at least a 'loss' key for training. Requires vocabulary, embedder, and encoder. ```python from typing import Dict, Optional import torch from allennlp.data import Vocabulary from allennlp.models import Model from allennlp.modules import TextFieldEmbedder, Seq2VecEncoder from allennlp.nn import util from allennlp.training.metrics import CategoricalAccuracy @Model.register("text_classifier") class TextClassifier(Model): def __init__( self, vocab: Vocabulary, embedder: TextFieldEmbedder, encoder: Seq2VecEncoder, num_labels: int = None, ) -> None: super().__init__(vocab) self.embedder = embedder self.encoder = encoder num_labels = num_labels or vocab.get_vocab_size("labels") self.classifier = torch.nn.Linear(encoder.get_output_dim(), num_labels) self.accuracy = CategoricalAccuracy() self.loss = torch.nn.CrossEntropyLoss() def forward( self, tokens: Dict[str, torch.Tensor], label: torch.Tensor = None, ) -> Dict[str, torch.Tensor]: # Embed and encode embedded = self.embedder(tokens) mask = util.get_text_field_mask(tokens) encoded = self.encoder(embedded, mask) # Classify logits = self.classifier(encoded) probs = torch.softmax(logits, dim=-1) output = {"logits": logits, "probs": probs} if label is not None: output["loss"] = self.loss(logits, label) self.accuracy(logits, label) return output def get_metrics(self, reset: bool = False) -> Dict[str, float]: return {"accuracy": self.accuracy.get_metric(reset)} ``` -------------------------------- ### Access AllenNLP Model Components Source: https://context7.com/allenai/allennlp/llms.txt Access the model, configuration, and dataset readers from a loaded archive object. ```python model = archive.model config = archive.config dataset_reader = archive.dataset_reader validation_dataset_reader = archive.validation_dataset_reader ``` -------------------------------- ### Evaluate Model with AllenNLP CLI Source: https://context7.com/allenai/allennlp/llms.txt Use the `allennlp evaluate` command to evaluate a trained model. Supports specifying CUDA device, output files, and prediction outputs. ```bash # Basic evaluation allennlp evaluate model.tar.gz test_data.jsonl ``` ```bash # Evaluation on GPU with output file allennlp evaluate model.tar.gz test_data.jsonl --cuda-device 0 --output-file metrics.json ``` ```bash # Evaluation with predictions output allennlp evaluate model.tar.gz test_data.jsonl --predictions-output-file predictions.jsonl ``` ```bash # Evaluate multiple files allennlp evaluate model.tar.gz "data1.jsonl,data2.jsonl" --output-file "out1.json,out2.json" ``` -------------------------------- ### Function: func_with_no_args Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Documentation for the func_with_no_args function, which takes no arguments. ```APIDOC ## func_with_no_args ### Description This function has no args. ### Method ```python def func_with_no_args() ``` ``` -------------------------------- ### Create Custom Predictor Source: https://context7.com/allenai/allennlp/llms.txt Defines a custom predictor by subclassing Predictor and registering it. Includes methods for prediction and instance conversion. ```python from allennlp.predictors import Predictor from allennlp.common.util import JsonDict from allennlp.data import Instance @Predictor.register("sentiment_classifier") class SentimentClassifierPredictor(Predictor): def predict(self, text: str) -> JsonDict: return self.predict_json({"text": text}) def _json_to_instance(self, json_dict: JsonDict) -> Instance: text = json_dict["text"] return self._dataset_reader.text_to_instance(text) def predict_instance(self, instance: Instance) -> JsonDict: outputs = super().predict_instance(instance) # Add human-readable label label_idx = outputs["probs"].argmax() outputs["label"] = self._model.vocab.get_token_from_index( label_idx, namespace="labels" ) return outputs ``` -------------------------------- ### Predict with AllenNLP CLI Source: https://context7.com/allenai/allennlp/llms.txt Use the `allennlp predict` command for bulk predictions on an input file. Supports batch processing, using dataset readers, and silent mode. ```bash # Basic prediction allennlp predict model.tar.gz input.jsonl --output-file predictions.jsonl ``` ```bash # Prediction with batch processing on GPU allennlp predict model.tar.gz input.jsonl --batch-size 32 --cuda-device 0 ``` ```bash # Use dataset reader for input processing allennlp predict model.tar.gz input.txt --use-dataset-reader ``` ```bash # Silent mode (no stdout output) allennlp predict model.tar.gz input.jsonl --output-file predictions.jsonl --silent ``` -------------------------------- ### Reset Local Tags After Failed Release Source: https://github.com/allenai/allennlp/blob/main/RELEASE_PROCESS.md Delete local tags and re-fetch from the remote to recover from a failed release process. ```bash git tag -l | xargs git tag -d && git fetch -t ``` -------------------------------- ### Load AllenNLP Model Source: https://context7.com/allenai/allennlp/llms.txt Load a trained AllenNLP model from a serialization directory or an archive file. Specify the CUDA device if needed. Use `Model.load` for directories and `Model.from_archive` for tar.gz files. ```python from allennlp.models import Model from allennlp.common import Params # Load model from serialization directory config = Params.from_file("output/model/config.json") model = Model.load( config=config, serialization_dir="output/model", cuda_device=0 # Load onto GPU ) # Load from archive file model = Model.from_archive("model.tar.gz") model.eval() # Make predictions outputs = model.forward_on_instance(instance) ``` -------------------------------- ### Vocabulary API Source: https://context7.com/allenai/allennlp/llms.txt The Vocabulary class maps strings to integers for model input/output. It supports building vocabularies from instances, loading from files, and creating from pretrained transformers. ```APIDOC ## Vocabulary API ### Vocabulary The Vocabulary class maps strings to integers for model input/output. ```python from allennlp.data import Vocabulary, Instance from allennlp.data.fields import TextField, LabelField # Build vocabulary from instances instances = [instance1, instance2, instance3] vocab = Vocabulary.from_instances( instances, min_count={"tokens": 2}, # Minimum count for tokens max_vocab_size={"tokens": 10000} # Maximum vocabulary size ) # Check vocabulary size print(f"Token vocab size: {vocab.get_vocab_size('tokens')}") print(f"Label vocab size: {vocab.get_vocab_size('labels')}") # Convert tokens to indices token_index = vocab.get_token_index("hello", namespace="tokens") token = vocab.get_token_from_index(token_index, namespace="tokens") # Save and load vocabulary vocab.save_to_files("vocab_dir") loaded_vocab = Vocabulary.from_files("vocab_dir") # Create vocabulary from pretrained transformer vocab = Vocabulary.from_pretrained_transformer("bert-base-uncased", namespace="tokens") ``` ``` -------------------------------- ### Global Variable: SOME_GLOBAL_VAR Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Documentation for the global variable SOME_GLOBAL_VAR. ```APIDOC ## SOME_GLOBAL_VAR ### Description This is a global var. ### Value ```python SOME_GLOBAL_VAR = "Ahhhh I'm a global var!!" ``` ``` -------------------------------- ### AllenNLP CLI: evaluate command Source: https://context7.com/allenai/allennlp/llms.txt The `evaluate` command evaluates a trained model against a dataset and reports metrics. ```APIDOC ## allennlp evaluate ### Description Evaluates a trained model against a dataset and reports metrics. ### Usage ```bash # Basic evaluation allennlp evaluate model.tar.gz test_data.jsonl # Evaluation on GPU with output file allennlp evaluate model.tar.gz test_data.jsonl --cuda-device 0 --output-file metrics.json # Evaluation with predictions output allennlp evaluate model.tar.gz test_data.jsonl --predictions-output-file predictions.jsonl # Evaluate multiple files allennlp evaluate model.tar.gz "data1.jsonl,data2.jsonl" --output-file "out1.json,out2.json" ``` ``` -------------------------------- ### Run unit tests with pytest Source: https://github.com/allenai/allennlp/blob/main/CONTRIBUTING.md Execute specific test modules locally using pytest. ```bash pytest -v tests/nn/util_test.py ``` -------------------------------- ### Evaluate Model from Archive File Source: https://context7.com/allenai/allennlp/llms.txt Use the Evaluator to assess model performance on a given dataset and save metrics and predictions. ```python from allennlp.evaluation import Evaluator from allennlp.commands.evaluate import evaluate_from_archive # Evaluate from archive file metrics = evaluate_from_archive( archive_file="model.tar.gz", input_file="data/test.jsonl", metrics_output_file="metrics.json", predictions_output_file="predictions.jsonl", cuda_device=0, batch_size=32 ) print(f"Test accuracy: {metrics['accuracy']:.4f}") print(f"Test loss: {metrics['loss']:.4f}") ``` -------------------------------- ### Define Class Method in Python Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Demonstrates defining a method within a Python class. This method does not perform any action and returns None. ```python class SomeClass: | ... | def some_method(self) -> None ``` -------------------------------- ### DataLoader API Source: https://context7.com/allenai/allennlp/llms.txt MultiProcessDataLoader provides efficient data loading with multiprocessing and batching. It supports shuffling, worker processes, and memory-efficient loading. ```APIDOC ## DataLoader API ### MultiProcessDataLoader Efficient data loading with multiprocessing and batching. ```python from allennlp.data import DataLoader, Vocabulary from allennlp.data.data_loaders import MultiProcessDataLoader from allennlp.data.samplers import BucketBatchSampler # Create data loader data_loader = MultiProcessDataLoader( reader=reader, data_path="data/train.jsonl", batch_size=32, shuffle=True, num_workers=4, # Number of data loading processes max_instances_in_memory=10000 # For memory-efficient loading ) # Index with vocabulary data_loader.index_with(vocab) # Iterate over batches for batch in data_loader: tokens = batch["tokens"] labels = batch["label"] # tokens is a dict of tensors ready for the model ``` ``` -------------------------------- ### Define Class with Long Constructor in Python Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Shows a Python class with a constructor that has multiple arguments, including keyword arguments. This demonstrates handling long argument lists. ```python class AnotherClassWithReallyLongConstructor: | def __init__( | self, | a_really_long_argument_name: int = 0, | another_long_name: float = 2, | these_variable_names_are_terrible: str = "yea I know", | **kwargs | ) -> None ``` -------------------------------- ### Read Data Instances Source: https://context7.com/allenai/allennlp/llms.txt Reads all instances from a JSONL file into a list. For large datasets, iterate directly over the reader for lazy loading. ```python instances = list(reader.read("data/train.jsonl")) ``` ```python for instance in reader.read("data/train.jsonl"): print(instance.fields["tokens"]) ``` -------------------------------- ### Python API: train_model Source: https://context7.com/allenai/allennlp/llms.txt Trains a model using a Params object directly for programmatic configuration. ```APIDOC ## train_model ### Description Trains a model using a Params object directly for programmatic configuration. ### Method ```python from allennlp.commands.train import train_model from allennlp.common import Params # Load params and train params = Params.from_file("config.jsonnet") params["trainer"]["num_epochs"] = 10 model = train_model( params=params, serialization_dir="output/model", force=True, return_model=True ) ``` ``` -------------------------------- ### Create Conda Virtual Environment Source: https://github.com/allenai/allennlp/blob/main/README.md Creates a new Conda environment named 'allennlp_env' with Python 3.8. This isolates AllenNLP and its dependencies from other Python projects. ```bash conda create -n allennlp_env python=3.8 ``` -------------------------------- ### Class: ClassWithDecorator Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Documentation for the ClassWithDecorator class, which uses a decorator. ```APIDOC ## ClassWithDecorator ### Description ```python @dataclass class ClassWithDecorator ``` ### Class Variables #### x ```python class ClassWithDecorator: | ... | x: int = None ``` ``` -------------------------------- ### AllenNLP CLI: predict command Source: https://context7.com/allenai/allennlp/llms.txt The `predict` command runs a trained model against a JSON-lines input file for bulk predictions. ```APIDOC ## allennlp predict ### Description Runs a trained model against a JSON-lines input file for bulk predictions. ### Usage ```bash # Basic prediction allennlp predict model.tar.gz input.jsonl --output-file predictions.jsonl # Prediction with batch processing on GPU allennlp predict model.tar.gz input.jsonl --batch-size 32 --cuda-device 0 # Use dataset reader for input processing allennlp predict model.tar.gz input.txt --use-dataset-reader # Silent mode (no stdout output) allennlp predict model.tar.gz input.jsonl --output-file predictions.jsonl --silent ``` ``` -------------------------------- ### Activate Conda Virtual Environment Source: https://github.com/allenai/allennlp/blob/main/README.md Activates the 'allennlp_env' Conda environment. You must activate this environment in each terminal session where you intend to use AllenNLP. ```bash conda activate allennlp_env ``` -------------------------------- ### Trainer API Source: https://context7.com/allenai/allennlp/llms.txt The GradientDescentTrainer is the main trainer class for supervised learning with gradient descent. It handles training loops, validation, and early stopping. ```APIDOC ## Trainer API ### GradientDescentTrainer The main trainer class for supervised learning with gradient descent. ```python from allennlp.training import GradientDescentTrainer from allennlp.training.optimizers import AdamOptimizer from allennlp.training.learning_rate_schedulers import LinearWithWarmup from allennlp.data import DataLoader # Create trainer trainer = GradientDescentTrainer( model=model, optimizer=AdamOptimizer(model.parameters(), lr=1e-4), data_loader=train_loader, validation_data_loader=val_loader, num_epochs=10, patience=3, # Early stopping patience validation_metric="+accuracy", # Maximize accuracy serialization_dir="output/model", cuda_device=0, grad_norm=1.0, # Gradient clipping learning_rate_scheduler=LinearWithWarmup( optimizer=optimizer, num_epochs=10, num_steps_per_epoch=len(train_loader), warmup_steps=100 ) ) # Train metrics = trainer.train() print(f"Best validation accuracy: {metrics['best_validation_accuracy']}") ``` ``` -------------------------------- ### Document Class Field in Python Dataclass Source: https://github.com/allenai/allennlp/blob/main/scripts/tests/py2md/basic_example_expected_output.md Shows how to document a field within a Python dataclass. The field `x` is typed as an integer and initially set to None. ```python class ClassWithDecorator: | ... | x: int = None ```