首页 > 解决方案 > TFBertForSequenceClassification Keras model.layers 信息详情为空?如何检查模型?

问题描述

我在 MacOS 上tensorflow 2.1.0使用 . 我正在使用以下方法构建 Keras 模型:transformers 2.5.1python 3.7TFBertForSequenceClassification

model = TFBertForSequenceClassification.from_pretrained('bert-base-cased',
                                                        num_labels=number_label)
model.compile(optimizer=optimizer,
                  loss=loss, 
                  metrics=[metric])

我可以探索结构:

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

正如我们从上面看到的,我们只看到了 3 个主要层,我们无法访问更多细节。

拟合模型后(不知道为什么,但如果我们不这样做,输入和输出变量将为空)我们可以访问:

model.inputs

{'attention_mask': <tf.Tensor 'attention_mask:0' shape=(None, 128) dtype=int32>,
 'input_ids': <tf.Tensor 'input_ids:0' shape=(None, 128) dtype=int32>,
 'token_type_ids': <tf.Tensor 'token_type_ids:0' shape=(None, 128) dtype=int32>}

model.outputs

[<tf.Tensor 'tf_bert_for_sequence_classification/Identity:0' shape=(None, 2) dtype=float32>]

这是一个好的开始,现在我想探索 keras 层:

model.layers

[<transformers.modeling_tf_bert.TFBertMainLayer at 0x1a415fd7d0>,
 <tensorflow.python.keras.layers.core.Dropout at 0x1a445c3550>,
 <tensorflow.python.keras.layers.core.Dense at 0x1a445c3890>]

但现在如果我尝试获取更多信息,它总是空的:

for layer in model.layers:
    print(layer.name, layer._inbound_nodes, layer._outbound_nodes)

bert [] []
dropout_37 [] []
classifier [] []

我尝试了其他一些方法,inbound_nodes但它总是空的!

有什么方法可以更详细地检查像 BERT 这样的复杂模型的层吗?我们得到空信息的原因是什么?

我也试过:

 tf.keras.utils.plot_model(model,
                          'model.png',
                          show_shapes=True)

但我得到的不是很丰富:

在此处输入图像描述

同样使用 TensorBoard,它给了我一张图和许多不连贯的元素 在此处输入图像描述

是的,作为一种选择,我很好地直接查看代码:代码,但我认为可以通过编程方式检查这种复杂模型的结构。

标签: pythontensorflowkerashuggingface-transformers

解决方案


推荐阅读