Git Product home page Git Product logo

riccorl / transformer-srl Goto Github PK

View Code? Open in Web Editor NEW
69.0 7.0 9.0 1.24 MB

Reimplementation of a BERT based model (Shi et al, 2019), currently the state-of-the-art for English SRL. This model implements also predicate disambiguation.

Python 42.51% Shell 2.43% Jsonnet 5.82% Perl 49.25%
propbank bert shi dataset srl transformer srltagger srl-annotations nlp natural-language-processing

transformer-srl's Introduction

Upload Python Package Code style: black

Semantic Role Lableing with BERT

Semantic Role Labeling based on AllenNLP implementation of Shi et al, 2019. Can be trained using both PropBank and VerbAtlas inventories and implements also the predicate disambiguation task, in addition to arguments identification and disambiguation.

How to use

Install the library

pip install transformer-srl

Pre-trained model

You can also use a pre-trained model. To use it, first install the correct version of transformer-srl:

pip install transformer-srl==2.4.6

then download the pretrained model srl_bert_base_conll2012.tar.gz from here.

File Model Version F1 Argument F1 Predicate
srl_bert_base_conll2012.tar.gz bert-base-cased 2.4.6 86.0 95.5

CLI

echo '{"sentence": "Did Uriah honestly think he could beat the game in under three hours?"}' | \
allennlp predict path/to/srl_bert_base_conll2012.tar.gz - --include-package transformer_srl

Inside Python Code

from transformer_srl import dataset_readers, models, predictors

