0

I'm trying to use maxpooling as a first layer using keras and I have a problem with the input and output dimensions.

print(x_train.shape)
print(y_train.shape)
(15662, 6)
(15662,)

x_train = np.reshape(x_train, (-1,15662, 6)) 
y_train = label_array.reshape(1, -1)

model = Sequential()
model.add(MaxPooling1D(pool_size = 2 , strides=1, input_shape = (15662,6)))
model.add(Dense(5, activation='relu'))
model.add(Flatten())
model.add(Dense(1, activation='softmax'))

model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=
['accuracy'])
model.fit(x_train, y_train,  batch_size= 32, epochs=1)

After running the model, I get the following error:

ValueError: Error when checking target: expected dense_622 (last layer) to have shape (1,) but got array with shape (15662,)

I'm doing classification and my target is binary (0,1) Thank you

4

1 回答 1

1

你的目标应该有 shape(batch_size, 1)但你正在传递一个 shape 数组(1, 15662)。看起来 15662 应该是批量大小,在这种情况下x_train应该有 shape(15662, 6)并且y_train应该有 shape (15662, 1)。然而,在这种情况下,将 MaxPooling1D 层作为模型的第一层没有任何意义,因为最大池化需要 3D 输入(即 shape (batch_size, time_steps, features))。您可能希望省略最大池化层(和 Flatten 层)。以下代码应该可以工作:

# x_train: (15662, 6)
# y_train: (15662,)

model = Sequential()
model.add(Dense(5, activation='relu', input_shape=(6,))) # Note: don't specify the batch size in input_shape
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=
['accuracy'])
model.fit(x_train, y_train,  batch_size= 32, epochs=1)

但这当然取决于您拥有什么样的数据。

于 2019-03-06T18:50:48.587 回答