使用tensorflow时Python中的值错误

fcg9iug3  于 2023-10-23  发布在  Python
关注(0)|答案(1)|浏览(174)

我在尝试运行脚本时遇到此错误:

Input 0 of layer "conv2d_1" is incompatible with the layer: expected min_ndim=4, 
found ndim=2. Full shape received: (None, 6).

我有一个形状为(332240,6,160,160)的输入数据。基本上对于每个目标数据,我想输入6个图像。我还有一个形状(332240,6)的二级数据集,在我展平图像后,我将其与图像数据连接起来。下面是我用来构建模型的代码。

image_inputs = [Input(shape=(160,160,1) for _ in range(6)]
    array_input = Input(shape=(6,)

    # Convolutional Layer
    conv_outputs = []
    for imgs in image_inputs:
      
      x = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform')(imgs)
      x = MaxPooling2D((2, 2))(x)
  
      # Flatten the convolutional output
      x = Flatten()(x)
      conv_outputs.append(x)
    
    #concatenate the flattened images together 
    conv_concat = Concatenate()(conv_outputs)
    
    # Concatenate the flattened images and array inputs
    concatenated_inputs = Concatenate()([conv_concat, array_input])

    # Create the model architecture
    for units in (100,10):
        concatenated_inputs = Dense(units, activation='relu')(concatenated_inputs)

    # Output layer for regression
    output_layer = Dense(1, activation='linear')(concatenated_inputs)

    # Create the final model with inputs and output
    final_model = Model(inputs=[image_inputs, array_input], outputs=output_layer)

    # Compile the model
    if optimizer == 'sgd':
        sgd_optimizer = SGD(learning_rate = 0.01, momentum = 0.9)
        optimizer = sgd_optimizer

    final_model.compile(optimizer=optimizer, loss= 'mean_squared_error', metrics= ['mean_squared_error','mean_absolute_error'])

final_model.fit([img_array,int_array],target_array,validation_split = 0.2, verbose = 1, batch_size = 32)

img_array的形状为(332240,6,160,160),而value_array的形状为(332240,6),target_array的形状为(3332240,1)。

twh00eeo

twh00eeo1#

如果你有7个Input层,你需要7个输入。问题是,网络没有将img_array(332240, 6, 160, 160)分割到6个Input层,而是将其作为一个输入处理,并将其全部提供给第一个Input,然后第二个Input获得int_array,并且发生形状匹配错误。
以下是6个图像,一个通道和一个阵列输入,以及1000个点的目标数据:

inputs = [np.random.rand(1000, 160, 160, 1).astype(np.float32) for _ in range(6)] + [np.random.rand(1000, 6).astype('float32')]
target_array = np.random.rand(1000, 1)

请注意,这不是有用的数据,但应该只显示正确的输入形状。

相关问题