首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法构建具有int输入的Keras图层

无法构建具有int输入的Keras图层
EN

Stack Overflow用户
提问于 2019-06-27 15:45:56
回答 1查看 3.7K关注 0票数 19

我有一个复杂的keras模型,其中一个层是一个自定义的预训练层,它期望"int32“作为输入。此模型作为继承自model的类来实现,其实现方式如下所示:

代码语言:javascript
复制
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方法时,它会抛出下一个错误:

代码语言:javascript
复制
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).

我怎么才能修复它?

EN

回答 1

Stack Overflow用户

发布于 2021-11-21 06:09:39

使用model.build构建模型时抛出异常。

model.build函数根据给定的输入形状建立模型。

引发该错误的原因是,当我们尝试构建模型时,它首先调用一个带有x参数的模型,具体取决于以下代码中的输入形状类型

代码语言:javascript
复制
if (isinstance(input_shape, list) and
    all(d is None or isinstance(d, int) for d in input_shape)):
  input_shape = tuple(input_shape)
if isinstance(input_shape, list):
  x = [base_layer_utils.generate_placeholders_from_shape(shape)
        for shape in input_shape]
elif isinstance(input_shape, dict):
  x = {
      k: base_layer_utils.generate_placeholders_from_shape(shape)
      for k, shape in input_shape.items()
  }
else:
  x = base_layer_utils.generate_placeholders_from_shape(input_shape)

X在这里是一个TensorFlow占位符。因此,当尝试调用一个以x作为输入的模型时,它将弹出一个TypeError,除了块之外,结果将会工作并给出一个错误。

我假设您的输入形状是16x16。不使用self.build([(16,16)]) this,而是调用基于实张量的模型

代码语言:javascript
复制
inputs = tf.keras.Input(shape=(16,))
self.call(inputs)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56786328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档