predictor = predictors.SrlTransformersPredictor.from_path("path/to/srl_bert_base_conll2012.tar.gz, "transformer_srl")
predictor.predict(
  sentence="Did Uriah honestly think he could beat the game in under three hours?"
)

Infos

  • Language Model: BERT
  • Dataset: CoNLL 2012

Results with VerbAtlas

With bert-base-cased:

# Dev set
- F1 arguments 87.6
- F1 predicates 95.5
# Test set
- F1 arguments x
- F1 predicates x

With bert-base-multilingual-cased:

# Dev set
- F1 arguments 86.2
- F1 predicates 94.2
# Test set
- F1 arguments 86.1
- F1 predicates 94.9

To-Dos

  • Works with both PropBank and VerbAtlas (infer inventory from dataset reader)
  • Compatibility with all models from Huggingface's Transformers. - Now works only with models that accept 1 as token type id
  • Predicate identification (without using spacy)

transformer-srl's People

Contributors

riccorl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

transformer-srl's Issues

VerbAtlas

Hi @Riccorl ,

Really appreciate the ease of access of the model. Do you plan to upload a pre-trained model that predicts with verbatlas annotations?

Best,
Zoher

RuntimeError: Error loading state dict for TransformerSrlSpan

On running the example in README.md, getting the following error:

RuntimeError: Error loading state dict for TransformerSrlSpan
        Missing keys: ['transformer.embeddings.position_ids']
        Unexpected keys: []

(Model downloaded from the Dropbox link mentioned)

Missing verb mappings

HI @Riccorl ,

In the pb2va file, there are many verbs that are predicted by the model but are not mapped to va. Do you have any suggestion to handle those situations?

Some examples are : naturalize.01, re-release.01, deadlock.01, fame.01, fit.02, fame.01, forecast.01, abbreviate.01, father.01

Error Loading State Script

I pip-installed in a fresh conda environment (on a CentOS 7 system), downloaded the pre-trained model, and followed the instructions in the readme file.

I got these results:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-3-bfc9a5474abe> in <module>
      1 srl_model_path = Path('resources', 'srl_bert_base_conll2012.tar.gz')
----> 2 predictor = predictors.SrlTransformersPredictor.from_path(srl_model_path, 'transformer-srl')

~/.conda/envs/pogrep4/lib/python3.9/site-packages/transformer_srl/predictors.py in from_path(cls, archive_path, predictor_name, cuda_device, dataset_reader_to_load, frozen, import_plugins, language, restrict_frames, restrict_roles)
    157             plugins.import_plugins()
    158         return SrlTransformersPredictor.from_archive(
--> 159             load_archive(archive_path, cuda_device=cuda_device),
    160             predictor_name,
    161             dataset_reader_to_load=dataset_reader_to_load,

~/.conda/envs/pogrep4/lib/python3.9/site-packages/allennlp/models/archival.py in load_archive(archive_file, cuda_device, overrides, weights_file)
    206             config.duplicate(), serialization_dir
    207         )
--> 208         model = _load_model(config.duplicate(), weights_path, serialization_dir, cuda_device)
    209     finally:
    210         if tempdir is not None:

~/.conda/envs/pogrep4/lib/python3.9/site-packages/allennlp/models/archival.py in _load_model(config, weights_path, serialization_dir, cuda_device)
    240 
    241 def _load_model(config, weights_path, serialization_dir, cuda_device):
--> 242     return Model.load(
    243         config,
    244         weights_file=weights_path,

~/.conda/envs/pogrep4/lib/python3.9/site-packages/allennlp/models/model.py in load(cls, config, serialization_dir, weights_file, cuda_device)
    404             # get_model_class method, that recurses whenever it finds a from_archive model type.
    405             model_class = Model
--> 406         return model_class._load(config, serialization_dir, weights_file, cuda_device)
    407 
    408     def extend_embedder_vocab(self, embedding_sources_mapping: Dict[str, str] = None) -> None:

~/.conda/envs/pogrep4/lib/python3.9/site-packages/allennlp/models/model.py in _load(cls, config, serialization_dir, weights_file, cuda_device)
    346 
    347         if unexpected_keys or missing_keys:
--> 348             raise RuntimeError(
    349                 f"Error loading state dict for {model.__class__.__name__}\n\t"
    350                 f"Missing keys: {missing_keys}\n\t"

RuntimeError: Error loading state dict for TransformerSrlSpan
	Missing keys: ['transformer.embeddings.position_ids']
	Unexpected keys: []

Cannot download model from drop-box

Could the pre-trained model be uploaded to another location please. Dropbox and google drive etc. are blocked due to corporate policy. Or could the url to the specific original model from Allen NLP be provided. Then you may just upload the extra files from the core models from Allen NLP on github.

Training_Dependency_Based_Model

The config jsonnet for the bert-base model (bert_base_dep.jsonnet) has the following lines:

"model": {
"type": "transformer_srl_span",
"embedding_dropout": 0.1,
"bert_model": "bert-base-cased",
},

which should be transformer_srl_dependency??? When changing to dependency running and training of the model results in an ERROR

When trying to train a model based on the bert_tiny_dep.jsonnet (which has the correct model-type) training also results in an ERROR, stating:

TypeError: forward() got an unexpected keyword argument 'sentence_end'

spaCy errors encountered while trying out transformer-srl library on Win 10, Python 3.8

I successfully installed transformers-srl library using my Jupyter-notebook terminal. However, I encountered the following error while trying to run the following code. I am using a Windows 10 system with Python- 3.8.

code:

from transformer_srl import dataset_readers, models, predictors

predictor = predictors.SrlTransformersPredictor.from_path("D:/srl_bert_base_conll2012.tar.gz", "transformer_srl")
predictor.predict(sentence="Did Uriah honestly think he could beat the game in under three hours?")

error with spacy version- 2.2.4

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-1-27de09498ecb> in <module>
      1 from transformer_srl import dataset_readers, models, predictors
      2 
----> 3 predictor = predictors.SrlTransformersPredictor.from_path("D:/srl_bert_base_conll2012.tar.gz", "transformer_srl")
      4 predictor.predict(sentence="Did Uriah honestly think he could beat the game in under three hours?")

~\anaconda3\lib\site-packages\transformer_srl\predictors.py in from_path(cls, archive_path, predictor_name, cuda_device, dataset_reader_to_load, frozen, import_plugins, language, restrict_frames, restrict_roles)
    153         if import_plugins:
    154             plugins.import_plugins()
--> 155         return SrlTransformersPredictor.from_archive(
    156             load_archive(archive_path, cuda_device=cuda_device),
    157             predictor_name,

~\anaconda3\lib\site-packages\transformer_srl\predictors.py in from_archive(cls, archive, predictor_name, dataset_reader_to_load, frozen, language, restrict_frames, restrict_roles)
    197             model.eval()
    198 
--> 199         return predictor_class(model, dataset_reader, language)

~\anaconda3\lib\site-packages\transformer_srl\predictors.py in __init__(self, model, dataset_reader, language)
     20         self, model: Model, dataset_reader: DatasetReader, language: str = "en_core_web_sm",
     21     ) -> None:
---> 22         super().__init__(model, dataset_reader, language)
     23 
     24     @staticmethod

~\anaconda3\lib\site-packages\allennlp_models\structured_prediction\predictors\srl.py in __init__(self, model, dataset_reader, language)
     22     ) -> None:
     23         super().__init__(model, dataset_reader)
---> 24         self._tokenizer = SpacyTokenizer(language=language, pos_tags=True)
     25 
     26     def predict(self, sentence: str) -> JsonDict:

~\anaconda3\lib\site-packages\allennlp\data\tokenizers\spacy_tokenizer.py in __init__(self, language, pos_tags, parse, ner, keep_spacy_tokens, split_on_spaces, start_tokens, end_tokens)
     61         end_tokens: Optional[List[str]] = None,
     62     ) -> None:
---> 63         self.spacy = get_spacy_model(language, pos_tags, parse, ner)
     64         if split_on_spaces:
     65             self.spacy.tokenizer = _WhitespaceSpacyTokenizer(self.spacy.vocab)

~\anaconda3\lib\site-packages\allennlp\common\util.py in get_spacy_model(spacy_model_name, pos_tags, parse, ner)
    273             disable.append("ner")
    274         try:
--> 275             spacy_model = spacy.load(spacy_model_name, disable=disable)
    276         except OSError:
    277             logger.warning(

~\AppData\Roaming\Python\Python38\site-packages\spacy\__init__.py in load(name, **overrides)
     28     if depr_path not in (True, False, None):
     29         deprecation_warning(Warnings.W001.format(path=depr_path))
---> 30     return util.load_model(name, **overrides)
     31 
     32 

~\AppData\Roaming\Python\Python38\site-packages\spacy\util.py in load_model(name, **overrides)
    162             return load_model_from_link(name, **overrides)
    163         if is_package(name):  # installed as package
--> 164             return load_model_from_package(name, **overrides)
    165         if Path(name).exists():  # path to model data directory
    166             return load_model_from_path(Path(name), **overrides)

~\AppData\Roaming\Python\Python38\site-packages\spacy\util.py in load_model_from_package(name, **overrides)
    183     """Load a model from an installed package."""
    184     cls = importlib.import_module(name)
--> 185     return cls.load(**overrides)
    186 
    187 

~\anaconda3\lib\site-packages\en_core_web_sm\__init__.py in load(**overrides)
      8 
      9 def load(**overrides):
---> 10     return load_model_from_init_py(__file__, **overrides)

~\AppData\Roaming\Python\Python38\site-packages\spacy\util.py in load_model_from_init_py(init_file, **overrides)
    226     if not model_path.exists():
    227         raise IOError(Errors.E052.format(path=path2str(data_path)))
--> 228     return load_model_from_path(data_path, meta, **overrides)
    229 
    230 

~\AppData\Roaming\Python\Python38\site-packages\spacy\util.py in load_model_from_path(model_path, meta, **overrides)
    207             config = meta.get("pipeline_args", {}).get(name, {})
    208             factory = factories.get(name, name)
--> 209             component = nlp.create_pipe(factory, config=config)
    210             nlp.add_pipe(component, name=name)
    211     return nlp.from_disk(model_path, exclude=disable)

~\AppData\Roaming\Python\Python38\site-packages\spacy\language.py in create_pipe(self, name, config)
    298                 raise KeyError(Errors.E108.format(name=name))
    299             else:
--> 300                 raise KeyError(Errors.E002.format(name=name))
    301         factory = self.factories[name]
    302         return factory(self, **config)

KeyError: "[E002] Can't find factory for 'tok2vec'. This usually happens when spaCy calls `nlp.create_pipe` with a component name that's not built in - for example, when constructing the pipeline from a model's meta.json. If you're using a custom component, you can write to `Language.factories['tok2vec']` or remove it from the model meta and add it via `nlp.add_pipe` instead."

I tried to upgrade to spacy-3.1.1, but got the following error:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-27de09498ecb> in <module>
----> 1 from transformer_srl import dataset_readers, models, predictors
      2 
      3 predictor = predictors.SrlTransformersPredictor.from_path("D:/srl_bert_base_conll2012.tar.gz", "transformer_srl")
      4 predictor.predict(sentence="Did Uriah honestly think he could beat the game in under three hours?")

~\anaconda3\lib\site-packages\transformer_srl\dataset_readers.py in <module>
     12 from allennlp.data.tokenizers import Token
     13 from allennlp_models.common.ontonotes import Ontonotes, OntonotesSentence
---> 14 from allennlp_models.structured_prediction import SrlReader
     15 from conllu import parse_incr
     16 from nltk import Tree

~\anaconda3\lib\site-packages\allennlp_models\structured_prediction\__init__.py in <module>
      1 # flake8: noqa: F403
----> 2 from allennlp_models.structured_prediction.predictors import *
      3 from allennlp_models.structured_prediction.dataset_readers import *
      4 from allennlp_models.structured_prediction.metrics import *
      5 from allennlp_models.structured_prediction.models import *

~\anaconda3\lib\site-packages\allennlp_models\structured_prediction\predictors\__init__.py in <module>
      2     BiaffineDependencyParserPredictor,
      3 )
----> 4 from allennlp_models.structured_prediction.predictors.constituency_parser import (
      5     ConstituencyParserPredictor,
      6 )

~\anaconda3\lib\site-packages\allennlp_models\structured_prediction\predictors\constituency_parser.py in <module>
      3 from overrides import overrides
      4 from nltk import Tree
----> 5 from spacy.lang.en.tag_map import TAG_MAP
      6 
      7 from allennlp.common.util import JsonDict, sanitize

ModuleNotFoundError: No module named 'spacy.lang.en.tag_map'

I am not sure, how to resolve the issue. Kindly help.
Any help is deeply appreciated. Thanks in advance.

Runtime Error

Thanks for sharing your code. It is really helpful. However, I face runtime error during execution.
Please help me with the following error message
RuntimeError: Error loading state dict for TransformerSrlSpan
Missing keys: ['transformer.embeddings.position_ids']
Unexpected keys: []

Thanks in advance.

AllenNLP Broken?

Hi. I came across your repository after having discovered that somewhere down the line, AllenNLP stopped returning reasonable results. Since you depend partly on AllenNLP, I am curious if you have encountered issues with simple sentences. For example, "This is a dog" returns no results whatsoever. "To be" and "To have" both seem bugged. Results also seem to be worse in general than what I remember. I was planning to try your implementation, but I don't see where to find a model to use for SRL / propbank.

The issue I posted over on AllenNLP's repository is here: allenai/allennlp#4818 .

In all honesty, I have no idea why this is happening. I have tried on multiple machines, older and newer versions and models. It seems something has changed.

Would you have any insight into this? I am hoping this issue can be resolved soon, as I depend on BERT/AllenNLP for a major project.

Error after upgrading to v2.4.6

@Riccorl , hi!

I have upgraded to https://github.com/Riccorl/transformer-srl/archive/2.4.6.tar.gz and have downloaded the pretrained model srl_bert_base_conll2012.tar.gz from https://www.dropbox.com/s/4tes6ypf2do0feb/srl_bert_base_conll2012.tar.gz
but now I am getting the following error during initialization:

2021-01-11 09:17:25 INFO     allennlp.nn.initializers  -    transformer.encoder.layer.9.output.dense.weight
I0111 09:17:25.121117 140505917486912 initializers.py:508]    transformer.pooler.dense.bias
2021-01-11 09:17:25 INFO     allennlp.nn.initializers  -    transformer.pooler.dense.bias
I0111 09:17:25.121154 140505917486912 initializers.py:508]    transformer.pooler.dense.weight
2021-01-11 09:17:25 INFO     allennlp.nn.initializers  -    transformer.pooler.dense.weight
I0111 09:17:25.315172 140505917486912 archival.py:211] removing temporary unarchived model dir at /tmp/tmp7vamph68
2021-01-11 09:17:25 INFO     allennlp.models.archival  - removing temporary unarchived model dir at /tmp/tmp7vamph68
Traceback (most recent call last):
  File "/home/dev/.local/bin/rasa", line 10, in <module>
    sys.exit(main())
  File "/home/dev/.local/lib/python3.6/site-packages/rasa/__main__.py", line 76, in main
    cmdline_arguments.func(cmdline_arguments)
  File "/home/dev/.local/lib/python3.6/site-packages/rasa/cli/run.py", line 88, in run
    rasa.run(**vars(args))
  File "/home/dev/.local/lib/python3.6/site-packages/rasa/run.py", line 33, in run
    import rasa.core.run
  File "/home/dev/.local/lib/python3.6/site-packages/rasa/core/run.py", line 22, in <module>
    from rasa.server import add_root_route
  File "/home/dev/.local/lib/python3.6/site-packages/rasa/server.py", line 76, in <module>
    srlPredictor = predictors.SrlTransformersPredictor.from_path("~/srl_bert_base_conll2012.tar.gz", "transformer_srl")
  File "/home/dev/nluFiles/transformer_srl/predictors.py", line 156, in from_path
    load_archive(archive_path, cuda_device=cuda_device),
  File "/home/dev/.local/lib/python3.6/site-packages/allennlp/models/archival.py", line 208, in load_archive
    model = _load_model(config.duplicate(), weights_path, serialization_dir, cuda_device)
  File "/home/dev/.local/lib/python3.6/site-packages/allennlp/models/archival.py", line 246, in _load_model
    cuda_device=cuda_device,
  File "/home/dev/.local/lib/python3.6/site-packages/allennlp/models/model.py", line 406, in load
    return model_class._load(config, serialization_dir, weights_file, cuda_device)
  File "/home/dev/.local/lib/python3.6/site-packages/allennlp/models/model.py", line 326, in _load
    missing_keys, unexpected_keys = model.load_state_dict(model_state, strict=False)
  File "/home/dev/.local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 1052, in load_state_dict
    self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for TransformerSrlSpan:
        size mismatch for frame_projection_layer.weight: copying a param with shape torch.Size([5497, 768]) from checkpoint, the shape in current model is torch.Size([5929, 768]).
        size mismatch for frame_projection_layer.bias: copying a param with shape torch.Size([5497]) from checkpoint, the shape in current model is torch.Size([5929]).

