简体   繁体   English

如何访问和可视化预训练的 TensorFlow 2 模型中的权重?

[英]How to access and visualize the weights in a pre-trained TensorFlow 2 model?

So, I have re-trained a pre-trained ResNet50 V2 model in TensorFlow 2 using the tf.keras framework with two Dense layers added to the top.因此,我使用 tf.keras 框架在 TensorFlow 2 中重新训练了一个预训练的 ResNet50 V2 模型,并在顶部添加了两个 Dense 层。 Now I want to visualize the weights in the layers within the base ResNet model.现在我想以可视化的权重基本RESNET模型的层。 However, reloading the saved model with但是,重新加载保存的模型

model = tf.keras.models.load_model(path/to/model.hdf5)
model.summary()

results in结果是

在此处输入图片说明

As you can see, the layers of ResNet model are not individually listed, meaning that calling可以看到,ResNet 模型的层数没有单独列出,这意味着调用

model.layers[0].get_weights()[1]

will only result in只会导致

[7 7 3 64]

Thus, how do I access the weights inside each of the layers in the base ResNet50 V2 model?因此,如何访问基本 ResNet50 V2 模型中每个层内的权重?

The right answer here was to write这里的正确答案是写

model.layers[0].summary()

instead of代替

model.summary()

Which will let me then see what all the layers are within the pre-trained model.这将让我看到预训练模型中的所有层。 Thus, writing因此,写

model.layers[0].layers[0].get_weights(name='input_1')

will give me the weights of the input to the ResNet base model.会给我输入到 ResNet 基础模型的权重。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM