Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将其他层添加到Huggingface transformers_Python_Tensorflow_Keras_Nlp_Huggingface Transformers - Fatal编程技术网

Python 将其他层添加到Huggingface transformers

Python 将其他层添加到Huggingface transformers,python,tensorflow,keras,nlp,huggingface-transformers,Python,Tensorflow,Keras,Nlp,Huggingface Transformers,我想在预训练的TFDistilBertModel、TFXLNetModel和TFRobertaModel拥抱面模型之后添加额外的density层。我已经了解了如何使用TFBertModel,例如: 因此,这里我需要使用BERT输出元组的第二项(即索引为1的项)。根据TFBertModel在该元组索引处具有pooler\u输出。但是其他三种型号没有pooler\u输出 那么,我如何向其他三个模型输出添加额外的层呢?看起来pooler\u输出是一个Roberta和Bert特定的输出 但是,我们不必使

我想在预训练的
TFDistilBertModel
TFXLNetModel
TFRobertaModel
拥抱面模型之后添加额外的
density
层。我已经了解了如何使用
TFBertModel
,例如:

因此,这里我需要使用
BERT
输出元组的第二项(即索引为
1
的项)。根据
TFBertModel
在该元组索引处具有
pooler\u输出。但是其他三种型号没有
pooler\u输出


那么,我如何向其他三个模型输出添加额外的层呢?

看起来
pooler\u输出
是一个
Roberta
Bert
特定的输出

但是,我们不必使用
pooler\u输出
,而是可以在所有模型中使用一些
隐藏状态
(因此,不仅是最后一个隐藏状态),因为
隐藏状态
比一个
最后一个隐藏状态
更精确

#导入所需的模型(Bert、Roberta或DistilBert),输出_hidden_states=True
变压器模型=TFBERTFORSEQUENCECLASSION。从预训练('bert-large-cased',输出隐藏状态=真)
input_id=tf.keras.input(shape=(128),dtype='int32')
注意,mask=tf.keras.Input(shape=(128),dtype='int32')
transformer=transformer\u模型([输入\u ID,注意\u掩码])
隐藏状态=变压器[1]#获取输出隐藏状态
隐藏状态大小=4#最后状态的计数
hiddes_states_ind=列表(范围(-hidden_states_size,0,1))
选定的隐藏状态=tf.keras.layers.concatenate(tuple([hidden_states[i]表示隐藏状态中的i]))
#现在,我们可以根据需要使用选定的隐藏状态
输出=tf.keras.layers.density(128,activation='relu')(选定的隐藏状态)
输出=tf.keras.layers.density(1,activation='sigmoid')(输出)
model=tf.keras.models.model(输入=[input\u id,attention\u mask],outputs=output)
compile(tf.keras.optimizers.Adam(lr=1e-4),loss='binary\u crossentropy',metrics=['accurity'])

是否可以冻结BERT模型并只训练添加的层?(或冻结部分图层)
output = bert_model([input_ids,attention_masks])
output = output[1]
output = tf.keras.layers.Dense(32,activation='relu')(output)