What am I missing?

Pre-trained model for allennlp >= 2.0

The current pre-trained model uses v2.4.6, which requires allennlp<1.3,>=1.2.

Has anybody successfully trained the model for the new version of allennnlp? Would really appreciate the help.

Error while loading model

When i try the Example or my own model i get the following Error while loading the model:

predictor = predictors.SrlTransformersPredictor.from_path("/path/to/model/srl_bert_base_conll2012.tar.gz", "transformer_srl")

==> for my dependency-model, for the example it changes to transformer_srl_span

allennlp.common.checks.ConfigurationError: transformer_srl_dependency not in acceptable choices for dataset_reader.type: ['babi', 'conll2003', 'interleaving', 'multitask', 'sequence_tagging', 'sharded', 'text_classification_json', 'multitask_shim', 'ptb_trees', 'semantic_dependencies', 'srl', 'universal_dependencies', 'sst_tokens', 'coref', 'preco', 'winobias', 'masked_language_modeling', 'next_token_lm', 'simple_language_modeling', 'copynet_seq2seq', 'seq2seq', 'cnn_dm', 'swag', 'commonsenseqa', 'piqa', 'fake', 'quora_paraphrase', 'snli', 'drop', 'qangaroo', 'quac', 'squad', 'squad1', 'squad2', 'transformer_squad', 'triviaqa', 'ccgbank', 'conll2000', 'ontonotes_ner', 'gqa', 'vqav2', 'visual-entailment']. You should either use the --include-package flag to make sure the correct module is loaded, or use a fully qualified class name in your config file like {"model": "my_module.models.MyModel"} to have it imported automatically.

