标签: bert-language-model

Tensorflow 2.X 错误 - 在 Colab 上运行的二进制文件中未注册操作类型“CaseFoldUTF8”

我已经使用 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. …

tensorflow2.0 bert-language-model

2
推荐指数
1
解决办法
4930
查看次数

访问预训练 DistilBERT 模型多层的输出

我正在尝试从预训练的“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

1
推荐指数
1
解决办法
969
查看次数

Roberta 模型预处理文本中的混乱

我想应用 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, …

nlp pytorch bert-language-model huggingface-transformers

1
推荐指数
1
解决办法
1678
查看次数

BERT 编码层在评估期间为所有输入生成相同的输出 (PyTorch)

我不明白为什么我的 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)

python nlp pre-trained-model pytorch bert-language-model

1
推荐指数
1
解决办法
2524
查看次数

无法 pip install -U 语句转换器

我无法做到: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 匹配的分布(来自句子转换器)有人可以帮忙吗?

transformer-model sentence bert-language-model

1
推荐指数
1
解决办法
3575
查看次数

如何将另一个预训练的 BERT 模型与 ktrain 文本分类器结合使用?

我们如何为 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)

有人该怎么做吗?谢谢!

python bert-language-model ktrain

1
推荐指数
1
解决办法
2348
查看次数

Bert 词嵌入的微调

我想加载一个预先训练的 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

1
推荐指数
1
解决办法
7332
查看次数

Pytorch - 在 GPU 上训练时,在设备 1 上的副本 1 中捕获 StopIteration 错误

我正在尝试在 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)

python-3.x pytorch bert-language-model

1
推荐指数
1
解决办法
4837
查看次数

BERT 用于时间序列分类

我想在时间序列数据上训练一个转换器编码器(例如 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

1
推荐指数
1
解决办法
1099
查看次数

InvalidConfigException:无法加载名称“HFTransformersNLP”的类。因拉萨

如何使用 Huggingface 转换器在 rasa 中实现 BERT,以及在 rasa 中运行 Bert 模型需要什么?

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)

nlp bert-language-model rasa huggingface-transformers

1
推荐指数
1
解决办法
1753
查看次数

我如何使用 BERT 进行机器翻译?

我遇到了一个大问题。对于我的学士论文,我必须使用 BERT 制作一个机器翻译模型。但我现在一事无成。你知道文档或可以帮助我的东西吗?我已经在这个方向上阅读了一些论文,但也许有文档或教程可以帮助我。

对于我的学士论文,我必须将文本摘要翻译成标题。我希望有一个人可以帮助我。

machine-translation jupyter-notebook sequence-to-sequence bert-language-model

0
推荐指数
1
解决办法
1797
查看次数

为什么我们在 Huggingface Transformers 的 BERT 预训练模型中需要 init_weight 函数?

在 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)

我的问题是 如果我们正在加载预训练模型,为什么我们需要为每个模块初始化权重?

我想我一定在这里误解了一些东西。

python bert-language-model huggingface-transformers

0
推荐指数
1
解决办法
2817
查看次数

Pytorch BERT 输入梯度

我正在尝试从 pytorch 中的 BERT 模型获取输入梯度。我怎样才能做到这一点?假设 y' = BertModel(x)。我试图找到 $d(loss(y,y'))/dx$

gradient deep-learning pytorch bert-language-model huggingface-transformers

0
推荐指数
1
解决办法
687
查看次数