- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Huggingface 进一步训练 BERT 模型。我使用两种方法保存模型:步骤 (1) 使用此代码保存整个模型:model.save_pretrained(save_location)
,步骤 (2) 使用此代码保存模型的 state_dict:torch.save(model.state_dict(),'model.pth')
但是,当我尝试使用以下代码加载此预训练 BERT 模型时,bert_mask_lm = BertForMaskedLM.from_pretrained('save_location')
用于步骤 (1) 和 torch.load('model.pth' )
对于步骤 (2),我在这两个步骤中都收到以下错误:
AttributeError Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/torch/serialization.py in _check_seekable(f)
307 try:
--> 308 f.seek(f.tell())
309 return True
AttributeError: 'torch._C.PyTorchFileReader' object has no attribute 'seek'
During handling of the above exception, another exception occurred:
步骤(1)的详细stacktrace如下:
AttributeError Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/torch/serialization.py in _check_seekable(f)
307 try:
--> 308 f.seek(f.tell())
309 return True
AttributeError: 'torch._C.PyTorchFileReader' object has no attribute 'seek'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/transformers/modeling_utils.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
1037 try:
-> 1038 state_dict = torch.load(resolved_archive_file, map_location="cpu")
1039 except Exception:
~/anaconda3/lib/python3.6/site-packages/torch/serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
593 return torch.jit.load(opened_file)
--> 594 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
595 return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
~/anaconda3/lib/python3.6/site-packages/moxing/framework/file/file_io_patch.py in _load(f, map_location, pickle_module, **pickle_load_args)
199
--> 200 _check_seekable(f)
201 f_should_read_directly = _should_read_directly(f)
~/anaconda3/lib/python3.6/site-packages/torch/serialization.py in _check_seekable(f)
310 except (io.UnsupportedOperation, AttributeError) as e:
--> 311 raise_err_msg(["seek", "tell"], e)
312 return False
~/anaconda3/lib/python3.6/site-packages/torch/serialization.py in raise_err_msg(patterns, e)
303 + " try to load from it instead.")
--> 304 raise type(e)(msg)
305 raise e
AttributeError: 'torch._C.PyTorchFileReader' object has no attribute 'seek'. You can only torch.load from a file that is seekable. Please pre-load the data into a buffer like io.BytesIO and try to load from it instead.
During handling of the above exception, another exception occurred:
OSError Traceback (most recent call last)
~/work/algo-FineTuningBert3/FineTuningBert3.py in <module>()
1 #Model load checking
----> 2 loadded_model = BertForMaskedLM.from_pretrained('/cache/raw_model/')
~/anaconda3/lib/python3.6/site-packages/transformers/modeling_utils.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
1039 except Exception:
1040 raise OSError(
-> 1041 f"Unable to load weights from pytorch checkpoint file for '{pretrained_model_name_or_path}' "
1042 f"at '{resolved_archive_file}'"
1043 "If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True. "
OSError: Unable to load weights from pytorch checkpoint file for '/cache/raw_model/' at '/cache/raw_model/pytorch_model.bin'If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True.
我使用的是最新的 torch (1.7.1) 和 transformers (4.3.3) 包。我不清楚导致此错误的原因以及如何解决此问题。
最佳答案
我也经历过同样的事情。事实证明,这可能是由于 PyTorch 和 transformers 的版本无关。它必须是特定于版本的。
我在没有下载最新的 bert-base-uncased 模型的情况下使用了以下内容:
pip install torch==1.5.1
pip install transformers==3.0.2
MODEL_NAME = 'bert-base-uncased'
model = BertForTokenClassification.from_pretrained(
MODEL_NAME
)
这将自动下载关于合适版本的变压器的预训练 BERT 模型注意:我单独从官方网站明确下载了 vocab.txt,并将其与 BERT 分词器类一起使用。
关于python - 加载预训练的 BERT 模型问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66442648/
我正在尝试从 BERT 模型中的隐藏状态中获取句子向量。看着拥抱脸 BertModel 说明 here ,其中说: from transformers import BertTokenizer, Be
我正在将 Huggingface BERT 用于 NLP 任务。我的文本包含被分成子词的公司名称。 tokenizer = BertTokenizerFast.from_pretrained('ber
对于 Transformer 模型,尤其是 BERT,以编程方式禁止模型以特殊标记作为预测结果是否有意义(并且在统计上是否正确)?在最初的实现中情况如何?在收敛过程中,模型必须学会不预测这些,但这种干
我有一个包含段落的数据集,我需要将其分为两类。这些段落通常有 3-5 句话长。其中绝大多数的长度不到 500 字。我想利用BERT来解决这个问题。 我想知道我应该如何使用 BERT 来生成这些段落的向
我想在特定域上微调 BERT。我在文本文件中有该域的文本。我如何使用这些来微调 BERT? 我在找 here目前。 我的主要目标是使用 BERT 获得句子嵌入。 最佳答案 这里要做出的重要区别是您是否
我想针对未标记数据的特定域微调 BERT,并让输出层检查它们之间的相似性。我该怎么做?我是否需要先微调分类器任务(或问题答案等)并获得嵌入?或者我可以只使用预先训练好的 Bert 模型而无需执行任务并
我遇到了这个page 1)我想在微调完成后获得句子级嵌入(由[CLS] token 给出的嵌入)。我怎么能做到? 2)我还注意到该页面上的代码需要花费大量时间才能返回测试数据的结果。这是为什么?当我训
我读过解释滑动窗口如何工作的帖子,但我找不到任何关于它是如何实际实现的信息。 据我了解,如果输入太长,可以使用滑动窗口来处理文本。 如果我错了,请纠正我。 假设我有一个文本 “2017 年 6 月 K
我正在尝试使用 BERT 微调模型(使用 transformers 库),但我对优化器和调度器有点不确定。 首先,我明白我应该使用 transformers.AdamW而不是 Pytorch 的版本。
我在 Tensorflow 中使用 BERT,有一个细节我不太明白。根据文档( https://tfhub.dev/google/bert_uncased_L-12_H-768_A-12/1 ),合并
我正在阅读 BERT paper并且不清楚 transformer 的输入编码器和解码器。 对于学习掩码语言模型(Cloze 任务),论文称 15% 的标记是被掩码的,并且训练网络来预测被掩码的标记。
我想使用 Bert 训练一个21 类 文本分类模型。但是我的训练数据很少,所以下载了一个类似的数据集,其中包含 5 类 和 200 万个样本。t并使用 bert 提供的 uncased 预训练模型对下
我正在训练一个在 BERT 之上使用自定义层的分类模型。在此期间,该模型的训练性能随着纪元的增加而下降(在第一个纪元之后)..我不确定这里要修复什么 - 是模型还是数据? (对于数据来说,它是二进制标
我是初学者..我正在和伯特一起工作。但出于公司网络的安全考虑,以下代码并没有直接接收bert模型。 tokenizer = BertTokenizer.from_pretrained('bert-ba
如何卡住上述预训练模型中的最后两层(dropout 和分类器层)?这样当模型运行时,我将得到一个致密层作为输出。 最佳答案 我想指出 BertForSequenceClassification 的定义
我正在使用 Huggingface 进一步训练 BERT 模型。我使用两种方法保存模型:步骤 (1) 使用此代码保存整个模型:model.save_pretrained(save_location),
我收到以下错误: AssertionError:文本输入必须为 str(单个示例)、List[str](批处理或单个预标记示例)或 List[List[str]](预标记示例批处理)类型。,当我运行
我想构建一个多类分类模型,我将对话数据作为 BERT 模型的输入(使用 bert-base-uncased)。 QUERY: I want to ask a question. ANSWER: Sur
我很感兴趣如何从 BERT 模型中获得不同句子中词嵌入的相似性(实际上,这意味着词在不同场景中具有不同的含义)。 例如: sent1 = 'I like living in New York.' se
众所周知,BERT 模型的词嵌入能力可能优于 word2vec 和任何其他模型。 我想在 BERT 词嵌入上创建一个模型来生成同义词或相似词。就像我们在 Gensim Word2Vec 中所做的一样。
我是一名优秀的程序员,十分优秀!