flair icon indicating copy to clipboard operation
flair copied to clipboard

[Question]: Multi-Task Learning with use_all_task

Open zrjohnnyl opened this issue 3 months ago • 0 comments

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

zrjohnnyl avatar May 10 '24 23:05 zrjohnnyl