ktrain icon indicating copy to clipboard operation
ktrain copied to clipboard

NER training error with pretrained transformer

Open evilying opened this issue 3 years ago • 1 comments

Hello, thank you for this great lib. I am working on the NER training with pretrained transformer. However, whenever I used the model = text.sequence_tagger('bilstm-transformer', preproc, transformer_model='microsoft/deberta-v3-base', wv_path_or_url=WV_URL)

I noticed that there was a issue https://github.com/amaiya/ktrain/issues/437 about this. But I did not see that the issue is resolved yet. Do you have some suggestion to solve this? Thank you!

It will give me the following error: learner.fit(1e-2, 1, cycle_len=1) File "/home/kun/.local/lib/python3.10/site-packages/ktrain/core.py", line 1636, in fit hist = fit_fn( File "/home/kun/.local/lib/python3.10/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler raise e.with_traceback(filtered_tb) from None File "/home/kun/.local/lib/python3.10/site-packages/tensorflow/python/eager/execute.py", line 54, in quick_execute tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:

Detected at node 'model/concatenate/concat_1' defined at (most recent call last): File "/home/kun/work/ner/train_ner.py", line 26, in learner.fit(1e-2, 1, cycle_len=1) File "/home/kun/.local/lib/python3.10/site-packages/ktrain/core.py", line 1636, in fit hist = fit_fn( File "/home/kun/.local/lib/python3.10/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler return fn(*args, **kwargs) File "/home/kun/.local/lib/python3.10/site-packages/keras/engine/training.py", line 1384, in fit tmp_logs = self.train_function(iterator) File "/home/kun/.local/lib/python3.10/site-packages/keras/engine/training.py", line 1021, in train_function return step_function(self, iterator) File "/home/kun/.local/lib/python3.10/site-packages/keras/engine/training.py", line 1010, in step_function outputs = model.distribute_strategy.run(run_step, args=(data,)) File "/home/kun/.local/lib/python3.10/site-packages/keras/engine/training.py", line 1000, in run_step outputs = model.train_step(data) File "/home/kun/.local/lib/python3.10/site-packages/keras/engine/training.py", line 859, in train_step y_pred = self(x, training=True) File "/home/kun/.local/lib/python3.10/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler return fn(*args, **kwargs) File "/home/kun/.local/lib/python3.10/site-packages/keras/engine/base_layer.py", line 1096, in call outputs = call_fn(inputs, *args, **kwargs) File "/home/kun/.local/lib/python3.10/site-packages/keras/utils/traceback_utils.py", line 92, in error_handler return fn(*args, **kwargs) File "/home/kun/.local/lib/python3.10/site-packages/keras/engine/functional.py", line 451, in call return self._run_internal_graph( File "/home/kun/.local/lib/python3.10/site-packages/keras/engine/functional.py", line 589, in _run_internal_graph outputs = node.layer(*args, **kwargs) File "/home/kun/.local/lib/python3.10/site-packages/keras/utils/traceback_utils.py", line 64, in error_handler return fn(*args, **kwargs) File "/home/kun/.local/lib/python3.10/site-packages/keras/engine/base_layer.py", line 1101, in call self._set_mask_metadata(inputs, outputs, input_masks, not eager) File "/home/kun/.local/lib/python3.10/site-packages/keras/engine/base_layer.py", line 2573, in _set_mask_metadata output_masks = self.compute_mask(inputs, previous_mask) File "/home/kun/.local/lib/python3.10/site-packages/keras/layers/merge.py", line 579, in compute_mask concatenated = backend.concatenate(masks, axis=self.axis) File "/home/kun/.local/lib/python3.10/site-packages/keras/backend.py", line 3313, in concatenate return tf.concat([to_dense(x) for x in tensors], axis) Node: 'model/concatenate/concat_1' ConcatOp : Dimension 1 in both shapes must be equal: shape[0] = [16,66,1] vs. shape[1] = [16,69,768] [[{{node model/concatenate/concat_1}}]] [Op:__inference_train_function_18731]

evilying avatar May 09 '23 08:05 evilying

Hi @evilying The previous issue you referenced was solved a while ago. This is actually a different issue that is specific to deberta. The DeBERTa tokenizer includes preceding space in offset_mapping, which was causing problems.

The issue should now be fixed in the develop branch, and the next release will include the fix:

!wget https://raw.githubusercontent.com/amaiya/ktrain/master/tests/resources/conll2003/train.txt -O /tmp/train.txt
!wget https://raw.githubusercontent.com/amaiya/ktrain/master/tests/resources/conll2003/valid.txt -O /tmp/valid.txt
MODEL_NAME = 'microsoft/deberta-v3-base'
import ktrain
(trn, val, preproc) = ktrain.text.entities_from_conll2003('/tmp/train.txt', val_filepath='/tmp/valid.txt')
model = ktrain.text.sequence_tagger('bilstm-transformer', preproc, transformer_model=MODEL_NAME)
learner = ktrain.get_learner(model, train_data=trn, val_data=val, batch_size=128)
learner.fit(0.01, 1, cycle_len=1)
predictor = ktrain.get_predictor(model, preproc)
predictor.predict('George Washington went to Washington.')

# output
#[('George', 'B-PER'),
#('Washington', 'I-PER'),
#('went', 'O'),
#('to', 'O'),
#('Washington', 'B-LOC'),
#('.', 'O')]

Thanks for letting me know about this issue. If you have further questions/problems, feel free to reply to this issue.

amaiya avatar May 09 '23 21:05 amaiya