allennlp | An open-source NLP research library , built on PyTorch | Natural Language Processing library

 by   allenai Python Version: 2.10.1 License: Apache-2.0

kandi X-RAY | allennlp Summary

kandi X-RAY | allennlp Summary

allennlp is a Python library typically used in Institutions, Learning, Education, Artificial Intelligence, Natural Language Processing, Deep Learning, Pytorch applications. allennlp has no bugs, it has no vulnerabilities, it has build file available, it has a Permissive License and it has high support. You can install using 'pip install allennlp' or download it from GitHub, PyPI.

An Apache 2.0 NLP research library, built on PyTorch, for developing state-of-the-art deep learning models on a wide variety of linguistic tasks.
Support
    Quality
      Security
        License
          Reuse

            kandi-support Support

              allennlp has a highly active ecosystem.
              It has 11506 star(s) with 2256 fork(s). There are 284 watchers for this library.
              OutlinedDot
              It had no major release in the last 12 months.
              There are 80 open issues and 2477 have been closed. On average issues are closed in 30 days. There are 11 open pull requests and 0 closed requests.
              It has a positive sentiment in the developer community.
              The latest version of allennlp is 2.10.1

            kandi-Quality Quality

              allennlp has 0 bugs and 0 code smells.

            kandi-Security Security

              allennlp has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
              allennlp code analysis shows 0 unresolved vulnerabilities.
              There are 0 security hotspots that need review.

            kandi-License License

              allennlp is licensed under the Apache-2.0 License. This license is Permissive.
              Permissive licenses have the least restrictions, and you can use them in most projects.

            kandi-Reuse Reuse

              allennlp releases are available to install and integrate.
              Deployable package is available in PyPI.
              Build file is available. You can build the component from source.
              Installation instructions, examples and code snippets are available.
              It has 56841 lines of code, 3687 functions and 596 files.
              It has medium code complexity. Code complexity directly impacts maintainability of the code.

            Top functions reviewed by kandi - BETA

            kandi has reviewed allennlp and discovered the below as its top functions. This is intended to give you an instant insight into allennlp implemented functionality, and help decide if they suit your requirements.
            • Construct a TrainModel from partial objects
            • Construct a new instance of this class
            • Returns the constructor of the wrapped function
            • Instantiate a class from a dictionary
            • Compute the matching between two sentences
            • Compute the multi - perspective matching between two vectors
            • Compute cosine similarity between two vectors
            • Return a tiny value for a given dtype
            • Construct a vocabulary from pretrained pretrained pretrained data
            • Performs an ELMo forward transformation
            • Return a decorator to register a key
            • Compute token embedding
            • Load weights from a given file
            • Select a batch of spans matching the given spans
            • Perform a forward projection
            • Compute the embedding
            • Compute the token embedding
            • Convert a tag sequence into a list of spans
            • Creates embeddings for the given tokens
            • Permute the top k k
            • Create a model from pretrained module
            • Returns a T5StackOutput object
            • Forward computation
            • Performs the forward computation
            • Get a pre - trained model
            • Evaluate a model
            Get all kandi verified functions for this library.

            allennlp Key Features

            No Key Features are available at this moment for allennlp.

            allennlp Examples and Code Snippets

            OntoNotes-5.0-NER
            Pythondot img1Lines of Code : 80dot img1no licencesLicense : No License
            copy iconCopy
            OntoNotes-5.0-NER
              -conll-formatted-ontenotes-5.0/
              -collect_conll.py
              -README.md
              -..
              -onotenotes-release-5.0/
            
            $ conda create --name py27 python=2.7
            $ source activate py27
            
            ./conll-formatted-ontonotes-5.0/scripts/skeleton2conll.sh -D ./ontono  
            <code>SciBERT</code>,Model training,Training new models using AllenNLP
            Pythondot img2Lines of Code : 22dot img2License : Permissive (Apache-2.0)
            copy iconCopy
            ├── ner
            │   ├── JNLPBA
            │   ├── NCBI-disease
            │   ├── bc5cdr
            │   └── sciie
            ├── parsing
            │   └── genia
            ├── pico
            │   └── ebmnlp
            └── text_classification
                ├── chemprot
                ├── citation_intent
                
            Setup
            Pythondot img3Lines of Code : 8dot img3no licencesLicense : No License
            copy iconCopy
            conda create -n allennlp_spacy
            
            source activate allennlp_spacy
            
            pip install http://download.pytorch.org/whl/torch-0.2.0.post3-cp36-cp36m-macosx_10_7_x86_64.whl
            
            python -m spacy download es
            
            pip install -r requirements.txt
            
            python setup.py develop
            
            pi  
            dgl - model utils
            Pythondot img4Lines of Code : 35dot img4License : Non-SPDX (Apache License 2.0)
            copy iconCopy
            import torch as th
            from torch.autograd import Function
            
            
            def batch2tensor(batch_adj, batch_feat, node_per_pool_graph):
                """
                transform a batched graph to batched adjacency tensor and node feature tensor
                """
                batch_size = int(batch_adj.si  
            copy iconCopy
            import ast
            df["OIE Triples"] = df["OIE output"].apply(ast.literal_eval)
            
            df["OIE Triples"] = df["OIE Triples"].apply(lambda val: [a_dict["description"]
                                                                     for a_dict in val["verbs"]])
            d
            Parsing nested dictionary (allen nlp hierplane_tree)
            Pythondot img6Lines of Code : 74dot img6License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            def get_entity_attributes(obj, key, value):
                """Recursively fetch values from nested JSON."""
                arr = []
            
                def extract(obj, arr, key):
                    """Recursively search for values of key in JSON tree."""
                    if isinstance(obj, dict
            TypeError: 'NLP' object is not callable
            Pythondot img7Lines of Code : 4dot img7License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            >>> from nlg.utils import load_spacy_model
            >>> nlp = load_spacy_model()
            >>> text = nlp("The virginica species has the least average sepal_width.")
            
            Read JSON file correctly
            Pythondot img8Lines of Code : 6dot img8License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            def _read(self, file_path):
                with open(cached_path(file_path), "r") as data_file:
                    data = json.load(data_file)
                for item in data:
                    text = item["text"]
            
            PyTorch BERT TypeError: forward() got an unexpected keyword argument 'labels'
            Pythondot img9Lines of Code : 16dot img9License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            def forward(self, input_ids, attention_mask=None, token_type_ids=None,
                        position_ids=None, head_mask=None, labels=None):
            
            Outputs: `Tuple` comprising various elements depending on the configuration (confi
            How to use Elmo word embedding with the original pre-trained model (5.5B) in interactive mode
            Pythondot img10Lines of Code : 5dot img10License : Strong Copyleft (CC BY-SA 4.0)
            copy iconCopy
            elmo = ElmoEmbedder(
                options_file='https://s3-us-west-2.amazonaws.com/allennlp/models/elmo/2x4096_512_2048cnn_2xhighway_5.5B/elmo_2x4096_512_2048cnn_2xhighway_5.5B_options.json', 
                weight_file='https://s3-us-west-2.amazonaws.com/all

            Community Discussions

            QUESTION

            Error training ELMo - RuntimeError: The size of tensor a (5158) must match the size of tensor b (5000) at non-singleton dimension 1
            Asked 2022-Mar-24 at 17:17

            I am trying to train my own custom ELMo model on AllenNLP.

            The following bug RuntimeError: The size of tensor a (5158) must match the size of tensor b (5000) at non-singleton dimension 1 arises when training the model. There are instances where the size of tensor a is stated to be other values (e.g. 5300). When I tested on a small subset of files, I was able to train the model successfully.

            Based on my intuition, this is something that deals with the number of tokens in my model. More specifically specific files which has tokens more than 5000. However, there is no parameter within the AllenNLP package which allows me to tweak this to bypass this error.

            Any advice on how I can overcome this issue? Would tweaking the PyTorch code to set it at a 5000 size work (If yes, how can I do that)? Any insights will be deeply appreciated.

            FYI, I am currently using a customised DatasetReader for tokenisation purposes. I've generated my own vocab list before training the model (to save some time) which is used to train the ELMo model via AllenNLP.

            Update: I found out that there is this variable from AllenNLP max_len=5000 which is why the error is showing. See code here. I've tweaked the parameter to larger values and ended up with CUDA Out of Memory Error on many occasions instead. Making me believe this should not be touched.

            Environment: Python 3.6.9, Linux Ubuntu, allennlp=2.9.1, allennlp-models=2.9.0

            Traceback:

            ...

            ANSWER

            Answered 2022-Mar-24 at 17:17

            By setting the max_tokens variable for the custom DatasetReader built to below 5000, this error no longer persists. This was also suggested by one of AllenNLP's contributor to make sure the tokenizer truncates the input to 5000 tokens.

            Same question was posted on AllenNLP: https://github.com/allenai/allennlp/discussions/5601

            Source https://stackoverflow.com/questions/71514727

            QUESTION

            Loading a HuggingFace model into AllenNLP gives different predictions
            Asked 2022-Mar-13 at 14:56

            I have a custom classification model trained using transformers library based on a BERT model. The model classifies text into 7 different categories. It is persisted in a directory using:

            ...

            ANSWER

            Answered 2022-Mar-11 at 19:55

            As discussed on GitHub: The problem is that you are constructing a 7-way classifier on top of BERT. Even though the BERT model will be identical, the 7-way classifier on top of it is randomly initialized every time.

            BERT itself does not come with a classifier. That has to be fine-tuned for your data.

            Source https://stackoverflow.com/questions/69876688

            QUESTION

            RuntimeError: Error loading state dict for SrlBert Missing keys: ['bert_model.embeddings.position_ids'] Unexpected keys: []
            Asked 2022-Mar-11 at 04:52

            I am just a beginner in NLP and was trying to learn the Semantic role labeling concept through implementation. I was trying to load the bert-base-srl model from the public storage of allennlp. But was facing the following error:

            ...

            ANSWER

            Answered 2022-Mar-11 at 04:52

            If you are on the later versions of allennlp-models, you can use this archive_file instead: https://storage.googleapis.com/allennlp-public-models/structured-prediction-srl-bert.2020.12.15.tar.gz.

            The latest versions of the model archive files can be found on the demo page in the Model Card tab: https://demo.allennlp.org/semantic-role-labeling

            Source https://stackoverflow.com/questions/71432983

            QUESTION

            Allennlp: How to load a pretrained ELMo as the embedding of allennlp model?
            Asked 2022-Feb-24 at 19:15

            I am new in allennlp. I trained an elmo model to apply it to other allennlp models as the embedding but failed. It seems that my model is not compatible to the interface the config gives. What can I do?

            My elmo is trained by allennlp with the command:

            ...

            ANSWER

            Answered 2022-Feb-24 at 19:15

            You are right, those two formats don't align.

            I'm afraid there is no easy way out. I think you'll have to write a TokenEmbedder that can read and apply the output from bidirectional_language_model.jsonnet.

            If you do, we'd love to have it as a contribution to AllenNLP!

            Source https://stackoverflow.com/questions/71182588

            QUESTION

            How to change AllenNLP BERT based Semantic Role Labeling to RoBERTa in AllenNLP
            Asked 2022-Feb-24 at 12:34

            Currently i'm able to train a Semantic Role Labeling model using the config file below. This config file is based on the one provided by AllenNLP and works for the default bert-base-uncased model and also GroNLP/bert-base-dutch-cased.

            ...

            ANSWER

            Answered 2022-Feb-24 at 02:14

            The easiest way to resolve this is to patch SrlReader so that it uses PretrainedTransformerTokenizer (from AllenNLP) or AutoTokenizer (from Huggingface) instead of BertTokenizer. SrlReader is an old class, and was written against an old version of the Huggingface tokenizer API, so it's not so easy to upgrade.

            If you want to submit a pull request in the AllenNLP project, I'd be happy to help you get it merged into AllenNLP!

            Source https://stackoverflow.com/questions/71223907

            QUESTION

            How to interpret Allen NLP Coreference resolution model output?
            Asked 2022-Feb-10 at 16:15

            I am working on extracting people and tasks from texts (multiple sentences) and need a way to resolve coreferencing. I found this model, and it seems very promising, but once I installed the required libraries allennlp and allennlp_models and testing the model out for myself I got:

            Script:

            ...

            ANSWER

            Answered 2022-Feb-10 at 16:15

            The information you are looking for is in 'clusters', where each list corresponds to an entity. Within each entity list, you will find the mentions referring to the same entity. The number are indices that mark the beginning and ending of each coreferential mention. E.g. Paul Allen [0,1] and Allen [24, 24].

            Source https://stackoverflow.com/questions/70786812

            QUESTION

            Google mT5-small configuration error because number attention heads is not divider of model dimension
            Asked 2022-Jan-20 at 09:48

            The configuration file for the HuggingFace google/mt5-small Model (https://huggingface.co/google/mt5-small)

            defines

            ...

            ANSWER

            Answered 2022-Jan-20 at 09:48

            This is a very good question, and shows a common misconception about Transformers, stemming from an (unfortunate) formulation in the original Transformers paper. In particular, the authors write the following in Section 3.2.2:

            In this work, we employ h = 8 parallel attention layers, or heads. For each of these we use d_k = d_v = d_(model) / h = 64. [...]

            Note that the equality of d_k/d_v = d_(model) is not strictly necessary; it is only important that you do match the final hidden representation (d_(model)) after the Feed-Forward portion of each layer. Specifically for mt5-small, the authors actually use an internal dimension of 384 which is simply the product of parameters d_kv * num_heads = 64 * 6.

            Now, the problem is that many libraries make a similar assumption of the enforced relation between d_kv and d_(model), because it saves some implementation effort that most people won't use anyways. I suspect (not super familiar with AllenNLP) that they have made similar assumptions here, which is why you cannot load the model.

            Also, to clarify this, here is a peek at the modules of a loaded mt5-small:

            Source https://stackoverflow.com/questions/70769151

            QUESTION

            Writing custom metrics in allennlp
            Asked 2021-Dec-10 at 02:12

            I'm writing down my first allennlp project to detect specific spans in newspaper articles. I was able to have it train on my dataset. The loss computed with cross entropy seems to decrease correctly, but I'm having some issues with my metric. I wrote a custom metric which is supposed to give an estimate of how accurate my model predicts spans according to some ground truth spans. The problem is that right now, our metric doesn't seem to update correctly even though the loss is decreasing.

            I'm not sure how to tackle the problem and guess my questions are the following:

            1. What is the exact use of the reset() function in the Metric class ?
            2. Apart from writing the __call__(), get_metric() and reset() function, are there other things to watch out for?

            Below is a snapshot of my custom Metric class in case you need it.

            ...

            ANSWER

            Answered 2021-Dec-10 at 02:12

            During training, the trainer will call the metric (using Metric.__call__()) with the results from every batch. The metric is supposed to update its internal state when this happens. The trainer expects to get the current value(s) of the metric when it calls Metric.get_metric(). Metric.reset() has to reset the metric into a state as if it had never been called before. When get_metric() gets called with reset = True, it's expected to reset the metric as well.

            From what I can tell, your code does all these things correctly. Your code will not run correctly in a distributed setting, but if you are not training on multiple GPUs, that's not a problem.

            What you're doing is similar to the SQuAD metric: https://github.com/allenai/allennlp-models/blob/main/allennlp_models/rc/metrics/squad_em_and_f1.py The SQuAD metric goes out of its way to call the original SQuAD evaluation code, so it's a little more complicated than what you would want, but maybe you can adapt it? The main difference would be that you are calculating F scores across the whole dataset, while SQuAD calculates them per-document, and then averages across documents.

            Finally, you can write a simple test for your metric, similar to the SQuAD test: https://github.com/allenai/allennlp-models/blob/main/tests/rc/metrics/squad_em_and_f1_test.py That might help narrow down where the problem is.

            Source https://stackoverflow.com/questions/70273872

            QUESTION

            Using multiprocessing with AllenNLP decoding is sluggish compared to non-multiprocessing case
            Asked 2021-Dec-01 at 15:18

            I'm using the AllenNLP (version 2.6) semantic role labeling model to process a large pile of sentences. My Python version is 3.7.9. I'm on MacOS 11.6.1. My goal is to use multiprocessing.Pool to parallelize the work, but the calls via the pool are taking longer than they do in the parent process, sometimes substantially so.

            In the parent process, I have explicitly placed the model in shared memory as follows:

            ...

            ANSWER

            Answered 2021-Dec-01 at 15:18

            Turns out that I wasn't comparing exactly the right things. This thread: https://github.com/allenai/allennlp/discussions/5471 goes into all the detail. Briefly, because pytorch can use additional resources under the hood, my baseline test without multiprocessing wasn't taxing my computer enough when running two instances in parallel; I had to run 4 instances to see the penalty, and in that case, the total processing time was essentially the same for 4 parallel nonmultiprocessing invocations, or one multiprocessing case with 4 subprocesses.

            Source https://stackoverflow.com/questions/70008621

            QUESTION

            How to incorporate ELMo into the simple classification of AllenNLP Guide
            Asked 2021-Nov-12 at 23:10

            I am a beginner and not a native English speaker, so I may ask poor questions.Sorry!

            I recently finished the official AllenNLP tutorial(https://guide.allennlp.org/training-and-prediction) and want to change the simple classifier's word embedding to ELMo.

            Also, I want to make the architecture of the simple classifier more complex to increase its accuracy. I think I'm done with the implementation of the model.

            simple_classifier.py

            ...

            ANSWER

            Answered 2021-Nov-12 at 23:10

            Check out the way the BiDAF model uses ELMo: https://raw.githubusercontent.com/allenai/allennlp-models/main/training_config/rc/bidaf_elmo.jsonnet

            You can steal some of the components of that config. You will need the token embedder under the name "elmo", and, I believe, both the token indexers under "tokens" and "elmo".

            It should work without having to write any code.

            Source https://stackoverflow.com/questions/69879808

            Community Discussions, Code Snippets contain sources that include Stack Exchange Network

            Vulnerabilities

            No vulnerabilities reported

            Install allennlp

            If you're interested in using AllenNLP for model development, we recommend you check out the AllenNLP Guide for a thorough introduction to the library, followed by our more advanced guides on GitHub Discussions.
            If you want to use allennlp train and config files to specify experiments, use this template. We recommend this approach.
            If you'd prefer to use python code to configure your experiments and run your training loop, use this template. There are a few things that are currently a little harder in this setup (loading a saved model, and using distributed training), but otherwise it's functionality equivalent to the config files setup.
            Hyperparameter optimization for AllenNLP using Optuna
            Training with multiple GPUs in AllenNLP
            Training on larger batches with less memory in AllenNLP
            How to upload transformer weights and tokenizers from AllenNLP to HuggingFace
            AllenNLP requires Python 3.6.1 or later and PyTorch. We support AllenNLP on Mac and Linux environments. We presently do not support Windows but are open to contributions.

            Support

            ↗️ Website🔦 Guide🖼 Gallery💻 Demo📓 Documentation ( latest | stable | commit )⬆️ Upgrade Guide from 1.x to 2.0❓ Stack Overflow✋ Contributing Guidelines🤖 Officially Supported Models Pretrained Models Documentation ( latest | stable | commit )⚙️ Continuous Build🌙 Nightly Releases
            Find more information at:

            Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

            Find more libraries
            Install
          • PyPI

            pip install allennlp

          • CLONE
          • HTTPS

            https://github.com/allenai/allennlp.git

          • CLI

            gh repo clone allenai/allennlp

          • sshUrl

            git@github.com:allenai/allennlp.git

          • Stay Updated

            Subscribe to our newsletter for trending solutions and developer bootcamps

            Agree to Sign up and Terms & Conditions

            Share this Page

            share link