Lexical Substitution Framework

Overview

LexSubGen

Lexical Substitution Framework

This repository contains the code to reproduce the results from the paper:

Arefyev Nikolay, Sheludko Boris, Podolskiy Alexander, Panchenko Alexander, "Always Keep your Target in Mind: Studying Semantics and Improving Performance of Neural Lexical Substitution", Proceedings of the 28th International Conference on Computational Linguistics, 2020

Installation

Clone LexSubGen repository from github.com.

git clone https://github.com/Samsung/LexSubGen
cd LexSubGen

Setup anaconda environment

  1. Download and install conda
  2. Create new conda environment
    conda create -n lexsubgen python=3.7.4
  3. Activate conda environment
    conda activate lexsubgen
  4. Install requirements
    pip install -r requirements.txt
  5. Download spacy resources and install context2vec and word_forms from github repositories
    ./init.sh

Setup Web Application

If you do not plan to use the Web Application, skip this section and go to the next!

  1. Download and install NodeJS and npm.
  2. Run script for install dependencies and create build files.
bash web_app_setup.sh

Install lexsubgen library

python setup.py install

Results

Results of the lexical substitution task are presented in the following table. To reproduce them, follow the instructions above to install the correct dependencies.

Model SemEval COINCO
GAP P@1 P@3 R@10 GAP P@1 P@3 R@10
OOC 44.65 16.82 12.83 18.36 46.3 19.58 15.03 12.99
C2V 55.82 7.79 5.92 11.03 48.32 8.01 6.63 7.54
C2V+embs 53.39 28.01 21.72 33.52 50.73 29.64 24.0 21.97
ELMo 53.66 11.58 8.55 13.88 49.47 13.58 10.86 11.35
ELMo+embs 54.16 32.0 22.2 31.82 52.22 35.96 26.62 23.8
BERT 54.42 38.39 27.73 39.57 50.5 42.56 32.64 28.73
BERT+embs 53.87 41.64 30.59 43.88 50.85 46.05 35.63 31.67
RoBERTa 56.74 32.25 24.26 36.65 50.82 35.12 27.35 25.41
RoBERTa+embs 58.74 43.19 31.19 44.61 54.6 46.54 36.17 32.1
XLNet 59.12 31.75 22.83 34.95 53.39 38.16 28.58 26.47
XLNet+embs 59.62 49.53 34.9 47.51 55.63 51.5 39.92 35.12

Results reproduction

Here we list XLNet reproduction commands that correspond to the results presented in the table above. Reproduction commands for all models you can find in scripts/lexsub-all-models.sh Besides saving to the 'run-directory' all results are saved using mlflow. To check them you can run mlflow ui in LexSubGen directory and then open the web page in a browser.

Also you can use pytest to check the reproducibility. But it may take a long time:

pytest tests/results_reproduction
  • XLNet:

XLNet Semeval07:

python lexsubgen/evaluations/lexsub.py solve --substgen-config-path configs/subst_generators/lexsub/xlnet.jsonnet --dataset-config-path configs/dataset_readers/lexsub/semeval_all.jsonnet --run-dir='debug/lexsub-all-models/semeval_all_xlnet' --force --experiment-name='lexsub-all-models' --run-name='semeval_all_xlnet'

XLNet CoInCo:

python lexsubgen/evaluations/lexsub.py solve --substgen-config-path configs/subst_generators/lexsub/xlnet.jsonnet --dataset-config-path configs/dataset_readers/lexsub/coinco.jsonnet --run-dir='debug/lexsub-all-models/coinco_xlnet' --force --experiment-name='lexsub-all-models' --run-name='coinco_xlnet'

XLNet with embeddings similarity Semeval07:

python lexsubgen/evaluations/lexsub.py solve --substgen-config-path configs/subst_generators/lexsub/xlnet_embs.jsonnet --dataset-config-path configs/dataset_readers/lexsub/semeval_all.jsonnet --run-dir='debug/lexsub-all-models/semeval_all_xlnet_embs' --force --experiment-name='lexsub-all-models' --run-name='semeval_all_xlnet_embs'

XLNet with embeddings similarity CoInCo:

python lexsubgen/evaluations/lexsub.py solve --substgen-config-path configs/subst_generators/lexsub/xlnet_embs.jsonnet --dataset-config-path configs/dataset_readers/lexsub/coinco.jsonnet --run-dir='debug/lexsub-all-models/coinco_xlnet_embs' --force --experiment-name='lexsub-all-models' --run-name='coinco_xlnet_embs'

Word Sense Induction Results

Model SemEval 2013 SemEval 2010
AVG AVG
XLNet 33.4 52.1
XLNet+embs 37.3 54.1

