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)