Error_when_training_own_model

I would like to train my own model based on https://github.com/UniversalPropositions/UP-1.0/tree/master/UP_English-EWT
I got everything up running, but when loading the dataset from the connlu files i get this Error:

AssertionError: Found no field that needed padding; we are surprised you got this error, please open an issue on github

My Code:

====================================================================================
#!/bin/bash

#Home-Path:
HOME="/path/to/home"

#Dataset:
DATASET="$HOME/path/to/dir/with/conllu"

export SRL_TRAIN_DATA_PATH="$DATASET/en_ewt-up-train.conllu"
export SRL_VALIDATION_DATA_PATH="$DATASET/en_ewt-up-dev.conllu"

CONFIG="path/to/training_config/bert_base_dep.jsonnet"

/path/to/python3 -m allennlp train $CONFIG -s models/up_ewt_test --include-package transformer_srl

====================================================================================

any guesses?:)

Predicting is returning none

The code to predict runs but it always returns none.
I used python 3.11 and transformer-srl 2.4.6. This should work right?

How to run the model on GPU?

Hi, thanks for your nice work.
I have many sentences needed to be parsed, could you tell me how to parse the sentence with this model on GPU.

Run dependency-based SRL

Hello, thanks for your bert-based implementation of SRL.

Can you please provide the script for running the code on dependency-based SRL (ConLL 2009)? and is there any pretrained model? Thanks

How do you train the model on VerbAtlas?

Hi Thank you very much for this wonderful package and your encouraging responses on the discussions in this repo! I have downloaded the package and retained model and it runs perfect.

I was not sure if this is an appropriate place to ask this, but I wonder how you trained the model on VerbAtlas. Looks like the pretrained model is trained on PropBank, but you I found that you have also tried on VerbAtlas (if I am not mistaken). I have downloaded VerbAtlas resources from here, but I did not find sentence annotations. Could you provide me with some insights how you would go about this? In the mean time (when it turns out there is no publicly available annotations with VerbAtlas), I am thinking to look up VerbAtlas frames using the predicate ids returned by this package...

Thank you very much, and I apologize if this is off-topic!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.