Package: relbert
RelBERTWe release the packagerelbertthat includes the official implementation ofDistilling Relation Embeddings from Pre-trained Language Models(https://aclanthology.org/2021.emnlp-main.712/)
that has been accepted by theEMNLP 2021 main conferenceWhat's RelBERT?RelBERT is a state-of-the-art lexical relation embedding model (i.e. model representing any word pair such as "Paris-France" as a fixed-length vector) based on large-scale pretrained masked language models. RelBERT also establishes a very strong baseline to solve analogies in a zero-shot transfer fashion and even outperform strong few-shot models such asGPT-3andAnalogical Proportion (AP).SAT (full)SATU2U4GoogleBATSGloVe48.947.846.539.89668.7FastText49.747.84340.796.672RELATIVE24.924.632.527.16239pair2vec33.734.125.428.266.653.8GPT-2 (AP)41.435.941.244.980.463.5RoBERTa (AP)49.642.449.149.190.869.7GPT-2 (tuned AP)57.856.750.949.595.281.2RoBERTa (tuned AP)55.853.458.357.493.678.4GPT3 (zeroshot)53.7-----GPT3 (fewshot)65.2-----RelBERT72.272.765.865.394.279.3We also report the performance of RelBERT universal relation embeddings on lexical relation classification datasets, which reinforces the capability of RelBERT to model relations.
All datasets are public and available in the following links:analogy questions,lexical relation classification.
Please have a look our paper to know more about RelBERT andAnalogyToolorAP paperfor more information about the datasets.What can we do withrelbert?In this repository, we release a python packagerelbertto work around with RelBERT and its checkpoints viahuggingface modelhubandgensim.
In brief, what you can do with therelbertis summarized as below:Get a high quality embedding vectorgiven a pair of wordGet similar word pairs (nearest neighbors)Reproduce the resultsof our EMNLP 2021 paper.Get StartedpipinstallrelbertPlay with RelBERTRelBERT can give you a high-quality relation embedding vector of a word pair. First, you need to define the model class with a RelBERT checkpoint.fromrelbertimportRelBERTmodel=RelBERT()Then you give a word pair to the model to get the embedding.# the vector has (1024,)v_tokyo_japan=model.get_embedding(['Tokyo','Japan'])Let's run a quick experiment to check the embedding quality. Given candidate lists['Paris', 'France'],['music', 'pizza'], and['London', 'Tokyo'], the pair which shares
the same relation with the['Tokyo', 'Japan']is['Paris', 'France']. Would the RelBERT embedding be possible to retain it with simple cosine similarity?fromrelbertimportcosine_similarityv_paris_france,v_music_pizza,v_london_tokyo=model.get_embedding([['Paris','France'],['music','pizza'],['London','Tokyo']])cosine_similarity(v_tokyo_japan,v_paris_france)>>>0.999cosine_similarity(v_tokyo_japan,v_music_pizza)>>>0.991cosine_similarity(v_tokyo_japan,v_london_tokyo)>>>0.996Bravo! The distance between['Tokyo', 'Japan']and['Paris', 'France']is the closest among the candidates.
In fact, this pipeline is how we evaluate the RelBERT on the analogy question.Nearest Neighbours of RelBERTTo get the similar word pairs in terms of the RelBERT embedding, we convert the RelBERT embedding to a gensim model file with a fixed vocabulary.
Specifically, we take the vocabulary of theRELATIVE embeddingthat is released as a part ofAnalogy Tool, and generate the embedding for all the word pairs with RelBERT (asahi417/relbert-roberta-large).
Following the original vocabulary representation, words are joined by__and multiple token should be combined by_such asNew_york__Tokyo.The RelBERT embedding gensim file can be foundhere. For example, you can get the nearest neighbours as below.fromgensim.modelsimportKeyedVectorsmodel=KeyedVectors.load_word2vec_format('gensim_model.bin',binary=True)model.most_similar('Tokyo__Japan')>>>[('Moscow__Russia',0.9997282028198242),('Cairo__Egypt',0.9997045993804932),('Baghdad__Iraq',0.9997043013572693),('Helsinki__Finland',0.9996970891952515),('Paris__France',0.999695897102356),('Damascus__Syria',0.9996891617774963),('Bangkok__Thailand',0.9996803998947144),('Madrid__Spain',0.9996673464775085),('Budapest__Hungary',0.9996543526649475),('Beijing__China',0.9996539354324341)]CitationIf you use any of these resources, please cite the followingpaper:@inproceedings{ushio-etal-2021-distilling,
    title = "Distilling Relation Embeddings from Pretrained Language Models",
    author = "Ushio, Asahi  and
      Camacho-Collados, Jose  and
      Schockaert, Steven",
    booktitle = "Proceedings of the 2021 Conference on Empirical Methods in Natural Language Processing",
    month = nov,
    year = "2021",
    address = "Online and Punta Cana, Dominican Republic",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2021.emnlp-main.712",
    doi = "10.18653/v1/2021.emnlp-main.712",
    pages = "9044--9062",
    abstract = "Pre-trained language models have been found to capture a surprisingly rich amount of lexical knowledge, ranging from commonsense properties of everyday concepts to detailed factual knowledge about named entities. Among others, this makes it possible to distill high-quality word vectors from pre-trained language models. However, it is currently unclear to what extent it is possible to distill relation embeddings, i.e. vectors that characterize the relationship between two words. Such relation embeddings are appealing because they can, in principle, encode relational knowledge in a more fine-grained way than is possible with knowledge graphs. To obtain relation embeddings from a pre-trained language model, we encode word pairs using a (manually or automatically generated) prompt, and we fine-tune the language model such that relationally similar word pairs yield similar output vectors. We find that the resulting relation embeddings are highly competitive on analogy (unsupervised) and relation classification (supervised) benchmarks, even without any task-specific fine-tuning. Source code to reproduce our experimental results and the model checkpoints are available in the following repository: https://github.com/asahi417/relbert",
}