# BERT model not working (KeyError: 'sample\_weight\_mode')

**URL:** <https://forum.knime.com/t/bert-model-not-working-keyerror-sample-weight-mode/37037>\
**Category:** Partner Extensions\
**Tags:** redfield\
**Created:** [October 29, 2021, 12:49pm UTC](https://forum.knime.com/t/bert-model-not-working-keyerror-sample-weight-mode/37037 "2021-10-29T12:49:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mick](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@Mick](https://forum.knime.com/u/Mick)\
**Post date:** [October 29, 2021, 12:49pm UTC](https://forum.knime.com/t/bert-model-not-working-keyerror-sample-weight-mode/37037/1 "2021-10-29T12:49:16Z")

</div>

I trained a set of BERT models about a year ago (in December 2020) and I am only now trying to use one for prediction today. The model reads OK, and the predictor node seems to load the table of sentences (467 rows/sentences) OK, with the progress meter getting to 10%, but then (after a brief pause) it stops with this message:

ERROR BERT Predictor 0:393 Execute failed: ‘sample\_weight\_mode’  
Traceback (most recent call last):  
File “”, line 10, in   
File “C:\Program Files\KNIME423\plugins\se.redfield.bert\_0.0.1.v202012081121\py\BertClassifier.py”, line 155, in run\_predict  
model = tf.keras.models.load\_model(file\_store)  
File “C:\Users\FirstName.LastName\anaconda3\envs\py3\_knime\_tf2\lib\site-packages\tensorflow\python\keras\saving\save.py”, line 190, in load\_model  
return saved\_model\_load.load(filepath, compile)  
File “C:\Users\FirstName.LastName\anaconda3\envs\py3\_knime\_tf2\lib\site-packages\tensorflow\python\keras\saving\saved\_model\load.py”, line 126, in load  
training\_config))  
File “C:\Users\FirstName.LastName\anaconda3\envs\py3\_knime\_tf2\lib\site-packages\tensorflow\python\keras\saving\saving\_utils.py”, line 230, in compile\_args\_from\_training\_config  
sample\_weight\_mode = training\_config[‘sample\_weight\_mode’]  
KeyError: ‘sample\_weight\_mode’

I found a similar issue discussed ([KeyError: 'sample\_weight\_mode' · Issue #14040 · keras-team/keras · GitHub](https://github.com/keras-team/keras/issues/14040)) which recommends turning off model compilation, however I don’t know how to do this in Knime, looks like it is not an option.

My Python and TensorFlow/DeepLearning environments are unchanged since a year ago (Python version 3.6.12), the only major change that I can remember is updating Knime itself (from 4.2.3 [I think] to 4.3.3) in May this year.

This failing model was created on 12 Dec 2020, so as an experiment, I tested another of the BERT models that I produced last December, from a few days earlier (7 Dec 2020) and this one DOES work OK (processing 74 sentences)! So I’m wondering why one model works and another (from a few days later) doesn’t. I tried reducing the number of rows from 467 to 74, and then to just one row, but it still fails.

I checked my conda environment in Anaconda (against the list of required versions here: [BERT Text Classification for Everyone | KNIME](https://www.knime.com/blog/bert-text-classification-for-everyone)) and the only differences are that I have slightly more recent versions of these three packages, for some reason:

numpy (I have 1.19.4, should be 1.19.1)  
tokenizers (0.8.1rc1, should be 0.7.0)  
tqdm (4.54.0, should be 4.48.0)

Could these make a difference, or cause such an error? I can’t remember how to revert to earlier versions in Python, but it took me days to get this close to the required versions, so I’m a bit wary of fiddling any more, especially as one of the models has worked.

Any ideas welcome! Would upgrading Knime to the latest version help?

Thanks

Mike

---

<div class="post-metadata">

**Author:** ![Redfield](https://forum-cdn.knime.com/user_avatar/forum.knime.com/redfield/32/12356_2.png) [@Redfield](https://forum.knime.com/u/Redfield)\
**Post date:** [November 2, 2021, 7:18am UTC](https://forum.knime.com/t/bert-model-not-working-keyerror-sample-weight-mode/37037/2 "2021-11-02T07:18:59Z")

</div>

Hello @Mick

Could you please downgrade the versions of Python packages to the recommended ones? You can do it with pip command something like this “pip install tokenizers==0.7.0”.

Is it possible for you to share the model so I can try it on my environment?

And the last thing, you can try to use this workflow that has Conda Environment Propagation nodes with the required packages. Just install a new environment and select it in Python Deep Learning settings for TensorFlow2 section.

> **[DBPedia texts classification with BERT by Redfield – redfield](https://hub.knime.com/redfield/spaces/Public/latest/DBPedia_texts_classification_with_BERT_by_Redfield~F36Skypp8tyDSHEH)**
>
> The workflow uses enhanced version that includes Portuguese texts. English data is taken from here: https://www.kaggle.com/danofer/dbpedia-classes The workflow…

Please let me know if any of the advice help.

Best,  
Artem.

---

<div class="post-metadata">

**Author:** ![Mick](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/m/f04885/32.png) [@Mick](https://forum.knime.com/u/Mick)\
**Post date:** [November 3, 2021, 9:51am UTC](https://forum.knime.com/t/bert-model-not-working-keyerror-sample-weight-mode/37037/3 "2021-11-03T09:51:42Z")

</div>

Thank you Artem. I have managed to get the model working by using a different environment (‘test-pip’) that I created a year ago, at the same time as I built the models using the ‘py3\_knime\_tf2’ environment. I can’t remember now why I created test-pip or what it is, but I think I was probably trying to side-step using Anaconda, which seemed to create deadlocks and incompatabilities when trying to install the full set of required python packages of the recommended versions. Looking at test-pip’s package versions, it is notable that tensorflow is 2.3.0.

I have tested my ‘py3\_knime\_tf2’ environment and it is still capable of building models (without error messages). And as I said, one model has worked OK under it, but not this other one that I am currently needing to run. But this latter runs under ‘test-pip’ so the immediate problem is ‘solved’.

But when I get time I will try downgrading those particular packages in ‘py3\_knime\_tf2’, to see if I can get it to work with this model (and I will also try the Conda ‘create environment’ node, it looks very useful).

Thanks again

Mike

---

<div class="post-metadata">

**Author:** ![Redfield](https://forum-cdn.knime.com/user_avatar/forum.knime.com/redfield/32/12356_2.png) [@Redfield](https://forum.knime.com/u/Redfield)\
**Post date:** [November 3, 2021, 12:09pm UTC](https://forum.knime.com/t/bert-model-not-working-keyerror-sample-weight-mode/37037/4 "2021-11-03T12:09:44Z")

</div>

I am glad you found a workaround for your issue. Unfortunately package management is a big problem for Python and BERT in particular.

In case it is not sensitive could you please send me the model and list of packages with versions for both environments - where the model and where does not?  
This information might be helpful for us to make our node more flexible and avoid similar issues in the future.

Please feel free to get back to this thread in case you have some questions or updates.

Best,  
Artem.
