我正在尝试从预训练的“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
下面是我微调的结果。
Training Loss Valid. Loss Valid. Accur. Training Time Validation Time
epoch
1 0.16 0.11 0.96 0:02:11 0:00:05
2 0.07 0.13 0.96 0:02:19 0:00:05
3 0.03 0.14 0.97 0:02:22 0:00:05
4 0.02 0.16 0.96 0:02:21 0:00:05
Run Code Online (Sandbox Code Playgroud)
接下来我尝试使用该模型来预测 csv 文件中的标签。我创建了一个标签列,将类型设置为 int64 并运行预测。
print('Predicting labels for {:,} test sentences...'.format(len(input_ids)))
model.eval()
# Tracking variables
predictions , true_labels = [], []
# Predict
for batch in prediction_dataloader:
# Add batch to GPU
batch = tuple(t.to(device) for t in batch)
# Unpack the inputs from …Run Code Online (Sandbox Code Playgroud) python nlp machine-learning pytorch 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),用于可以建模为分类的任务。在谈论我面临的问题之前,让我们先简要描述一下我正在使用的数据。
我正在使用 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
from transformers import CTRLTokenizer, TFCTRLLMHeadModel
tokenizer_ctrl = CTRLTokenizer.from_pretrained('ctrl', cache_dir='./cache', local_files_only=True)
model_ctrl = TFCTRLLMHeadModel.from_pretrained('ctrl', cache_dir='./cache', local_files_only=True)
print(tokenizer_ctrl)
gen_nlp = pipeline("text-generation", model=model_ctrl, tokenizer=tokenizer_ctrl, device=1, return_full_text=False)
Run Code Online (Sandbox Code Playgroud)
你好,我的代码可以将变压器模型(例如这里的 CTRL)加载到 GPU 内存中。使用后如何将其从 GPU 中删除,以释放更多 GPU 内存?
显示我使用torch.cuda.empty_cache()?
谢谢。
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) 有没有办法使用 返回概率和实际类别Trainer.predict?
我检查了此页面的文档但无法弄清楚。截至目前,它似乎正在返回 logits
显然,概率和实际类别都可以使用额外的编码来计算,但想知道是否有任何预构建的方法可以执行相同的操作
我当前的输出如下
new_predictions=trainer.predict(dataset_for_future_predicition_after_tokenizer)
new_predictions
PredictionOutput(predictions=array([[-0.43005577, 3.646306 , -0.8073783 , -1.0651836 , -1.3480505 ,
-1.108013 ],
[ 3.5415223 , -0.8513837 , -1.8553216 , -0.18011567, -0.35627165,
-1.8364134 ],
[-1.0167522 , -0.8911268 , -1.7115675 , 0.01204597, 1.7177908 ,
1.0401527 ],
[-0.82407415, -0.46043932, -1.089274 , 2.6252217 , 0.33935028,
-1.3623345 ]], dtype=float32), label_ids=None, metrics={'test_runtime': 0.0182, 'test_samples_per_second': 219.931, 'test_steps_per_second': 54.983})
Run Code Online (Sandbox Code Playgroud) 我尝试使用 wav2vec2 (XLSR 模型)但没有成功:
import transformers
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
import librosa
import torch
wav2vec2_processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-large-xlsr-53")
wav2vec2_model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-large-xlsr-53")
file_name = "test.wav"
speech, sr = librosa.load(file_name, sr=16000)
input_values = wav2vec2_processor(speech, sampling_rate=16000, return_tensors="pt").input_values
logits = wav2vec2_model(input_values).logits
Run Code Online (Sandbox Code Playgroud)
错误:
OSError: Can't load tokenizer for 'facebook/wav2vec2-large-xlsr-53'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure 'facebook/wav2vec2-large-xlsr-53' is the correct path to a directory containing all relevant files …Run Code Online (Sandbox Code Playgroud) deep-learning huggingface-transformers huggingface-tokenizers huggingface
目前,我正在尝试按照Huggingface 课程的内容建立一个提取式 QA 管道。在那里,他们展示了如何创建compute_metrics()函数来在训练后评估模型。但是,我想知道是否有一种方法可以获取这些训练指标,并将compute_metrics() 函数直接传递给训练器。他们仅使用训练损失进行训练,我想获得训练的评估 f1 分数。
但是,正如我所见,这可能有点棘手,因为他们需要原始跨度来计算小队指标,但您无法将这些原始跨度传递到标记化训练数据集上。
predicted_answer = {'id': '56be4db0acb8001400a502ec', 'prediction_text': 'Denver Broncos'}
theoretical_answer = {'id': '56be4db0acb8001400a502ec', 'answers': {'text': ['Denver Broncos', 'Denver Broncos', 'Denver Broncos'], 'answer_start': [177, 177, 177]}}
metric.compute(predictions=predicted_answers, references=theoretical_answers)
Run Code Online (Sandbox Code Playgroud)
这就是为什么他们创建整个compute_metrics()函数,除了评估循环中输出的预测之外还采用一些额外的参数,因为他们需要重建这些跨度。
Trainer对象一起使用?我需要一个能够对未知数量的类(即数量可能随着时间的推移而增长)的文本进行分类的模型。零样本文本分类的蕴涵方法似乎是我问题的解决方案,我尝试的模型facebook / bart-large-mnli在我的注释数据上表现不佳。有没有办法在不损失模型稳健性的情况下对其进行微调?
我的数据集如下所示:
# http://groups.di.unipi.it/~gulli/AG_corpus_of_news_articles.html
World, "Afghan Army Dispatched to Calm Violence KABUL, Afghanistan - Government troops intervened in Afghanistan's latest outbreak of deadly fighting between warlords, flying from the capital to the far west on U.S. and NATO airplanes to retake an air base contested in the violence, officials said Sunday..."
Sports, "Johnson Helps D-Backs End Nine-Game Slide (AP) AP - Randy Johnson took a four-hitter into the ninth inning to help the Arizona Diamondbacks end a …Run Code Online (Sandbox Code Playgroud)