Tensorflow自定义层后Tensorboard中GRAPHS页面下Tag中只有Default图没有keras图

Tensorflow中在继承tf.keras.layer.Layer进行自定义层,并且将该层添加到tf.keras.Model之后,程序可以正常运行,但Tensorboard中GRAPHS页面下Tag中只有Default图没有keras图,是所有的包含自定义层的Model都不会显示Graph图吗,否则该如何修改,有什么注意事项。

其实原因很简单,如果模型中包含自定义层(或者其他子类化类可能也适用),需要实现get_config方法,之后一切都正常了

保存路径你是用的单斜杠吗,改成双斜杠如 tensorboard --logdir=F://yourpath//graph

以下答案引用自GPT-3大模型,请合理使用:

。

是的,添加自定义层的Keras模型不会在TensorBoard中可视化为图表。除非我们显式调用tf.keras.utils.plot_model API将Keras模型可视化为图表,或者在模型定义时采用tf.Graph函数。

例如,我们定义了一个包含自定义层的简单Keras模型,它将使用Sequential模型API进行构建:

import tensorflow as tf
from tensorflow.keras import layers

class MyLayer(layers.Layer):
    
    def __init__(self,output_dim,**kwargs):
        self.output_dim = output_dim
        super(MyLayer,self).__init__(**kwargs)

    def build(self,input_shape):
        # Create a trainable weight variable for this layer.
        self.kernel = self.add_weight(name='kernel', 
                                      shape=(input_shape[1], self.output_dim),
                                      initializer='uniform',
                                      trainable=True)

    def call(self, inputs):
        return tf.matmul(inputs, self.kernel)
        
# 使用Sequential模型定义模型
model = tf.keras.Sequential([MyLayer(10), 
                             layers.Activation('softmax')])

# 将模型可视化
tf.keras.utils.plot_model(model, to_file='model.png')

如果我的回答解决了您的问题,请采纳我的回答