我在尝试运行脚本时遇到此错误:
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)。
如有任何帮助,我们将不胜感激。
如果有 7 个
Input
层,则需要 7 个输入。问题在于,网络不会将 (332240, 6, 160, 160)
的 img_array
拆分为 6 个 Input
层,而是将其作为一个输入处理,并将其全部分配给第一个 Input
,然后分配给第二个 Input
得到 int_array
并且出现形状不匹配错误。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)
请注意,这不是有用的数据,而应仅显示正确的输入形状。