基于 BERT 的 NER 模型在反序列化时给出不一致的预测
BERT-based NER model giving inconsistent prediction when deserialized
我正在尝试使用 Colab 云 GPU 上的 HuggingFace 转换器库训练 NER 模型,对其进行 pickle 并自行加载模型 CPU 以进行预测。
代码
模型如下:
from transformers import BertForTokenClassification
model = BertForTokenClassification.from_pretrained(
"bert-base-cased",
num_labels=NUM_LABELS,
output_attentions = False,
output_hidden_states = False
)
我正在使用此代码段在 Colab 上保存模型
import torch
torch.save(model.state_dict(), FILENAME)
然后使用
将其加载到我的本地CPU
# Initiating an instance of the model type
model_reload = BertForTokenClassification.from_pretrained(
"bert-base-cased",
num_labels=len(tag2idx),
output_attentions = False,
output_hidden_states = False
)
# Loading the model
model_reload.load_state_dict(torch.load(FILENAME, map_location='cpu'))
model_reload.eval()
用于标记文本和进行实际预测的代码片段在 Colab GPU 笔记本实例和我的 CPU 笔记本实例上是相同的。
预期行为
经过 GPU 训练的模型表现正确并完美地对以下标记进行分类:
O [CLS]
O Good
O morning
O ,
O my
O name
O is
B-per John
I-per Kennedy
O and
O I
O am
O working
O at
B-org Apple
O in
O the
O headquarters
O of
B-geo Cupertino
O [SEP]
实际行为
加载模型并使用它对我的 CPU 进行预测时,预测完全错误:
I-eve [CLS]
I-eve Good
I-eve morning
I-eve ,
I-eve my
I-eve name
I-eve is
I-geo John
B-eve Kennedy
I-eve and
I-eve I
I-eve am
I-eve working
I-eve at
I-gpe Apple
I-eve in
I-eve the
I-eve headquarters
I-eve of
B-org Cupertino
I-eve [SEP]
有没有人知道为什么它不起作用?我错过了什么吗?
我修好了,有两个问题:
令牌的索引标签映射是错误的,出于某种原因,list() 函数在 Colab GPU 上的工作方式与我的 CPU (??)
不同
用于保存模型的代码片段不正确,对于基于 huggingface-transformers 库的模型你不能使用 model.save_dict() 并稍后加载它,你需要使用模型 class 的 save_pretrained() 方法,稍后使用 from_pretrained().
加载它
我正在尝试使用 Colab 云 GPU 上的 HuggingFace 转换器库训练 NER 模型,对其进行 pickle 并自行加载模型 CPU 以进行预测。
代码
模型如下:
from transformers import BertForTokenClassification
model = BertForTokenClassification.from_pretrained(
"bert-base-cased",
num_labels=NUM_LABELS,
output_attentions = False,
output_hidden_states = False
)
我正在使用此代码段在 Colab 上保存模型
import torch
torch.save(model.state_dict(), FILENAME)
然后使用
将其加载到我的本地CPU# Initiating an instance of the model type
model_reload = BertForTokenClassification.from_pretrained(
"bert-base-cased",
num_labels=len(tag2idx),
output_attentions = False,
output_hidden_states = False
)
# Loading the model
model_reload.load_state_dict(torch.load(FILENAME, map_location='cpu'))
model_reload.eval()
用于标记文本和进行实际预测的代码片段在 Colab GPU 笔记本实例和我的 CPU 笔记本实例上是相同的。
预期行为
经过 GPU 训练的模型表现正确并完美地对以下标记进行分类:
O [CLS]
O Good
O morning
O ,
O my
O name
O is
B-per John
I-per Kennedy
O and
O I
O am
O working
O at
B-org Apple
O in
O the
O headquarters
O of
B-geo Cupertino
O [SEP]
实际行为
加载模型并使用它对我的 CPU 进行预测时,预测完全错误:
I-eve [CLS]
I-eve Good
I-eve morning
I-eve ,
I-eve my
I-eve name
I-eve is
I-geo John
B-eve Kennedy
I-eve and
I-eve I
I-eve am
I-eve working
I-eve at
I-gpe Apple
I-eve in
I-eve the
I-eve headquarters
I-eve of
B-org Cupertino
I-eve [SEP]
有没有人知道为什么它不起作用?我错过了什么吗?
我修好了,有两个问题:
令牌的索引标签映射是错误的,出于某种原因,list() 函数在 Colab GPU 上的工作方式与我的 CPU (??)
不同用于保存模型的代码片段不正确,对于基于 huggingface-transformers 库的模型你不能使用 model.save_dict() 并稍后加载它,你需要使用模型 class 的 save_pretrained() 方法,稍后使用 from_pretrained().
加载它