tensorflow - How to get summary graph of custom (subclass) Keras layer?
问题描述
How do you print a summary() of the layers of a custom layer?
model.summary() prints a beautiful summary graph of the entire model, but the subclass layer called 'magic_layer' here, which has many layers within it, is aggregated...
Model: "transformer"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
positioning (PositionalEncod (None, 504, 6) 0
_________________________________________________________________
magic_layer (CustomLayer) (None, 6, 504) 3088040
_________________________________________________________________
g_pooling (GlobalAveragePool (None, 504) 0
_________________________________________________________________
dropout_2 (Dropout) (None, 504) 0
_________________________________________________________________
dense_2 (Dense) (None, 32) 16160
_________________________________________________________________
dropout_3 (Dropout) (None, 32) 0
_________________________________________________________________
dense_3 (Dense) (None, 5) 165
=================================================================
Total params: 3,104,365
Trainable params: 3,104,365
Non-trainable params: 0
_________________________________________________________________
If you have a custom-defined Tensorflow/Keras layer (read more about that here: Making new layers and models via subclassing - Francis Chollet) then the summary call won't break out all the layers in that sublayer. 'magic_layer' in this example, is the subclass layer that I'm interested in.
How could you get this same print-out of sublayers for the layer called 'magic_layer' in this example?
model.layers[1].summary()
does not work unfortunately... Perhaps I need to include a summary def in the custom layer class, but I was hoping there was a way of inheriting this functionality from the model class.
解决方案
由于模型是层的子类,只需从 tf.keras.Model 而不是 tf.keras.layers.Layer 制作自定义层的子类。现在您可以通过 summary() 打印“层”的摘要。
model.summary 不是递归的——它不会打印嵌入式模型的摘要。如果需要,您必须自己编写,或者仅根据原始源创建自己的摘要函数。
推荐阅读
- javascript - 如何使用 ES6 功能减少 3 个 for 循环以提高 javascript 性能?
- angular - Angular 7 如何在打字稿中使用给定的 API 引用
- python-3.x - 错误:没有为 webdriver 找到匹配的发行版
- javascript - 如何在具有大型数据集的 JavaScript 中使用图表
- c# - 如何修复错误类型“IObjectContainer”都存在于两者中
- ruby-on-rails - 源代码中有图像标签时无法加载图像?
- react-native - React Native 打包器:如何同时拥有平台和视网膜后缀
- javascript - 在vue组件中导入promise时无法设置未定义的属性
- php - mysql 数据库错误:在 null 上调用成员函数 query()
- python - 如何在 Python 应用程序中存储和使用 300KB 的查找表?