You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CNN目标数组不匹配问题求助:预期1个数组却收到300个数组

问题:CNN名字识别模型的输入形状错误排查

我尝试用CNN做名字识别与预测,为精简代码删除3个卷积层后,仍碰到输入形状相关错误,附上代码和报错信息,求解决。

代码

# split data into train and testing
train = create_label.data[:1200]
test = create_label.data[1200:]


# x train in 0 index. -1 calculates the x-train number of train 50, 50 is the image shape.
# 1 is grayscale image
X_train = np.array([i[0] for i in train]).reshape(-1, 50, 50, 1)
print(f'X_train shape is {X_train.shape}')

# y train in 1 index -1 calculates the y-train number of train 50, 50 is the image shape
y_train = [i[1] for i in train]
X_test = np.array([i[0] for i in test]).reshape(-1, 50, 50, 1)
print(f'X_test shape is {X_test.shape}')
y_test = [i[1] for i in test]



# DNN
# input_shape = input_data(shape=[50,50,1])
model = Sequential()

# 1 - Convolution
model.add(Conv2D(32,5, padding='same', input_shape=(50, 50,1)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(strides=2))
model.add(Dropout(0.8))

# 2nd Convolution layer
model.add(Conv2D(64,5, padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(strides=2))
model.add(Dropout(0.8))



# Flattening
model.add(Flatten())


# Fully connected layer 2nd layer
model.add(Dense(1024))
model.add(BatchNormalization())
model.add(Activation('softmax'))
model.add(Dropout(0.8))

model.add(Dense(3, activation='relu'))

opt = Adam(lr=0.001)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])


history = model.fit(X_train, y_train, epochs=10,
                    validation_data=(X_test, y_test))

报错信息

ValueError: 检查模型目标时出错:传递给模型的NumPy数组列表大小不符合模型预期。预期看到1个数组(对应输入['dense_1']),但实际收到300个数组的列表:[array([[0],
       [0],
       [1]]), array([[0],
       [1],
       [0]]), None, array([[0],
       [1],
       [0]]), array([[0],
       [0],
       [1]]), array([[0],
       [0],
       [1]]), arr...

问题原因及解决办法

  1. 标签数据格式错误
    当前y_train和y_test是包含None值的数组列表,Keras要求标签是二维NumPy数组(形状为(样本数, 类别数)),不是列表格式。需要同步过滤无效标签并转换格式:

    # 过滤训练集中的None标签,同步处理X和y
    train_filtered = [item for item in train if item[1] is not None]
    X_train = np.array([i[0] for i in train_filtered]).reshape(-1, 50, 50, 1)
    y_train = np.array([i[1] for i in train_filtered])
    
    # 过滤测试集中的None标签,同步处理X和y
    test_filtered = [item for item in test if item[1] is not None]
    X_test = np.array([i[0] for i in test_filtered]).reshape(-1, 50, 50, 1)
    y_test = np.array([i[1] for i in test_filtered])
    
  2. 模型激活函数配置错误

    • 倒数第二层用了softmax激活,会导致后续层输入范围异常,应改为relu:
      model.add(Dense(1024))
      model.add(BatchNormalization())
      model.add(Activation('relu'))  # 替换原softmax
      model.add(Dropout(0.8))
      
    • 输出层用了relu激活,搭配categorical_crossentropy损失函数不合理,3分类任务应改为softmax:
      model.add(Dense(3, activation='softmax'))
      
  3. Dropout率过高
    当前Dropout设置为0.8,会过度抑制模型学习特征,建议调整为0.3-0.5区间,比如:

    model.add(Dropout(0.3))
    

内容的提问来源于stack exchange,提问作者Rambo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 12:27:13