【问题标题】:Input layer 0 of sequence is incompatible with the layer - CNNs序列的输入层 0 与层不兼容 - CNNs
【发布时间】:2022-06-21 23:24:03
【问题描述】:

我正在尝试使用超参数化来创建 CNN 模型以进行图像分类。当我运行代码时,我收到以下错误:

ValueError: Input 0 of layer "sequential" is in compatible with the layer: expected shape=(None, 32, 32, 32, 3), found shape=(32, 32, 32, 3)

如何修复错误?这是下面粘贴的整个代码:


# first we create our actual code which requires the arguments, units, activation, dropout, lr:
def build_model(hp):
  model = ks.Sequential([
  # adding first conv2d layer
  ks.layers.Conv2D(
            #Let's tune the filters, kernel_size, activation function.
            filters = hp.Int("conv_1_filter", min_value=1,max_value=100, step = 16),
            kernel_size = hp.Choice("conv_1_kernel", values = [3,5]),
            activation = hp.Choice("conv_1_activation", ["relu", "tanh", "softmax"]),
            input_shape = (32,32,32,3) 
   ),
  # adding second conv2d layer
  ks.layers.Conv2D(
            #Let's tune the filters, kernel_size, activation function.
            filters = hp.Int("conv_2_filter", min_value=1,max_value=50, step = 16),
            kernel_size = hp.Choice("conv_2_kernel", values = [3,5]),
            activation = hp.Choice("conv_2_activation", ["relu", "tanh", "softmax"]),
            input_shape = (32,32,32,3)
  )])
  model.add(layers.Flatten())

  # Let's tune the number of Dense layers.
  for i in range(hp.Int("num_dense_layers", 1, 3)):
    model.add(
        layers.Dense(
            # Let's tune the number of units separately
            units = hp.Int(f"units_{i}", min_value=1, max_value = 100, step = 16),
            activation = hp.Choice("activation", ["relu", "tanh", "softmax"])
        ))

  if hp.Boolean("dropout"):
    model.add(layers.Dropout(rate = 0.25))

  model.add(layers.Dense(10, activation = "softmax"))
  learning_rate = hp.Float("lr", min_value = 1e-4, max_value = 1e-2, sampling="log")
  model.compile(
      optimizer = ks.optimizers.Adam(learning_rate = learning_rate),
      loss = "categorical_crossentropy",
      metrics = ["accuracy"]
  )  
  return model

  build_model(keras_tuner.HyperParameters())

【问题讨论】:

    标签: tensorflow conv-neural-network valueerror


    【解决方案1】:

    由于输入形状不匹配,您会收到此错误。

    在这里,我在 mnist 时尚数据集上实现了超模型,其中包含形状 (28,282,1) 的图像。

    def build_model(hp):
      model = tf.keras.Sequential([
      tf.keras.Input(shape=(28,28,1)),
      # adding first conv2d layer
      tf.keras.layers.Conv2D(
                #Let's tune the filters, kernel_size, activation function.
                filters = hp.Int("conv_1_filter", min_value=1,max_value=100, step = 16),
                kernel_size = hp.Choice("conv_1_kernel", values = [3,5]),
                activation = hp.Choice("conv_1_activation", ["relu", "tanh", "softmax"]),
                input_shape = (28,28,1) 
       ),
      tf.keras.layers.MaxPooling2D(
          pool_size=hp.Choice('pooling_1',values=[2,3])),
    
      # adding second conv2d layer
      tf.keras.layers.Conv2D(
                #Let's tune the filters, kernel_size, activation function.
                filters = hp.Int("conv_2_filter", min_value=1,max_value=50, step = 16),
                kernel_size = hp.Choice("conv_2_kernel", values = [3,5]),
                activation = hp.Choice("conv_2_activation", ["relu", "tanh", "softmax"]),
                input_shape = (28,28,1)
      )])
      tf.keras.layers.MaxPooling2D(
          pool_size=hp.Choice('pooling_2',values=[2,3])),
    
      model.add(tf.keras.layers.Flatten())
    
      if hp.Boolean("dropout"):
        model.add(tf.keras.layers.Dropout(rate = 0.25))
    
      model.add(tf.keras.layers.Dense(10, activation = "softmax"))
      learning_rate = hp.Float("lr", min_value = 1e-4, max_value = 1e-2, sampling="log")
      model.compile(
          optimizer = tf.keras.optimizers.Adam(learning_rate = learning_rate),
          loss = "categorical_crossentropy",
          metrics = ["accuracy"]
      )  
      return model
    

    通过提供正确的形状,您不会得到任何错误。

    更多详情,请参考gist和documentation。谢谢!

    【讨论】:

      猜你喜欢
      • 2020-11-14
      • 1970-01-01
      • 2020-10-25
      • 2021-07-04
      • 2021-05-31
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      相关资源
      最近更新 更多