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

时间:2019-09-13 17:50:00

标签: python tensorflow keras

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

ValueError:检查目标时出错:预期density_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    
=================================================================

1 个答案:

答案 0 :(得分:0)

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)