# When finetuning Bert on classification task raised TypeError(f'Object of type {o.\_\_class\_\_.\_\_name\_\_} ' TypeError: Object of type ndarray is not JSON serializable

**URL:** <https://discuss.huggingface.co/t/when-finetuning-bert-on-classification-task-raised-typeerror-fobject-of-type-o-class-name-typeerror-object-of-type-ndarray-is-not-json-serializable/11370>\
**Category:** Beginners\
**Created:** [November 4, 2021, 6:18pm UTC](https://discuss.huggingface.co/t/when-finetuning-bert-on-classification-task-raised-typeerror-fobject-of-type-o-class-name-typeerror-object-of-type-ndarray-is-not-json-serializable/11370 "2021-11-04T18:18:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![emmakelo](https://avatars.discourse-cdn.com/v4/letter/e/ea666f/32.png) [@emmakelo](https://discuss.huggingface.co/u/emmakelo)\
**Post date:** [November 4, 2021, 6:18pm UTC](https://discuss.huggingface.co/t/when-finetuning-bert-on-classification-task-raised-typeerror-fobject-of-type-o-class-name-typeerror-object-of-type-ndarray-is-not-json-serializable/11370/1 "2021-11-04T18:18:37Z")

</div>

Hello, I am trying to finetune bert on classification task but I am getting this error during the training.

e[ASaving model checkpoint to /gpfswork/rech/kpf/umg16uw/results\_hf/checkpoint-500  
Configuration saved in /gpfswork/rech/kpf/umg16uw/results\_hf/checkpoint-500/config.json  
Model weights saved in /gpfswork/rech/kpf/umg16uw/results\_hf/checkpoint-500/pytorch\_model.bin  
Traceback (most recent call last):  
File “/gpfs7kw/linkhome/rech/genlig01/umg16uw/test/expe\_5/traitements/Flaubert\_huggingface.py”, line 225, in   
train\_results = trainer.train()  
File “/linkhome/rech/genlig01/umg16uw/.conda/envs/bert/lib/python3.9/site-packages/transformers/trainer.py”, line 1325, in train  
self.\_maybe\_log\_save\_evaluate(tr\_loss, model, trial, epoch)  
File “/linkhome/rech/genlig01/umg16uw/.conda/envs/bert/lib/python3.9/site-packages/transformers/trainer.py”, line 1422, in \_maybe\_log\_save\_evaluate  
self.\_save\_checkpoint(model, trial, metrics=metrics)  
File “/linkhome/rech/genlig01/umg16uw/.conda/envs/bert/lib/python3.9/site-packages/transformers/trainer.py”, line 1537, in \_save\_checkpoint  
self.state.save\_to\_json(os.path.join(output\_dir, “trainer\_state.json”))  
File “/linkhome/rech/genlig01/umg16uw/.conda/envs/bert/lib/python3.9/site-packages/transformers/trainer\_callback.py”, line 96, in save\_to\_json  
json\_string = json.dumps(dataclasses.asdict(self), indent=2, sort\_keys=True) + “\n”  
File “/linkhome/rech/genlig01/umg16uw/.conda/envs/bert/lib/python3.9/json/ **init**.py”, line 234, in dumps  
return cls(  
File “/linkhome/rech/genlig01/umg16uw/.conda/envs/bert/lib/python3.9/json/encoder.py”, line 201, in encode  
chunks = list(chunks)  
File “/linkhome/rech/genlig01/umg16uw/.conda/envs/bert/lib/python3.9/json/encoder.py”, line 431, in \_iterencode  
yield from \_iterencode\_dict(o, \_current\_indent\_level)  
File “/linkhome/rech/genlig01/umg16uw/.conda/envs/bert/lib/python3.9/json/encoder.py”, line 405, in \_iterencode\_dict  
yield from chunks  
File “/linkhome/rech/genlig01/umg16uw/.conda/envs/bert/lib/python3.9/json/encoder.py”, line 325, in \_iterencode\_list  
yield from chunks  
File “/linkhome/rech/genlig01/umg16uw/.conda/envs/bert/lib/python3.9/json/encoder.py”, line 405, in \_iterencode\_dict  
yield from chunks  
File “/linkhome/rech/genlig01/umg16uw/.conda/envs/bert/lib/python3.9/json/encoder.py”, line 438, in \_iterencode  
o = \_default(o)  
File “/linkhome/rech/genlig01/umg16uw/.conda/envs/bert/lib/python3.9/json/encoder.py”, line 179, in default  
raise TypeError(f’Object of type {o. **class**. **name** } ’  
TypeError: Object of type ndarray is not JSON serializable

76%|███████▋ | 500/654 [02:52\<00:52, 2.91it/s]  
srun: error: r10i6n1: task 0: Exited with exit code 1  
srun: Terminating job step 1775050.0

output file :

file in training… /gpfs7kw/linkhome/rech/genlig01/umg16uw/test/expe\_5/dataset/train\_corpus/train\_80tr\_moins\_20t/80tr/corpusIxAug\_et\_Or80tr.xlsx

Filename in processed… corpusIxAug\_et\_Or80tr  
Number of sentences 18568.00…  
Type of preprocessing… verbatim  
Train : 13926 Val : 4642  
{‘loss’: 1.0099, ‘learning\_rate’: 4.9923547400611625e-05, ‘epoch’: 0.0}  
{‘loss’: 0.872, ‘learning\_rate’: 4.235474006116208e-05, ‘epoch’: 0.46}  
{‘eval\_loss’: 0.5922592878341675, ‘eval\_accuracy’: 0.7615252046531668, ‘eval\_f1’: array([0.64872657, 0.83726867, 0.71302958]), ‘eval\_precision’: array([0.62674095, 0.80523732, 0.79571811]), ‘eval\_recall’: array([0.67231076, 0.87195392, 0.64590876]), ‘eval\_f1\_mi’: 0.7615252046531666, ‘eval\_precision\_mi’: 0.7615252046531668, ‘eval\_recall\_mi’: 0.7615252046531668, ‘eval\_f1\_ma’: 0.733008272114256, ‘eval\_precision\_ma’: 0.7425654572607411, ‘eval\_recall\_ma’: 0.7300578132910654, ‘eval\_runtime’: 9.6426, ‘eval\_samples\_per\_second’: 481.407, ‘eval\_steps\_per\_second’: 7.571, ‘epoch’: 0.46}  
{‘loss’: 0.6014, ‘learning\_rate’: 3.4709480122324164e-05, ‘epoch’: 0.92}  
{‘eval\_loss’: 0.30887845158576965, ‘eval\_accuracy’: 0.8862559241706162, ‘eval\_f1’: array([0.80689306, 0.92978868, 0.8742268]), ‘eval\_precision’: array([0.82146543, 0.95429104, 0.83191629]), ‘eval\_recall’: array([0.79282869, 0.90651307, 0.92107169]), ‘eval\_f1\_mi’: 0.8862559241706162, ‘eval\_precision\_mi’: 0.8862559241706162, ‘eval\_recall\_mi’: 0.8862559241706162, ‘eval\_f1\_ma’: 0.8703028482577086, ‘eval\_precision\_ma’: 0.8692242527354628, ‘eval\_recall\_ma’: 0.873471147629887, ‘eval\_runtime’: 9.6181, ‘eval\_samples\_per\_second’: 482.632, ‘eval\_steps\_per\_second’: 7.59, ‘epoch’: 0.92}  
{‘loss’: 0.3815, ‘learning\_rate’: 2.7064220183486238e-05, ‘epoch’: 1.38}  
{‘eval\_loss’: 0.16964389383792877, ‘eval\_accuracy’: 0.9467901766479966, ‘eval\_f1’: array([0.9054878 , 0.96444059, 0.94674556]), ‘eval\_precision’: array([0.92427386, 0.94437367, 0.96749811]), ‘eval\_recall’: array([0.8874502 , 0.98537882, 0.92686459]), ‘eval\_f1\_mi’: 0.9467901766479966, ‘eval\_precision\_mi’: 0.9467901766479966, ‘eval\_recall\_mi’: 0.9467901766479966, ‘eval\_f1\_ma’: 0.9388913189246848, ‘eval\_precision\_ma’: 0.9453818807708362, ‘eval\_recall\_ma’: 0.933231203841253, ‘eval\_runtime’: 9.402, ‘eval\_samples\_per\_second’: 493.727, ‘eval\_steps\_per\_second’: 7.764, ‘epoch’: 1.38}  
{‘loss’: 0.2669, ‘learning\_rate’: 1.9418960244648318e-05, ‘epoch’: 1.83}  
{‘eval\_loss’: 0.10839153826236725, ‘eval\_accuracy’: 0.9648858250753986, ‘eval\_f1’: array([0.93973442, 0.97762021, 0.96196232]), ‘eval\_precision’: array([0.96436059, 0.97783688, 0.9448324]), ‘eval\_recall’: array([0.91633466, 0.97740363, 0.97972484]), ‘eval\_f1\_mi’: 0.9648858250753986, ‘eval\_precision\_mi’: 0.9648858250753986, ‘eval\_recall\_mi’: 0.9648858250753986, ‘eval\_f1\_ma’: 0.9597723163259425, ‘eval\_precision\_ma’: 0.9623432895564524, ‘eval\_recall\_ma’: 0.9578210438568343, ‘eval\_runtime’: 9.6063, ‘eval\_samples\_per\_second’: 483.223, ‘eval\_steps\_per\_second’: 7.599, ‘epoch’: 1.83}  
{‘loss’: 0.1962, ‘learning\_rate’: 1.1773700305810397e-05, ‘epoch’: 2.29}  
{‘eval\_loss’: 0.07769232243299484, ‘eval\_accuracy’: 0.978026712623869, ‘eval\_f1’: array([0.96184739, 0.98504179, 0.97815004]), ‘eval\_precision’: array([0.96963563, 0.9781564 , 0.98388278]), ‘eval\_recall’: array([0.95418327, 0.99202481, 0.97248371]), ‘eval\_f1\_mi’: 0.978026712623869, ‘eval\_precision\_mi’: 0.978026712623869, ‘eval\_recall\_mi’: 0.978026712623869, ‘eval\_f1\_ma’: 0.9750130736531469, ‘eval\_precision\_ma’: 0.9772249371959657, ‘eval\_recall\_ma’: 0.9728972620291924, ‘eval\_runtime’: 9.458, ‘eval\_samples\_per\_second’: 490.8, ‘eval\_steps\_per\_second’: 7.718, ‘epoch’: 2.29}

and then it stops and causes the error

script :

---

<div class="post-metadata">

**Author:** ![sgugger](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sgugger/32/2291_2.png) [@sgugger](https://discuss.huggingface.co/u/sgugger)\
**Post date:** [November 4, 2021, 6:28pm UTC](https://discuss.huggingface.co/t/when-finetuning-bert-on-classification-task-raised-typeerror-fobject-of-type-o-class-name-typeerror-object-of-type-ndarray-is-not-json-serializable/11370/2 "2021-11-04T18:28:50Z")

</div>

It looks like your `compute_metric` function is returning NumPy arrays, which is not supported.

---

<div class="post-metadata">

**Author:** ![emmakelo](https://avatars.discourse-cdn.com/v4/letter/e/ea666f/32.png) [@emmakelo](https://discuss.huggingface.co/u/emmakelo)\
**Post date:** [November 4, 2021, 6:36pm UTC](https://discuss.huggingface.co/t/when-finetuning-bert-on-classification-task-raised-typeerror-fobject-of-type-o-class-name-typeerror-object-of-type-ndarray-is-not-json-serializable/11370/3 "2021-11-04T18:36:34Z")

</div>

hello this is how I defined my computer metric but what type of format is supported ?

Can I print a classification report ? :

def compute\_metrics(pred):  
labels = pred.label\_ids  
preds = pred.predictions.argmax(-1)  
precision, recall, f1, \_ = precision\_recall\_fscore\_support(labels, preds)  
precision\_ma, recall\_ma, f1\_ma, \_ = precision\_recall\_fscore\_support(labels, preds, average=‘macro’)  
precision\_mi, recall\_mi, f1\_mi, \_ = precision\_recall\_fscore\_support(labels, preds, average=‘micro’)  
acc = accuracy\_score(labels, preds)  
return {  
‘accuracy’: acc,  
‘f1’: f1,  
‘precision’: precision,  
‘recall’: recall,

```
	'f1_mi': f1_mi,
	'precision_mi': precision_mi,
	'recall_mi': recall_mi,
	

	'f1_ma': f1_ma,
	'precision_ma': precision_ma,
	'recall_ma': recall_ma
}

```

I wanted to get the micro and macro precision since I am doing multiclassification.

---

<div class="post-metadata">

**Author:** ![Juventin](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@Juventin](https://discuss.huggingface.co/u/Juventin)\
**Post date:** [February 12, 2024, 8:18am UTC](https://discuss.huggingface.co/t/when-finetuning-bert-on-classification-task-raised-typeerror-fobject-of-type-o-class-name-typeerror-object-of-type-ndarray-is-not-json-serializable/11370/5 "2024-02-12T08:18:59Z")

</div>

Hello,  
I am getting the exact same error on multi-output classification.  
Did you find a solution ?

```auto
from setfit import SetFitModel, Trainer, SetFitTrainer, TrainingArguments
from sentence_transformers.losses import CosineSimilarityLoss

model = SetFitModel.from_pretrained(
    model_name,
    multi_target_strategy='multi-output',
    use_differentiable_head=True,
    head_params={"out_features": len(categories)},
)

model.labels = categories

args = TrainingArguments(
    batch_size=8,
    num_epochs=1,
    loss=CosineSimilarityLoss,
)

trainer = Trainer(
    model=model,
    args=args,
    train_dataset=train_ds,
    eval_dataset=test_ds
)

trainer.train()

```
