You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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:
fromflair.datasetsimportCONLL_03_DUTCHfromflair.embeddingsimportTransformerWordEmbeddingsfromflair.modelsimportSequenceTagger, MultitaskModelfromflair.trainersimportModelTrainercorpus=CONLL_03_DUTCH()
# Adding the special label type to make use_all_tasks=True work.forcorpus_splitin (corpus.train, corpus.dev, corpus.test):
forsentincorpus_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,
)
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
The text was updated successfully, but these errors were encountered: