【问题标题】:Keras: difference of InputLayer and InputKeras:InputLayer和Input的区别
【发布时间】:2018-02-19 04:23:25
【问题描述】:

我使用 Keras 和 Tensorflow 制作了一个模型。我将Inputlayer 与这些代码行一起使用:

img1 = tf.placeholder(tf.float32, shape=(None, img_width, img_heigh, img_ch))
first_input = InputLayer(input_tensor=img1, input_shape=(img_width, img_heigh, img_ch)) 
first_dense = Conv2D(16, 3, 3, activation='relu', border_mode='same', name='1st_conv1')(first_input)

但我收到此错误:

ValueError: Layer 1st_conv1 was called with an input that isn't a symbolic tensor. Received type: <class 'keras.engine.topology.InputLayer'>. Full input: [<keras.engine.topology.InputLayer object at 0x00000000112170F0>]. All inputs to the layer should be tensors.

当我像这样使用Input 时,它工作正常:

first_input = Input(tensor=img1, shape=(224, 224, 3), name='1st_input')
first_dense = Conv2D(16, 3, 3, activation='relu', border_mode='same', name='1st_conv1')(first_input)

InputlayerInput 有什么区别?

【问题讨论】:

    标签: tensorflow keras


    【解决方案1】:

    根据 tensorflow 网站,“一般建议通过 Input 使用功能层 API,(创建 InputLayer)而不直接使用 InputLayer。” 在此页面了解更多信息here

    【讨论】:

      【解决方案2】:
      • InputLayer 是一个层。
      • Input 是张量。

      您只能调用将张量传递给它们的层。

      想法是:

      outputTensor = SomeLayer(inputTensor)
      

      所以,只有Input 可以通过,因为它是张量。

      老实说,我不知道InputLayer 存在的原因。也许它应该在内部使用。我从来没有用过它,而且我似乎永远不需要它。

      【讨论】:

      • 然而,当在 Keras 中创建输入并将模型序列化为 json 时,输入被转换为 InputLayer...所以我猜它不仅仅是内部的。不知道为什么。
      • 我也不知道它是干什么用的,但是我的同事说它可以用来指定输入形状,而不需要.compile() 模型(无论如何Sequential())。
      • 与编译绝对没有关系。编译用于将优化器、指标和损失添加到图中。你需要为训练而编译,你不需要为其他事情编译。
      猜你喜欢
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-15
      • 2020-10-02
      • 2015-11-14
      • 2020-11-30
      • 1970-01-01
      相关资源
      最近更新 更多