python - 使用 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]))
来自张量流文档:
调用
.builds
它的层。
推荐阅读
- javascript - 如何使用 Jest 对 typeorm getRepository 进行单元测试?
- javascript - Telegram Bot:从私人群组转发消息
- python - 如何为python类中的私有属性编写getter-setter方法?
- bash - bash 到 zsh 的实际语法差异,例如 'if' 中的括号
- jsf - Primefaces - 有条件地自定义 p:confirmDialog 内容
- crystal-lang - 如何在 Crystal-lang 中打印当前用户名?
- python - 非整数n的En函数的python实现?
- python - 在 PyCharm 中更改“查找用法”的默认范围
- c++ - 无法在 DirectX11 中找出 StructuredBuffers
- lotus-notes - 将文本附加到 RichText 项目(“正文”)会导致两个正文项目...为什么?