- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
出于实验目的,我需要访问编码器的嵌入层。也就是说,假设 Tensorflow 实现,该层定义为 tf.keras.layers.Embedding(...)。
例如,在转换器的编码器部分设置 Embedding() 层的 'embeddings_regularizer=' 参数的方法是什么?
最佳答案
您可以像任何其他模型一样迭代 BERT 模型,如下所示:
for layer in model.layers:
if isinstance(layer ,tf.keras.layers.Embedding):
layer.embeddings_regularizer = argument
isinstance
检查图层的类型,因此您实际上可以在此处放置任何图层类型并更改您需要的内容。
我还没有具体检查 embeddings_regularizer
是否可用,但是如果您想查看该特定层可以使用哪些方法,请运行调试器并调用 dir(layer)
code> 在上面的函数中。
更新问题
TFBertForSequenceClassification 模型有 3 层:
>>> model.summary()
Model: "tf_bert_for_sequence_classification"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
bert (TFBertMainLayer) multiple 108310272
_________________________________________________________________
dropout_37 (Dropout) multiple 0
_________________________________________________________________
classifier (Dense) multiple 1538
=================================================================
Total params: 108,311,810
Trainable params: 108,311,810
Non-trainable params: 0
同样,调用 model.layers
会得到:
[<transformers.models.bert.modeling_tf_bert.TFBertMainLayer at 0x7efda85595d0>,
<tensorflow.python.keras.layers.core.Dropout at 0x7efd6000ae10>,
<tensorflow.python.keras.layers.core.Dense at 0x7efd6000afd0>]
我们可以访问TFBERtMainLayer
内部的层:
>>> model.layers[0]._layers
[<transformers.models.bert.modeling_tf_bert.TFBertEmbeddings at 0x7efda8080f90>,
<transformers.models.bert.modeling_tf_bert.TFBertEncoder at 0x7efda855ced0>,
<transformers.models.bert.modeling_tf_bert.TFBertPooler at 0x7efda84f0450>,
DictWrapper({'name': 'bert'})]
因此,从上面我们可以通过以下方式访问 TFBertEmbeddings 层:
model.layers[0].embeddings
OR
model.layers[0]._layers[0]
如果您检查 documentation (搜索“TFBERtEmbeddings”类)您可以看到它继承了标准的 tf.keras.layers.Layer 这意味着您可以访问所有正常的正则化器方法,因此您应该能够调用像这样:
from tensorflow.keras import regularizers
model.layers[0].embeddings.activity_regularizer = regularizers.l2(1e-5)
或者您需要更改的任何参数/正则化器。请参阅here用于正则化器文档。
关于tensorflow - 如何访问 Huggingface 预训练 BERT 模型的特定层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67052427/
如何将 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,
我是一名优秀的程序员,十分优秀!