Code Monkey home page Code Monkey logo

zero-shot-bert-adapters's Introduction

Zero-shot-BERT-adapters

Repository containing code for Zero-shot-BERT-adapters (Z-BERT-A), a two-stage method for multilingual intent discovery relying on a Transformer architecture, fine-tuned with Adapters, which is initially trained for Natural Language Inference (NLI) and later applied for unknown intent classification in a zero-shot setting.

Deployment example of an use-case of Zero-shot-BERT-adapters pipeline

image

Important information

This repository makes use of the module Adapter Transformers.
The wget tool is required for this package, for Windows users please make sure to install it accordingly before running this module.

Installation

In order to install Zero-shot-BERT-adapters it is just needed to execute the following pip command which will make sure everything is accordingly installed. Zero-shot-BERT-adapters uses spaCy with 'en_core_web_trf', this package will try to install it automatically itself if not present but make sure you have it installed through the suggested way of spaCy.

pip install git+https://github.com/GT4SD/zero-shot-bert-adapters.git

Usage information

In order to reproduce the results here there is the sample code which can also be found in an example Jupyter Notebook.

import torch
from zberta.data.data_snli import DataSNLI
from zberta.data.data_banking import DataBanking
import zberta.model.model as model
from zberta.train.trainer import Trainer
from zberta.intent_discovery.unknown_intents import unknown_intents_set
from zberta.intent_discovery.zberta import ZBERTA

model_name = "bert-base-uncased"
training = False
testing = False
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if training:
    snli = DataSNLI(model_name, device)
    train_iterator, valid_iterator, test_iterator = snli.iterators()
    berta = model.instantiate_model(snli.labels(), snli.output_dim(), device, model_name, snli.nli_labels())
    trainer = Trainer(berta, train_iterator, valid_iterator, test_iterator, device)
    trainer.start_training()
    if testing:
        trainer.start_testing()
z_banking = DataBanking(model_name, device)
z_dataset = z_banking.z_iterator()
z_intents = unknown_intents_set(z_dataset['test']['text'])
berta = model.instantiate_model(z_banking.labels(), z_banking.output_dim(), device, model_name,
                                z_banking.nli_labels(), path="model.pt", dict=True)
zberta = ZBERTA(berta, model_name, z_dataset['test']['text'], z_intents, z_dataset['test']['category'])
z_acc = zberta.zero_shot_intents()
print(z_acc)

For simple usage of the Zero-shot-BERT-adapters pipeline instead it's just needed to load the pipeline and model through this simple code which encapsulate all the complexity behind.

import torch
from zberta.intent_discovery.zberta import ZBERTA
import zberta.model.model as model

model_name = "bert-base-uncased"
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
berta = model.instantiate_model(2, 2, device, model_name,
                                {"entailment": 0, "contradiction": 1}, path="model.pt", dict=True)
zberta = ZBERTA(berta, model_name)

As example run:

zberta.find_new_intents(["I want to buy a book but I lost all my money, where can I make a withdrawal?"])
Output: ['make withdrawal']

zero-shot-bert-adapters's People

Contributors

christofid avatar comidan avatar drugilsberg avatar gilpasternak 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

zero-shot-bert-adapters's Issues

Training Issue

Hi, Thank you for your making this pipeline open source. I really appreciate that.

I am having an issue in BERTA instance while training. Please find the error and the code.

image

image

Any solution/advice would be really appreciated. Thanks

Need to train the model on any other dataset other than Banking

Hi @comidan, Thanks again for making this pipeline open-source.
I have two concerns.

  1. Whether the model is using both datasets (SNLI and Banking77-OOS) or it’s only using SNLI for the training? if yes, then can you please point out where and how is it used in the code?

  2. It is related to the 1st one as well. Actually, I need to train the BERT-A model with any other dataset other than Banking77-OOS to generate unknown intents. Can you please guide me on how to do that?

Model download failed

The model link you provided cannot be accessed at all? Can you directly feed the model into the code? Or explain what kind of pre trained model is useless? Thank you.

Error while running sample code on Colab

---> 15 z_intents = unknown_intents_set("en_core_web_trf", z_dataset['test']['text'])
16 berta = model.instantiate_model(z_banking.labels(), z_banking.output_dim(), device, model_name,
17 z_banking.nli_labels(), path="model.pt", dict=True)

/usr/local/lib/python3.7/dist-packages/zberta/intent_discovery/unknown_intents.py in unknown_intents_set(dataset, spacy_model)
15 def unknown_intents_set(dataset, spacy_model=None):
16 global nlp_engine
---> 17 if spacy_model is not None and spacy_model not in default_spacy:
18 os.system("python -m spacy download " + spacy_model + " --no-deps")
19 nlp_engine = spacy.load(spacy_model)

TypeError: 'in ' requires string as left operand, not list

Error when running the example in README

After installing all I run the example

model_name = "bert-base-uncased"
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
berta = model.instantiate_model(2, 2, device, model_name,
                                {"entailment": 0, "contradiction": 1}, path="model.pt", dict=True)

and I get error

AttributeError: 'BertModel' object has no attribute 'add_adapter'

Python version: Python 3.10.6
OS Version: `MacOS Monterey 12.6'

Failing to install using poetry

I tried to install the package on a clean env using the following commands:

poetry add  git+https://github.com/GT4SD/zberta.git

and

poetry add git+ssh://[email protected]/github.com/GT4SD/zberta.git

and I getting the following error:

CalledProcessError

  Command '['git', 'clone', '--recurse-submodules', '--', 'ssh://[email protected]/github.com/GT4SD/zberta.git', '/var/folders/5t/lgg8r5ws1xbdm8pp6kh37ytm0000gn/T/pypoetry-git-zbertaeirek49a']' returned non-zero exit status 128.

  at /usr/local/Cellar/poetry/1.1.15/libexec/lib/python3.10/site-packages/poetry/utils/_compat.py:217 in run
      213│                 process.wait()
      214│                 raise
      215│             retcode = process.poll()
      216│             if check and retcode:
    → 217│                 raise CalledProcessError(
      218│                     retcode, process.args, output=stdout, stderr=stderr
      219│                 )
      220│         finally:
      221│             # None because our context manager __exit__ does not use them.

python version: 3.9 & 3.10
os version: MacOS Monterey 12.6
poetry version: 1.1.15

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.