To reproduce these results use 2.3.0 version of transformers and the following command:

bash scripts/wsi.sh

Web application

You could use command line interface to run Web application.

# Run main server
lexsubgen-app run --host HOST 
                  --port PORT 
                  [--model-configs CONFIGS] 
                  [--start-ids START-IDS] 
                  [--start-all] 
                  [--restore-session]

Example:

# Run server and serve models BERT and XLNet. 
# For BERT create server for serving model and substitute generator instantly (load resources in memory).
# For XLNet create only server.
lexsubgen-app run --host '0.0.0.0' 
                  --port 5000 
                  --model-configs '["my_cool_configs/bert.jsonnet", "my_awesome_configs/xlnet.jsonnet"]' 
                  --start-ids '[0]'

# After shutting down server JSON file with session dumps in the '~/.cache/lexsubgen/app_session.json'.
# The content of this file looks like:
# [
#     'my_cool_configs/bert.jsonnet',
#     'my_awesome_configs/xlnet.jsonnet',
# ]
# You can restore it with flag 'restore-session'
lexsubgen-app run --host '0.0.0.0' 
                  --port 5000 
                  --restore-session
# BERT and XLNet restored now
Arguments:
Argument Default Description
--help Show this help message and exit
--host IP address of running server host
--port 5000 Port for starting the server
--model-configs [] List of file paths to the model configs.
--start-ids [] Zero-based indices of served models for which substitute generators will be created
--start-all False Whether to create substitute generators for all served models
--restore-session False Whether to restore session from previous Web application run

FAQ

  1. How to use gpu? - You can use environment variable CUDA_VISIBLE_DEVICES to use gpu for inference: export CUDA_VISIBLE_DEVICES='1' or CUDA_VISIBLE_DEVICES='1' before your command.
  2. How to run tests? - You can use pytest: pytest tests
You might also like...
An Open Source Machine Learning Framework for Everyone
An Open Source Machine Learning Framework for Everyone

Documentation TensorFlow is an end-to-end open source platform for machine learning. It has a comprehensive, flexible ecosystem of tools, libraries, a

Intel® Nervana™ reference deep learning framework committed to best performance on all hardware

DISCONTINUATION OF PROJECT. This project will no longer be maintained by Intel. Intel will not provide or guarantee development of or support for this

A modular active learning framework for Python
A modular active learning framework for Python

Modular Active Learning framework for Python3 Page contents Introduction Active learning from bird's-eye view modAL in action From zero to one in a fe

🔥 Cogitare - A Modern, Fast, and Modular Deep Learning and Machine Learning framework for Python
🔥 Cogitare - A Modern, Fast, and Modular Deep Learning and Machine Learning framework for Python

Cogitare is a Modern, Fast, and Modular Deep Learning and Machine Learning framework for Python. A friendly interface for beginners and a powerful too

A Sklearn-like Framework for Hyperparameter Tuning and AutoML in Deep Learning projects. Finally have the right abstractions and design patterns to properly do AutoML. Let your pipeline steps have hyperparameter spaces. Enable checkpoints to cut duplicate calculations. Go from research to production environment easily. A Comparative Framework for Multimodal Recommender Systems
A Comparative Framework for Multimodal Recommender Systems

Cornac Cornac is a comparative framework for multimodal recommender systems. It focuses on making it convenient to work with models leveraging auxilia

A unified framework for machine learning with time series

Welcome to sktime A unified framework for machine learning with time series We provide specialized time series algorithms and scikit-learn compatible

A framework for joint super-resolution and image synthesis, without requiring real training data
A framework for joint super-resolution and image synthesis, without requiring real training data

SynthSR This repository contains code to train a Convolutional Neural Network (CNN) for Super-resolution (SR), or joint SR and data synthesis. The met

An open framework for Federated Learning.
An open framework for Federated Learning.

Welcome to Intel® Open Federated Learning Federated learning is a distributed machine learning approach that enables organizations to collaborate on m

