Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: Multi-Task Learning with use_all_task #3453

Open
EmbedCrafter opened this issue May 10, 2024 · 1 comment
Open

[Question]: Multi-Task Learning with use_all_task #3453

EmbedCrafter opened this issue May 10, 2024 · 1 comment
Labels
question Further information is requested

Comments

@EmbedCrafter
Copy link

Question

How can I correctly train two tasks simultaneously on a single corpus using the parameter use_all_task=True? When I attempted to train two models together on one corpus, I encountered a RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn.

multitask_dataset = CONLL_03_DUTCH()
tasks = ['ner', 'pos']
model_1 = initialize_tagger(multitask_dataset, shared_embedding, tasks[0])
model_2 = initialize_tagger(multitask_dataset, shared_embedding, tasks[1])
multitask_model = MultitaskModel([model_1, model_2], use_all_tasks=True, task_ids=tasks)
trainer = ModelTrainer(multitask_model, multitask_dataset)
trainer.fine_tune('resources/taggers/sota-ner-flert',
learning_rate=5.0e-6,
max_epochs=20)
╭─────────────────────────────────────── Traceback (most recent call last) ───────────────────────────────────────╮
│ in :60 │
│ │
│ ❱ 60 trainer.fine_tune('resources/taggers/sota-ner-flert', │
│ │
│ /pyzr/active_venv/lib/python3.10/site-packages/flair/trainers/trainer.py:253 in fine_tune │
│ │
│ ❱ 253 │ │ return self.train_custom( │
│ │
│ /pyzr/active_venv/lib/python3.10/site-packages/flair/trainers/trainer.py:606 in train_custom │
│ │
│ ❱ 606 │ │ │ │ │ │ │ self._backward(scaler.scale(loss)) │
│ │
│ /pyzr/active_venv/lib/python3.10/site-packages/flair/trainers/trainer.py:124 in _backward │
│ │
│ ❱ 124 │ │ loss.backward() │
│ │
│ /pyzr/active_venv/lib/python3.10/site-packages/torch/_tensor.py:487 in backward │
│ │
│ ❱ 487 │ │ torch.autograd.backward( │
│ │
│ /pyzr/active_venv/lib/python3.10/site-packages/torch/autograd/init.py:200 in backward │
│ │
│ ❱ 200 │ Variable._execution_engine.run_backward( # Calls into the C++ engine to run the bac │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

@EmbedCrafter EmbedCrafter added the question Further information is requested label May 10, 2024
@fkdosilovic
Copy link
Contributor

If you look at the docs, you'll see you need to explicitly add a special multitask_id label type. Here's a minimal working example using the CONLL_03_DUTCH dataset:

from flair.datasets import CONLL_03_DUTCH
from flair.embeddings import TransformerWordEmbeddings
from flair.models import SequenceTagger, MultitaskModel
from flair.trainers import ModelTrainer

corpus = CONLL_03_DUTCH()

# Adding the special label type to make use_all_tasks=True work.
for corpus_split in (corpus.train, corpus.dev, corpus.test):
    for sent in corpus_split:
        sent.add_label("multitask_id", "task_ner")
        sent.add_label("multitask_id", "task_pos")
        
# Preparing the label dictionary for each task.
ner_label_dict = corpus.make_label_dictionary("ner")
pos_label_dict = corpus.make_label_dictionary("pos")

# Preparing the shared embeddings and tagger for each task.
shared_embeddings = TransformerWordEmbeddings(
    model="FacebookAI/roberta-base",
    layers="-1",
    subtoken_pooling="first",
    fine_tune=True,
    use_context=False,
)

ner_tagger_model = SequenceTagger(
    hidden_size=256,
    embeddings=shared_embeddings,
    tag_dictionary=ner_label_dict,
    tag_type="ner",
    use_crf=False,
    use_rnn=False,
    reproject_embeddings=False,
)

pos_tagger_model = SequenceTagger(
    hidden_size=256,
    embeddings=shared_embeddings,
    tag_dictionary=pos_label_dict,
    tag_type="pos",
    use_crf=False,
    use_rnn=False,
    reproject_embeddings=False,
)

# Preparing the multitask model.
multitask_model = MultitaskModel(
    models=[ner_tagger_model, pos_tagger_model],
    task_ids=["task_ner", "task_pos"],
    use_all_tasks=True,
)

# Train the multitask model.
trainer = ModelTrainer(model=multitask_model, corpus=corpus)
trainer.fine_tune(
    base_path="ner_pos_mt_model_v1",
    warmup_fraction=0.06,
    learning_rate=2.0e-5,
    mini_batch_size=32,
    train_with_dev=False,
    train_with_test=False,
    max_epochs=3,
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants