简体   繁体   中英

Why does the last fully-connected/dense layer in a keras neural network expect to have 2 dim even if its input has more dimensions?

I'm trying neural networks with keras for the firs time, and am a bit confused with the dimensions it expects. I am sure that my X_train and y_train data are of the same dimension, and that the X_test and y_test data are also of the same dimension, but I am getting this error from keras:

Error when checking input: expected dense_38_input to have 2 dimensions, but got array with shape (1, 512, 512, 186, 1)

I've tried reshaping the training and validation data sets with (-1, 2) to match the 2 dimensions it is expecting, but that doesn't work and I'm not sure why.

Here is the training model I am trying

num_classes = 2

input_shape = (512, 512, 186, 1)

model = Sequential()
model.add(Conv3D(32, kernel_size=(5, 5, 5), strides=(1, 1, 1),
                 activation='relu',
                 input_shape=input_shape))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
model.add(Conv3D(64, (5, 5, 5), 
                 activation='relu'))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))
model.add(Flatten())
model.add(Dense(1000, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

I am hoping to simply get the neural network to run without error but I'm not sure how to manipulate my dataset's dimensions to get an appropriate dimension/shape for the training model.

I am sure that my X_train and y_train data are of the same dimension

If your y is dimensionally isometric to your X data, then your output shape would have to be the same as your input shape. I'm guessing you want the output shape you specified in your prediction (last) layer: an output predicting between 2 classes. In this case, your y shape should be of dimensions (num_samples, 2).

For clarity:

+---+------------------+------------------+-------------------------+
|   |  Dataframe shape | Data-point shape | Shape to assign network |
+---+------------------+------------------+-------------------------+
| X | (1000,244,244,3) |   (1,244,244,3)  |    input: (244,244,3)   |
+---+------------------+------------------+-------------------------+
| y |     (1000,2)     |       (1,2)      |       output: (2)       |
+---+------------------+------------------+-------------------------+

Instead of:

model.add(Flatten())

use this:

model.add(GlobalAveragePooling3D())

Basically model.add(Desnse()) , would expect 2 dims ie, (batch_size, channels), which is same as the output of GlobalAveragePooling3D() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM