我已经使用 Tensorflow hub 的 BERT 编码器有一段时间了。以下是语法:
tfhub_handle_encoder = "https://tfhub.dev/tensorflow/bert_multi_cased_L-12_H-768_A-12/4" tfhub_handle_preprocess = "https://tfhub.dev/tensorflow/bert_multi_cased_preprocess/3" bert_preprocess_model = hub.KerasLayer(tfhub_handle_preprocess)
突然我遇到了这个错误消息
:FileNotFoundError: Op type not registered 'CaseFoldUTF8' in binary running on acb9309ebd87. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.) tf.contrib.resamplerexperimental_io_device tf.saved_model.LoadOptionsshould be done before importing the graph, as contrib ops are lazily registered when the module is first accessed. …
我正在尝试从预训练的“DistilBERT”模型的几个不同层访问输出嵌入。(“distilbert-base-uncased”)
bert_output = model(input_ids, attention_mask=attention_mask)
Run Code Online (Sandbox Code Playgroud)
bert_output 似乎只返回输入标记最后一层的嵌入值。
python nlp pytorch bert-language-model huggingface-transformers
我想应用 Roberta 模型来实现文本相似度。给定一对句子,输入的格式应为<s> A </s></s> B </s>。我想出了两种可能的方法来生成输入ID,即
A)
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained('roberta-base')
list1 = tokenizer.encode('Very severe pain in hands')
list2 = tokenizer.encode('Numbness of upper limb')
sequence = list1+[2]+list2[1:]
Run Code Online (Sandbox Code Playgroud)
在这种情况下,顺序是[0, 12178, 3814, 2400, 11, 1420, 2, 2, 234, 4179, 1825, 9, 2853, 29654, 2]
b)
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained('roberta-base')
list1 = tokenizer.encode('Very severe pain in hands', add_special_tokens=False)
list2 = tokenizer.encode('Numbness of upper limb', add_special_tokens=False)
sequence = [0]+list1+[2,2]+list2+[2]
Run Code Online (Sandbox Code Playgroud)
在这种情况下,顺序是[0, 25101, …
我不明白为什么我的 BERT 模型在评估期间返回相同的输出。我的模型在训练期间的输出似乎是正确的,因为值不同,但在评估期间却完全相同。

这是我的 BERT 模型类
class BERTBaseUncased(nn.Module):
def __init__(self):
super(BERTBaseUncased, self).__init__()
self.bert = BertModel.from_pretrained("bert-base-uncased")
self.bert_drop = nn.Dropout(0.3)
self.out = nn.Linear(768, 4)
def forward(self, ids, mask, token_type_ids):
_, o2 = self.bert(ids, attention_mask=mask, token_type_ids=token_type_ids) # Use one of the outputs
bo = self.bert_drop(o2)
return self.out(bo)
Run Code Online (Sandbox Code Playgroud)
我的数据集类
class BERTDataset:
def __init__(self, review, target, tokenizer, classes=4):
self.review = review
self.target = target
self.tokenizer = tokenizer
self.max_len = max_len
self.classes = classes
def __len__(self):
return len(self.review)
def __getitem__(self, item):
review = str(self.review)
review = …Run Code Online (Sandbox Code Playgroud) 我无法做到:pip install -U sentence-transformers。我在 Anaconda Prompt 上收到此消息:错误:找不到满足要求的版本 torch>=1.0.1(来自句子转换器)(来自版本:0.1.2、0.1.2.post1、0.1.2.post2 ) 错误:没有找到与 torch>=1.0.1 匹配的分布(来自句子转换器)有人可以帮忙吗?
我们如何为 ktrain 库中的文本分类器使用不同的预训练模型?使用时:
模型 = text.text_classifier('bert', (x_train, y_train) , preproc=preproc)
不过,我也想尝试单语模型。即荷兰语:“wietsedv/bert-base-dutch-cased”,它也用于其他 k-train 实现,例如.
但是,当尝试在文本分类器中使用此命令时,它不起作用:
model = text.text_classifier('bert', (x_train, y_train) ,
> preproc=preproc, bert_model='wietsedv/bert-base-dutch-cased')
Run Code Online (Sandbox Code Playgroud)
或者
model = text.text_classifier('wietsedv/bert-base-dutch-cased', (x_train, y_train), preproc=preproc)
Run Code Online (Sandbox Code Playgroud)
有人该怎么做吗?谢谢!
我想加载一个预先训练的 Bert 模型并对其进行微调,特别是使用自定义数据集的模型的词嵌入。任务是使用所选单词的词嵌入进行进一步分析。值得一提的是,该数据集由推文组成,没有标签。因此,我使用了 BertForMaskedLM 模型。
此任务可以使用输入 ID(标记化推文)作为标签吗?我没有标签。只有按随机顺序排列的推文。
从这一点开始,我展示我编写的代码:
首先,我清除了数据集中的表情符号、非 ASCII 字符等,如以下链接(2.3 部分)所述: https ://www.kaggle.com/jaskaransingh/bert-fine-tuning-with-pytorch
二、微调过程的代码:
import torch
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForMaskedLM.from_pretrained('bert-base-uncased')
model.to(device)
model.train()
lr = 1e-2
optimizer = AdamW(model.parameters(), lr=lr, correct_bias=False)
max_len = 82
chunk_size = 20
epochs = 20
for epoch in range(epochs):
epoch_losses = []
for j, batch in enumerate(pd.read_csv(path + file_name, chunksize=chunk_size)):
tweets = batch['content_cleaned'].tolist()
encoded_dict = tokenizer.batch_encode_plus(
tweets, # Sentence to encode.
add_special_tokens = True, # Add …Run Code Online (Sandbox Code Playgroud) python language-model word-embedding pytorch bert-language-model
我正在尝试在 git 链接中使用的 train2012 数据上训练 BertPunc 模型: https: //github.com/nkrnrnk/BertPunc。在启用 4 个 GPU 的服务器上运行时,出现以下错误:
StopIteration: Caught StopIteration in replica 1 on device 1.
Original Traceback (most recent call last):
File "/home/stenoaimladmin/.local/lib/python3.8/site-packages/torch/nn/parallel/parallel_apply.py", line 61, in _worker
output = module(*input, **kwargs)
File "/home/stenoaimladmin/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "/home/stenoaimladmin/notebooks/model_BertPunc.py", line 16, in forward
x = self.bert(x)
File "/home/stenoaimladmin/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "/home/stenoaimladmin/anaconda3/lib/python3.8/site-packages/pytorch_pretrained_bert/modeling.py", line 861, in forward
sequence_output, _ = self.bert(input_ids, token_type_ids, attention_mask, …Run Code Online (Sandbox Code Playgroud) 我想在时间序列数据上训练一个转换器编码器(例如 BERT),用于可以建模为分类的任务。在谈论我面临的问题之前,让我们先简要描述一下我正在使用的数据。
我正在使用 90 秒的窗口,并且每秒可以访问 100 个值(即 90 个大小为 100 的向量)。我的目标是每秒预测一个二进制标签(0 或 1)(即产生一个长度为 90 的 0 和 1 的最终向量)。
我的第一个想法是将其建模为一个多标签分类问题,我将使用 BERT 生成一个大小为 90 的向量,其中填充了 0 到 1 之间的数字,然后使用 nn.BCELoss 和 groundtruth 标签(y_true 看起来像 [0 ,0,0,1,1,1,0,0,1,1,1,0...,0])。一个简单的类比是将每一秒视为一个词,我可以访问的 100 个值作为相应的词嵌入。然后目标是在这些 100-dim 嵌入序列(所有序列长度相同:90)上训练 BERT(从头开始)。
问题:在处理文本输入时,我们只需将 CLS 和 SEP 标记添加到输入序列中,然后让标记生成器和模型完成剩下的工作。当直接对嵌入进行训练时,我们应该怎么做来考虑 CLS 和 SEP 令牌?
我的一个想法是在代表 CLS 令牌的位置 0 处添加一个 100-dim 嵌入,并在代表 SEP 令牌的位置 90+1=91 上添加一个 100-dim 嵌入。但我不知道我应该为这两个令牌使用什么嵌入。而且我也不确定这是否是一个好的解决方案。
有任何想法吗?
(我试着在 Huggingface 论坛上问这个问题,但没有得到任何回应。)
python time-series deep-learning bert-language-model huggingface-transformers
recipe: default.v1
*# Configuration for Rasa NLU.
# https://rasa.com/docs/rasa/nlu/components/*
language: en
pipeline:
*# how to implement this BERT in rasa*
- name: HFTransformersNLP
model_weights: "bert-base-uncased"
model_name: "bert"
- name: LanguageModelTokenizer
- name: LanguageModelFeaturizer
- name: DIETClassifier
epochs: 200
Run Code Online (Sandbox Code Playgroud) 我遇到了一个大问题。对于我的学士论文,我必须使用 BERT 制作一个机器翻译模型。但我现在一事无成。你知道文档或可以帮助我的东西吗?我已经在这个方向上阅读了一些论文,但也许有文档或教程可以帮助我。
对于我的学士论文,我必须将文本摘要翻译成标题。我希望有一个人可以帮助我。
machine-translation jupyter-notebook sequence-to-sequence bert-language-model
在 Hugginface Transformers 的代码中,有很多微调模型具有init_weight. 例如(这里),init_weight最后有一个函数。
class BertForSequenceClassification(BertPreTrainedModel):
def __init__(self, config):
super().__init__(config)
self.num_labels = config.num_labels
self.bert = BertModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.classifier = nn.Linear(config.hidden_size, config.num_labels)
self.init_weights()
Run Code Online (Sandbox Code Playgroud)
据我所知,它将调用以下代码
def _init_weights(self, module):
""" Initialize the weights """
if isinstance(module, (nn.Linear, nn.Embedding)):
# Slightly different from the TF version which uses truncated_normal for initialization
# cf https://github.com/pytorch/pytorch/pull/5617
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
elif isinstance(module, BertLayerNorm):
module.bias.data.zero_()
module.weight.data.fill_(1.0)
if isinstance(module, nn.Linear) and module.bias is not None:
module.bias.data.zero_()
Run Code Online (Sandbox Code Playgroud)
我的问题是 如果我们正在加载预训练模型,为什么我们需要为每个模块初始化权重?
我想我一定在这里误解了一些东西。
我正在尝试从 pytorch 中的 BERT 模型获取输入梯度。我怎样才能做到这一点?假设 y' = BertModel(x)。我试图找到 $d(loss(y,y'))/dx$
gradient deep-learning pytorch bert-language-model huggingface-transformers
python ×6
pytorch ×6
nlp ×4
gradient ×1
ktrain ×1
python-3.x ×1
rasa ×1
sentence ×1
time-series ×1