【发布时间】:2019-11-09 04:44:41
【问题描述】:
我有一个复杂的 keras 模型,其中一个层是自定义预训练层,它期望“int32”作为输入。该模型实现为继承自 Model 的类,实现方式如下:
class MyModel(tf.keras.models.Model):
def __init__(self, size, input_shape):
super(MyModel, self).__init__()
self.layer = My_Layer()
self.build(input_shape)
def call(self, inputs):
return self.layer(inputs)
但是当它到达self.build 方法时,它会抛出下一个错误:
ValueError: You cannot build your model by calling `build` if your layers do not support float type inputs. Instead, in order to instantiate and build your model, `call` your model on real tensor data (of the correct dtype).
我该如何解决?
【问题讨论】:
-
我遇到了同样的错误,你找到解决办法了吗?
-
不行,我必须重构为函数式风格。
-
我明白了,我也最终使用了函数式。我还在顺序模型中使用包装器对此进行了测试,并提供了一个输入层并且它工作正常。
-
有什么更新吗?功能 API 不起作用,因为它给出了不同的错误
-
可以显示图层类的代码吗?