【问题标题】:ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (68, 50, 50, 50, 1)ValueError:检查目标时出错:预期dense_1有2维,但得到的数组形状为(68、50、50、50、1)
【发布时间】:2019-09-13 17:50:00
【问题描述】:

我正在尝试构建一个可以在 3D 体素网格上工作的卷积网络。我尝试添加一个全连接层,但出现错误:

ValueError: 检查目标时出错:预期 dense_1 有 2 个维度,但得到了形状为 (68, 50, 50, 50, 1) 的数组

当我首先有一个展平层时,怎么会发生这种情况?那时我对密集层的输入不应该是平坦的吗?

x, y = load_data(directory)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=42)
model = Sequential()
model.add(Convolution3D(1, kernel_size=(3, 3, 3), activation='relu',
                        border_mode='same', name='conv1',
                        input_shape=(50, 50, 50, 1)))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))
model.add(Flatten())
model.add(Dense(32))
model.compile(
    loss='mean_squared_error',
    optimizer='adam',
    metrics=['accuracy']
    )
model.fit(
    x_train,
    y_train,
    epochs=10,
    batch_size=32,
    )
model.evaluate(x_test, y_test)
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1 (Conv3D)               (None, 50, 50, 50, 1)     28        
_________________________________________________________________
max_pooling3d_1 (MaxPooling3 (None, 25, 25, 25, 1)     0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 15625)             0         
_________________________________________________________________
dense_1 (Dense)              (None, 32)                500032    
=================================================================

【问题讨论】:

  • 你的输出数据是什么形状的?
  • 你是对的 - flatten 应该使它成为 1D。我们可以看到更多你的代码吗?例如从这里到您尝试编译的所有内容。仅供参考,在您的第一层中拥有超过 15,000 个完全连接的神经元太多了 - 它会添加 25 * 25 * 25* 15625 个参数,仅在该层中就有 2.34 亿个。
  • 感谢大家的帮助!我用更多细节更新了帖子并减小了密集层的大小
  • 你的 Y 是什么形状的?
  • (x_train, y_train), (x_test, y_test) = train_test_split ....

标签: python tensorflow keras


【解决方案1】:

train_test_split 方法将数组拆分为训练集和测试集。如果方法的输入是数组列表,则方法返回训练和测试元组。

train_set, test_set = train_test_split(x, y, test_size=0.25, random_state=42)
x_train, y_train = train_set
x_test, y_test = test_set

或者由于python支持对元组进行左侧赋值,

(x_train, y_train), (x_test, y_test) = train_test_split(x, y, test_size=0.25, random_state=42)

【讨论】:

    猜你喜欢
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 2020-04-15
    • 2018-08-03
    相关资源
    最近更新 更多