使用张量流构建自定义模型时无法获取摘要
Unable to get the summary while building custom model using 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.
我在这里错过了什么?
我能够通过修改线路构建网络
model.build(input_shape=input_shape) # Note the .build call
和
_ = model(tf.zeros([1,10]))
Calling the layer .builds
it.
我有一个非常简单的模型,如下所示:
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.
我在这里错过了什么?
我能够通过修改线路构建网络
model.build(input_shape=input_shape) # Note the .build call
和
_ = model(tf.zeros([1,10]))
Calling the layer
.builds
it.