【问题标题】:Is there an easy way to get something like Keras model.summary in Tensorflow?有没有一种简单的方法可以在 Tensorflow 中获得类似 Keras model.summary 的东西?
【发布时间】:2017-10-04 08:27:54
【问题描述】:

我一直在使用 Keras,非常喜欢 model.summary() 它很好地概述了不同层的大小,尤其是模型具有的参数数量的概述。

Tensorflow 中是否有类似的功能?我在 Stackoverflow 或 Tensorflow API 文档上一无所获。

【问题讨论】:

    标签: tensorflow keras


    【解决方案1】:

    看来你可以使用Slim

    例子:

    import numpy as np
    
    from tensorflow.python.layers import base
    import tensorflow as tf
    import tensorflow.contrib.slim as slim
    
    x = np.zeros((1,4,4,3))
    x_tf = tf.convert_to_tensor(x, np.float32)
    z_tf = tf.layers.conv2d(x_tf, filters=32, kernel_size=(3,3))
    
    def model_summary():
        model_vars = tf.trainable_variables()
        slim.model_analyzer.analyze_vars(model_vars, print_info=True)
    
    model_summary()
    

    输出:

    ---------
    Variables: name (type shape) [size]
    ---------
    conv2d/kernel:0 (float32_ref 3x3x3x32) [864, bytes: 3456]
    conv2d/bias:0 (float32_ref 32) [32, bytes: 128]
    Total size of variables: 896
    Total bytes of variables: 3584
    

    这里还有一个打印模型摘要的自定义函数示例: https://github.com/NVlabs/stylegan/blob/f3a044621e2ab802d40940c16cc86042ae87e100/dnnlib/tflib/network.py#L507

    如果您已经有.pb tensorflow 模型,您可以使用:inspect_pb.py 打印模型信息或使用带有--print_structure 标志的tensorflow summarize_graph 工具,它还可以检测输入和输出名称。

    【讨论】:

    • 出于某种原因,这只给了我可变的大小和信息。
    • @Blade 哪一个?
    • model_summary() 函数。
    • 这也适用于 TensorFlow Lite 模型! (似乎只有当它们是使用 TensorFlow 构建时)
    • Slim 在 Tensorflow 中不再可用
    【解决方案2】:

    我还没有看到类似 model.summary() 的 tensorflow 的东西......但是,我认为你不需要它。有一个 TensorBoard,您可以在其中轻松检查 NN 的架构。

    https://www.tensorflow.org/get_started/graph_viz

    【讨论】:

    • 张量板如何知道每一层的形状和参数个数?
    • 这不能回答 OP 问题。
    • @BiBi 是!见问题something like Keras model.summary
    【解决方案3】:

    您可以将 keras 与 tensorflow 后端一起使用,以获得 keras 或 tensorflow 的最佳功能。

    【讨论】:

    • 了解,但是,我在 Tensorflow 中有现有模型(例如由其他人编程),我想更好地理解这些模型,因此,我正在寻找一种从 Tensorflow 获得更多输出的方法。跨度>
    • 查看 tensorflow 训练的最佳方法是在代码中添加摘要并使用 tensorboard。
    • 有没有一种快速的方法可以将 tensorflow 模型或图形转换为 keras 模型,然后我们可以只做 model.summary()。真的很怀念这种快速的文字方式,(理解有张量板。)
    • 您可以从 keras 转到 tf,但不能反过来,因为 tf 图比 keras 图低。
    • 这应该是一条评论,因为它没有提供解决问题的答案(“只是不要使用 X,而是使用 Y”而是有资格作为建议)。
    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2011-04-20
    • 2020-12-16
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    相关资源
    最近更新 更多