ValueError:检查目标时出错:预期density_1具有2维,但数组的形状为(68,50,50,50,1) [英] ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (68, 50, 50, 50, 1)
问题描述
我正在尝试构建一个可在3D体素网格上工作的卷积网络.我尝试添加一个完全连接的层,但出现错误:
I'm trying to build a convolutional network that will work on a 3D voxel grid. I try to add a fully connected layer but get an error:
ValueError:检查目标时出错:预期density_1具有2维,但数组的形状为(68,50,50,50,1)
ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (68, 50, 50, 50, 1)
当我首先有一个平坦的层时,这怎么可能发生?那时候我对密集层的输入不应该平坦吗?
How can this be happening when I have a flatten layer first? Shouldn't my input to the dense layer at that point be, well, flat?
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
=================================================================
推荐答案
train_test_split
方法将数组拆分为train和测试集.如果该方法的输入是数组列表,则该方法将返回训练和测试元组.
train_test_split
method split arrays into train and test set. If input to the method are list of arrays, the methods returns train and test tuples.
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支持向元组的左侧分配,所以
or since python support left side assignment to tuples,
(x_train, y_train), (x_test, y_test) = train_test_split(x, y, test_size=0.25, random_state=42)
这篇关于ValueError:检查目标时出错:预期density_1具有2维,但数组的形状为(68,50,50,50,1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!