【问题标题】:Unable to get the summary while building custom model using tensorflow使用 tensorflow 构建自定义模型时无法获取摘要
【发布时间】:2020-06-30 11:39:01
【问题描述】:

我有一个非常简单的模型,如下所示:

import tensorflow as tf

class Model(tf.keras.Model):
    def __init__(self, input_shape=None, name="cus_model", **kwargs):
        super(Model, self).__init__(name=name, **kwargs)
        
    def build(self, input_shape):
        self.dense1 = tf.keras.layers.Dense(input_shape=input_shape, units=32)
        
    def call(self, input_tensor):
        return self.dense1(input_tensor)

input_shape=(1,10)
model = Model()
model.build(input_shape=input_shape) # Note the .build call
model.summary()

我已经按照this answer 添加了model.build() 调用,但我仍然收到以下错误:

ValueError: This model has not yet been built. Build the model first by calling `build()` or calling `fit()` with some data, or specify an `input_shape` argument in the first layer(s) for automatic build.

我在这里错过了什么?

【问题讨论】:

    标签: python tensorflow tensorflow2.0


    【解决方案1】:

    我可以通过修改线路来构建网络

    model.build(input_shape=input_shape) # Note the .build call
    

    _ = model(tf.zeros([1,10]))
    

    来自tensorflow documentation

    调用层.builds它。

    【讨论】:

    • 看起来不错。让 tf 通过调用模型而不是手动尝试调用 .build。
    猜你喜欢
    • 2021-12-16
    • 2017-06-20
    • 2012-03-04
    • 1970-01-01
    • 2017-03-06
    • 2018-02-24
    • 2016-08-15
    • 2022-11-20
    • 2021-09-19
    相关资源
    最近更新 更多