如何绘制KerasTensorflow子类API模型?

问题描述 投票:0回答:1

我做了一个使用Keras Subclassing API正确运行的模型。该 model.summary() 也能正常工作。当尝试使用 tf.keras.utils.plot_model() 来可视化我的模型的架构,它只会输出这个图像。

enter image description here

这几乎让人觉得是Keras开发团队开的玩笑 This almost feels like a joke from the Keras development team. 这就是完整的架构。

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from sklearn.datasets import load_diabetes
import tensorflow as tf
tf.keras.backend.set_floatx('float64')
from tensorflow.keras.layers import Dense, GaussianDropout, GRU, Concatenate, Reshape
from tensorflow.keras.models import Model

X, y = load_diabetes(return_X_y=True)

data = tf.data.Dataset.from_tensor_slices((X, y)).\
    shuffle(len(X)).\
    map(lambda x, y: (tf.divide(x, tf.reduce_max(x)), y))

training = data.take(400).batch(8)
testing = data.skip(400).map(lambda x, y: (tf.expand_dims(x, 0), y))

class NeuralNetwork(Model):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.dense1 = Dense(16, input_shape=(10,), activation='relu', name='Dense1')
        self.dense2 = Dense(32, activation='relu', name='Dense2')
        self.resha1 = Reshape((1, 32))
        self.gru1 = GRU(16, activation='tanh', recurrent_dropout=1e-1)
        self.dense3 = Dense(64, activation='relu', name='Dense3')
        self.gauss1 = GaussianDropout(5e-1)
        self.conca1 = Concatenate()
        self.dense4 = Dense(128, activation='relu', name='Dense4')
        self.dense5 = Dense(1, name='Dense5')

    def call(self, x, *args, **kwargs):
        x = self.dense1(x)
        x = self.dense2(x)
        a = self.resha1(x)
        a = self.gru1(a)
        b = self.dense3(x)
        b = self.gauss1(b)
        x = self.conca1([a, b])
        x = self.dense4(x)
        x = self.dense5(x)
        return x


skynet = NeuralNetwork()
skynet.build(input_shape=(None, 10))
skynet.summary()

model = tf.keras.utils.plot_model(model=skynet,
         show_shapes=True, to_file='/home/nicolas/Desktop/model.png')
python tensorflow keras deep-learning
1个回答
1
投票

无法完成,因为基本上模型子类化,因为它在TensorFlow中实现了,与使用FunctionalSequential API创建的模型(在TF术语中称为Graph网络)相比,它的功能和能力是有限的。如果你查看 plot_model 源代码,你会看到 下面的检查model_to_dot 函数(它被 plot_model):

if not model._is_graph_network:
  node = pydot.Node(str(id(model)), label=model.name)
  dot.add_node(node)
  return dot

正如我所提到的,子类模型不是图网络,因此,这些模型只有一个包含模型名称的节点才会被绘制出来(即与你观察到的情况相同)。

这已经在一篇文章中讨论过了。Github问题 和TensorFlow的开发者之一通过给出以下论点证实了这种行为。

@omalleyt12评论道:

是的,在一般情况下,我们不能假设一个子类模型的结构。如果你的模型可以看作是Layers块,并且你希望这样可视化,我们建议你查看功能API。

© www.soinside.com 2019 - 2024. All rights reserved.