Skip to content

[已解決] Some weights of the model checkpoint at distilbert-base-multilingual-cased were not used when initializing DistilBertForSequenceClassification: [‘vocab_projector.bias’, ‘vocab_layer_norm.bias’, ‘vocab_layer_norm.weight’, ‘vocab_transform.weight’, ‘vocab_transform.bias’]

問題描述

在使用 transformers 套件時,如果當我們使用如 AutoModelForSequenceClassification、AutoModelForSeq2SeqLM… 這樣不同 head 的任務模型時,經常我們會得到以下錯誤訊息:

Some weights of the model checkpoint at distilbert-base-multilingual-cased were not used when initializing DistilBertForSequenceClassification: ['vocab_projector.bias', 'vocab_layer_norm.bias', 'vocab_layer_norm.weight', 'vocab_transform.weight', 'vocab_transform.bias']
- This IS expected if you are initializing DistilBertForSequenceClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing DistilBertForSequenceClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
Some weights of DistilBertForSequenceClassification were not initialized from the model checkpoint at distilbert-base-multilingual-cased and are newly initialized: ['classifier.weight', 'pre_classifier.bias', 'pre_classifier.weight', 'classifier.bias']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.


這個警告訊息是由 Hugging Face 的 transformers 函式庫所產生的,它告訴我們當前正在初始化的模型 (DistilBertForSequenceClassification) 並沒有使用到原本預訓練模型 (distilbert-base-multilingual-cased) 的某些權重。這可能是因為原始的預訓練模型與目標模型有著不同的架構或者被訓練用於不同的任務。

另外,訊息也指出 DistilBertForSequenceClassification 的某些權重並沒有從預訓練模型中初始化,而是新建的。所以該警告建議我們應該在下游任務中持續 fine-tune 這個模型,以便於後續進行預測或推論。

也就是說,如果你是在準備訓練模型初始化時見到這個錯誤訊息,那是非常正常的事情,因為那些針對不同任務的 head 本來就是我們在下由任務要進行訓練的。

但也要注意,若是在 Inference 階段,你是讀取你訓練好的模型時看到這個錯誤,則是很不妙的事情。這代表你所訓練的模型架構與這個任務不匹配,可能在某處已經發生問題了。


解決方法

from transformers import logging as transformer_logging

# Ignore transformer warning
transformer_loggging.set_verbosity_error()


這樣設置後,只有錯誤級別的訊息才會被打印出來,所有的警告訊息都會被忽略。

然而,需要注意的是,雖然這種方式可以讓我們的輸出更為整潔,但是一些可能對我們的模型有影響的重要警告也可能會被忽略。所以只有在確定這些警告對我們的模型訓練和預測無影響的情況下,才建議使用此方法。


References


Read More

Leave a Reply