- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请帮助理解以下问题的原因以及如何构建 Keras 模型以在 huggingface 的预训练模型之上进行微调。
在 TFDistilBertForSequenceClassification 之上为 DistilBERT 微调创建自定义模型来自 Huggingface。
根据分词器输出的形状,我假设它是 (2, None, 256)
作为[input_ids, attention_mask]
将进入模型。
分词器的输出。
from transformers import DistilBertTokenizerFast
tokenizer = DistilBertTokenizerFast.from_pretrained('distilbert-base-uncased')
max_sequence_length = 256
tokens = tokenizer(
" ".join(["token"] * max_sequence_length),
truncation=True,
padding=True,
max_length=max_sequence_length,
return_tensors="tf"
)
print(tokens)
---
{
'input_ids': <tf.Tensor: shape=(1, 256), dtype=int32, numpy=array([[ 101, 19204, 19204, 19204, 19204, 19204, 19204, 19204, 19204, ...]], dtype=int32)>,
'attention_mask': <tf.Tensor: shape=(1, 256), dtype=int32, numpy=array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...]], dtype=int32)>
}
model = TFDistilBertForSequenceClassification.from_pretrained('distilbert-base-uncased')
for layer in model.layers:
if layer.name == "distilbert":
layer.trainable = False
model.summary()
---
Model: "tf_distil_bert_for_sequence_classification_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
distilbert (TFDistilBertMain multiple 66362880
_________________________________________________________________
pre_classifier (Dense) multiple 590592
_________________________________________________________________
classifier (Dense) multiple 1538
_________________________________________________________________
dropout_99 (Dropout) multiple 0
=================================================================
Total params: 66,955,010
Trainable params: 592,130
Non-trainable params: 66,362,880
使用 Sequential 在预训练模型之上添加了一个 Keras Dense 层。
seq = Sequential([
model,
Dense(
name="output_softmax",
units=2,
activation="softmax"
)
])
seq.compile(
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer=tf.keras.optimizers.Adam()
)
The base Layer class说构建方法创建权重。
build(self, input_shape): This method can be used to create weights that depend on the shape(s) of the input(s), using add_weight(). call() will automatically build the layer (if it has not been built yet) by calling build().
运行该方法但出现错误。
seq.build(input_shape=(2, None, max_sequence_length))
---
...
ValueError: You cannot build your model by calling `build` if your layers do not support float-type inputs. Instead, in order to instantiate and build your model, `call` your model on real tensor data (of the correct type).
根据错误消息,将分词器输出提供给模型并得到另一个错误。
seq(tokens)
---
TypeError: Failed to convert 'TFSequenceClassifierOutput(loss=None, logits=TensorShape([1, 2]), hidden_states=None, attentions=None)' to a shape: ''logits''could not be converted to a dimension. A shape should either be single dimension (e.g. 10), or an iterable of dimensions (e.g. [1, 10, None]).
python --version
---
Python 3.7.10
print(tf.__version__)
---
2.5.0
print(transformers.__version__)
---
4.8.2
最佳答案
在不使用 Sequential 或 build 方法的情况下,您尝试过 Keras Functional API 吗?-(我尝试过并使用其他预训练模型)s.a:-(伪代码)
def custom():
x=Input(shape=(256,))
y=Input(shape=(256,))
out=distilbertlayer([x,y])
out=Dense(2,activation='softmax')(out)
mod=tf.keras.models.Model([x,y],out)
return mod
custommodel=custom()
在这里,在给定的信息中。我认为错误是由于传递给自定义密集层的输出类型错误。作为建议,您可以尝试将不同的输出从 distilbertlayer 传递到自定义密集层,例如
out=distilbertlayer([x,y])
out=Dense(2,activation='softmax')(out[:,0])
但是,首先应该了解 distilberlayer 的输出格式。
关于tensorflow - Huggingface 微调——如何在预训练的基础上构建自定义模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68233265/
如何将 HuggingFace 数据集写入磁盘? 我使用 JSONL 文件制作了自己的 HuggingFace 数据集: Dataset({features: ['id', 'text'],num_r
是否有任何方法可以将两个评估数据集传递给 HuggingFace Trainer 对象,以便在训练期间可以在两个不同的集合(比如分布内和分布外集合)上评估训练模型?这是对象的实例化,它只接受一个 ev
想做类似的事情 tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') model = BertModel.from_pretra
来自文档 for from_pretrained ,我知道我不必每次都下载预训练的向量,我可以使用以下语法保存它们并从磁盘加载: - a path to a `directory` contain
默认缓存目录磁盘容量不足,我需要更改默认缓存目录的配置。 最佳答案 您可以在每次加载模型时指定缓存目录 .from_pretrained通过设置参数cache_dir .您可以通过导出环境变量 TRA
我正在使用 T5 模型和分词器执行下游任务。我想将某些 whitesapces 添加到分词器,例如行结尾 (\t) 和制表符 (\t)。添加这些标记是有效的,但不知何故标记器总是忽略第二个空格。因此,
我正在查看 Huggingface pipeline for Named Entity Recognition 的文档,我不清楚这些结果如何用于实际的实体识别模型。 例如,给出文档中的示例: >>>
我正在构建基于 Huggingface Longformer 的分类器。下面是我的主要代码 model = LongformerForSequenceClassification.from_pretr
我最近根据源代码对以下代码进行了测试: https://github.com/cl-tohoku/bert-japanese/blob/master/masked_lm_example.ipynb i
运行下面的代码下载一个模型 - 有谁知道它下载到哪个文件夹? !pip install -q transformers from transformers import pipeline model
我正在用变形金刚练习总结文本。 按照以下教程:https://huggingface.co/transformers/usage.html#summarization from transformer
我收到以下错误: AssertionError:文本输入必须为 str(单个示例)、List[str](批处理或单个预标记示例)或 List[List[str]](预标记示例批处理)类型。,当我运行
问题 请帮助理解以下问题的原因以及如何构建 Keras 模型以在 huggingface 的预训练模型之上进行微调。 目标 在 TFDistilBertForSequenceClassificatio
我希望摘要任务通常假定长文档。但是,遵循文档 here ,我所做的任何简单摘要调用都表明我的文档太长: >>> summarizer = pipeline("summarization") >>> s
我在 Colab Notebook 上下载了预训练模型后,它会在我重置 notebook 变量后消失。有没有办法可以下载模型以供第二次使用? tokenizer = BertTokenizer.fro
特尔;博士: 我的模型总是预测相同的标签,我不知道为什么。下面是我的整个微调代码,希望有人能指出我哪里出错了。 我正在使用 Huggingface 的 TFBertForSequenceClassif
我的编码功能如下所示: from transformers import BertTokenizer, BertModel MODEL = 'bert-base-multilingual-uncase
我想使用 HuggingFace 的转换器使用预训练的 "xlm-mlm-xnli15-1024" 将中文翻译成英文模型。 This tutorial显示如何从英语到德语。 我尝试按照教程进行操作,但
我想添加额外的 Dense预训练后的层 TFDistilBertModel , TFXLNetModel和 TFRobertaModel抱脸模特。我已经看到如何使用 TFBertModel 做到这一点
我正在尝试将预训练的 HuggingFace ALBERT 更改器(mutator)模型应用于我自己的文本分类任务,但损失并没有减少到某个点。 这是我的代码: 我的文本分类数据集中有四个标签: 0,
我是一名优秀的程序员,十分优秀!