Comments
  • Errors in setting up the code base

    Errors in setting up the code base

    Thanks for the great work and making it open source. I was trying to set up the code and follow this example, however, while setting it up, I am encountering this error:

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-12-2ebbdaa05245> in <module>()
          1 # Loading substitute generator
          2 sg = SubstituteGenerator.from_config(
    ----> 3     str(CONFIGS_PATH / "subst_generators" / "lexsub" / "xlnet_embs.jsonnet")
          4 )
    
    19 frames
    /content/LexSubGen/lexsubgen/subst_generator.py in from_config(cls, config_path)
         96             object of the SubstituteGenerator class.
         97         """
    ---> 98         subst_generator, _ = build_from_config_path(config_path)
         99         return subst_generator
        100 
    
    /content/LexSubGen/lexsubgen/utils/params.py in build_from_config_path(config_path, config)
        194     params = Params(config)
        195 
    --> 196     return build_from_params(params), config
        197 
        198 
    
    /content/LexSubGen/lexsubgen/utils/params.py in build_from_params(params)
        236                 item = build_from_params(item_params)
        237             elif isinstance(item_params, list):
    --> 238                 item = [build_from_params(elem_params) for elem_params in item_params]
        239             else:
        240                 item = item_params
    
    /content/LexSubGen/lexsubgen/utils/params.py in <listcomp>(.0)
        236                 item = build_from_params(item_params)
        237             elif isinstance(item_params, list):
    --> 238                 item = [build_from_params(elem_params) for elem_params in item_params]
        239             else:
        240                 item = item_params
    
    /content/LexSubGen/lexsubgen/utils/params.py in build_from_params(params)
        226     if "class_name" in params:
        227         cls_name = params.pop("class_name")
    --> 228         cls = clsname2cls(cls_name)
        229 
        230         # init params acquisition
    
    /content/LexSubGen/lexsubgen/utils/params.py in clsname2cls(clsname)
        203         import_path += "." + module_path
        204     # try:
    --> 205     module = importlib.import_module(import_path)
        206     cls = getattr(module, clsname)
        207     # except Exception as e:
    
    /usr/lib/python3.7/importlib/__init__.py in import_module(name, package)
        125                 break
        126             level += 1
    --> 127     return _bootstrap._gcd_import(name[level:], package, level)
        128 
        129 
    
    /usr/lib/python3.7/importlib/_bootstrap.py in _gcd_import(name, package, level)
    
    /usr/lib/python3.7/importlib/_bootstrap.py in _find_and_load(name, import_)
    
    /usr/lib/python3.7/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)
    
    /usr/lib/python3.7/importlib/_bootstrap.py in _load_unlocked(spec)
    
    /usr/lib/python3.7/importlib/_bootstrap_external.py in exec_module(self, module)
    
    /usr/lib/python3.7/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)
    
    /content/LexSubGen/lexsubgen/post_processors/target_excluder.py in <module>()
          8 
          9 
    ---> 10 class TargetExcluder(PostProcessor):
         11     def __init__(self, lemmatizer: Optional[str] = None, use_pos_tag: bool = True):
         12         """
    
    /content/LexSubGen/lexsubgen/post_processors/target_excluder.py in TargetExcluder()
         30         target_words: List[str],
         31         target_pos: Optional[List[str]] = None,
    ---> 32     ) -> Tuple[np.ndarray, Dict[str, int]]:
         33         """
         34         Abstract method that transforms prob estimator predictions.
    
    /usr/local/lib/python3.7/dist-packages/overrides/overrides.py in overrides(method, check_signature, check_at_runtime)
         86     """
         87     if method is not None:
    ---> 88         return _overrides(method, check_signature, check_at_runtime)
         89     else:
         90         return functools.partial(
    
    /usr/local/lib/python3.7/dist-packages/overrides/overrides.py in _overrides(method, check_signature, check_at_runtime)
        112                 return wrapper  # type: ignore
        113             else:
    --> 114                 _validate_method(method, super_class, check_signature)
        115                 return method
        116     raise TypeError(f"{method.__qualname__}: No super class method found")
    
    /usr/local/lib/python3.7/dist-packages/overrides/overrides.py in _validate_method(method, super_class, check_signature)
        133         and not isinstance(super_method, property)
        134     ):
    --> 135         ensure_signature_is_compatible(super_method, method, is_static)
        136 
        137 
    
    /usr/local/lib/python3.7/dist-packages/overrides/signature.py in ensure_signature_is_compatible(super_callable, sub_callable, is_static)
         93         ensure_return_type_compatibility(super_type_hints, sub_type_hints, method_name)
         94         ensure_all_kwargs_defined_in_sub(
    ---> 95             super_sig, sub_sig, super_type_hints, sub_type_hints, is_static, method_name
         96         )
         97         ensure_all_positional_args_defined_in_sub(
    
    /usr/local/lib/python3.7/dist-packages/overrides/signature.py in ensure_all_kwargs_defined_in_sub(super_sig, sub_sig, super_type_hints, sub_type_hints, check_first_parameter, method_name)
        154             ):
        155                 raise TypeError(
    --> 156                     f"`{method_name}: {name} must be a supertype of `{super_param.annotation}` but is `{sub_param.annotation}`"
        157                 )
        158 
    
    TypeError: `TargetExcluder.transform: target_words must be a supertype of `typing.Union[typing.List[str], NoneType]` but is `typing.List[str]`
    

    I made sure that the environment and versions are the same as suggested in the repo. Any help would be appreciated. Thanks

    opened by agoel00 3
  • Adding support for other huggingface models

    Adding support for other huggingface models

    In the example listed here, is it possible to use other models from the huggingface models hub to generate lexical substitutes? I am happy to contribute more models to the repo once I understand the pipeline of adding new models.

    Also, which approach from the paper does this example correspond to? Is it the XLNet+embs approach listed in bold in this table from the paper? Screenshot 2021-10-12 at 11 20 40 AM

    opened by agoel00 2
  • CVE-2007-4559 Patch

    CVE-2007-4559 Patch

    Patching CVE-2007-4559

    Hi, we are security researchers from the Advanced Research Center at Trellix. We have began a campaign to patch a widespread bug named CVE-2007-4559. CVE-2007-4559 is a 15 year old bug in the Python tarfile package. By using extract() or extractall() on a tarfile object without sanitizing input, a maliciously crafted .tar file could perform a directory path traversal attack. We found at least one unsantized extractall() in your codebase and are providing a patch for you via pull request. The patch essentially checks to see if all tarfile members will be extracted safely and throws an exception otherwise. We encourage you to use this patch or your own solution to secure against CVE-2007-4559. Further technical information about the vulnerability can be found in this blog.

    If you have further questions you may contact us through this projects lead researcher Kasimir Schulz.

    opened by TrellixVulnTeam 0
  • Scores order in GAP

    Scores order in GAP

    https://github.com/Samsung/LexSubGen/blob/d3d06b40dcc41475957bccf2dd6b4cd08373e181/lexsubgen/metrics/candidate_ranking_metrics.py#L113

    Could you please explain why we don't need to sort values here to calculate GAP? I believe we need, but it assumed that we give an input dict with the right order of insertion and it works properly. I think it's better to mention this fact in the annotation or to sort values.

    Anyway, thanks for the great results and the interesting article!

    opened by corwinnn 1
Owner
Samsung
Samsung Electronics Co.,Ltd.
Samsung
NAACL2021 - COIL Contextualized Lexical Retriever

COIL Repo for our NAACL paper, COIL: Revisit Exact Lexical Match in Information Retrieval with Contextualized Inverted List. The code covers learning

Luyu Gao 108 Dec 31, 2022
The deployment framework aims to provide a simple, lightweight, fast integrated, pipelined deployment framework that ensures reliability, high concurrency and scalability of services.

savior是一个能够进行快速集成算法模块并支持高性能部署的轻量开发框架。能够帮助将团队进行快速想法验证(PoC),避免重复的去github上找模型然后复现模型;能够帮助团队将功能进行流程拆解,很方便的提高分布式执行效率;能够有效减少代码冗余,减少不必要负担。

Tao Luo 125 Dec 22, 2022
FEDn is an open-source, modular and ML-framework agnostic framework for Federated Machine Learning

FEDn is an open-source, modular and ML-framework agnostic framework for Federated Machine Learning (FedML) developed and maintained by Scaleout Systems. FEDn enables highly scalable cross-silo and cross-device use-cases over FEDn networks.

Scaleout 75 Nov 9, 2022
Caffe: a fast open framework for deep learning.

Caffe Caffe is a deep learning framework made with expression, speed, and modularity in mind. It is developed by Berkeley AI Research (BAIR)/The Berke

Berkeley Vision and Learning Center 33k Dec 28, 2022
Game Agent Framework. Helping you create AIs / Bots that learn to play any game you own!

Serpent.AI - Game Agent Framework (Python) Update: Revival (May 2020) Development work has resumed on the framework with the aim of bringing it into 2

Serpent.AI 6.4k Jan 5, 2023
An Open Source Machine Learning Framework for Everyone

Documentation TensorFlow is an end-to-end open source platform for machine learning. It has a comprehensive, flexible ecosystem of tools, libraries, a

null 170.1k Jan 4, 2023
PyTorchCV: A PyTorch-Based Framework for Deep Learning in Computer Vision.

PyTorchCV: A PyTorch-Based Framework for Deep Learning in Computer Vision @misc{CV2018, author = {Donny You ([email protected])}, howpubl

Donny You 40 Sep 14, 2022
Karate Club: An API Oriented Open-source Python Framework for Unsupervised Learning on Graphs (CIKM 2020)

Karate Club is an unsupervised machine learning extension library for NetworkX. Please look at the Documentation, relevant Paper, Promo Video, and Ext

Benedek Rozemberczki 1.8k Jan 7, 2023
A flexible framework of neural networks for deep learning

Chainer: A deep learning framework Website | Docs | Install Guide | Tutorials (ja) | Examples (Official, External) | Concepts | ChainerX Forum (en, ja

Chainer 5.8k Jan 6, 2023