首页 > 解决方案 > 使用 tensorflow 构建自定义模型时无法获取摘要

问题描述

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

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()

我已经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.

我在这里想念什么?

标签: pythontensorflowtensorflow2.0

解决方案


我能够通过修改线路来建立网络

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

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

来自张量流文档

调用.builds它的层。


推荐阅读