![](/img/trans.png)
[英]RuntimeError, working on IA tryna use a pre-trained BERT model
[英]Weights of pre-trained BERT model not initialized
我正在使用語言解釋工具包(LIT) 加載和分析我在 NER 任務上預訓練的 BERT model。
但是,當我使用傳遞給它的預訓練 model 的路徑啟動 LIT 腳本時,它無法初始化權重並告訴我:
modeling_utils.py:648] loading weights file bert_remote/examples/token-classification/Data/Models/results_21_03_04_cleaned_annotations/04.03._8_16_5e-5_cleaned_annotations/04-03-2021 (15.22.23)/pytorch_model.bin
modeling_utils.py:739] Weights of BertForTokenClassification not initialized from pretrained model: ['bert.pooler.dense.weight', 'bert.pooler.dense.bias']
modeling_utils.py:745] Weights from pretrained model not used in BertForTokenClassification: ['bert.embeddings.position_ids']
然后它只是使用 BERT 的bert-base-german-cased
版本,它當然沒有我的自定義標簽,因此無法預測任何東西。 我認為這可能與 PyTorch 有關,但我找不到錯誤。
如果相關,這里是我如何將我的數據集加載到 CoNLL 2003 格式(在此處找到的數據加載器腳本的修改):
def __init__(self):
# Read ConLL Test Files
self._examples = []
data_path = "lit_remote/lit_nlp/examples/datasets/NER_Data"
with open(os.path.join(data_path, "test.txt"), "r", encoding="utf-8") as f:
lines = f.readlines()
for line in lines[:2000]:
if line != "\n":
token, label = line.split(" ")
self._examples.append({
'token': token,
'label': label,
})
else:
self._examples.append({
'token': "\n",
'label': "O"
})
def spec(self):
return {
'token': lit_types.Tokens(),
'label': lit_types.SequenceTags(align="token"),
}
這就是我如何初始化 model 並啟動 LIT 服務器(修改simple_pytorch_demo.py
腳本在這里找到):
def __init__(self, model_name_or_path):
self.tokenizer = transformers.AutoTokenizer.from_pretrained(
model_name_or_path)
model_config = transformers.AutoConfig.from_pretrained(
model_name_or_path,
num_labels=15, # FIXME CHANGE
output_hidden_states=True,
output_attentions=True,
)
# This is a just a regular PyTorch model.
self.model = _from_pretrained(
transformers.AutoModelForTokenClassification,
model_name_or_path,
config=model_config)
self.model.eval()
## Some omitted snippets here
def input_spec(self) -> lit_types.Spec:
return {
"token": lit_types.Tokens(),
"label": lit_types.SequenceTags(align="token")
}
def output_spec(self) -> lit_types.Spec:
return {
"tokens": lit_types.Tokens(),
"probas": lit_types.MulticlassPreds(parent="label", vocab=self.LABELS),
"cls_emb": lit_types.Embeddings()
這實際上似乎是預期的行為。 在GPT 模型的文檔中, HuggingFace 團隊寫道:
這將發出有關未使用某些預訓練權重和隨機初始化某些權重的警告。 那是因為我們丟棄了 BERT model 的預訓練頭,用隨機初始化的分類頭替換它。
所以微調似乎不是問題。 在我上面描述的用例中,盡管有警告,它仍然有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.