keras 不确定如何解决此ValueError:数据基数不明确

pinkon5k  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(143)

我正试图用猫和狗的图像来训练CNN。代码如下:

model = Sequential()
model.add(Reshape((32, 32, 3)))

model.add(Conv2D(32, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Conv2D(32, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Conv2D(32, (3, 3), padding = 'same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Dropout(0.2))

model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dense(2))
model.add(Activation('softmax'))

opt = keras.optimizers.legacy.RMSprop(learning_rate=0.0001, decay=1e-6)

model.compile(loss='categorical_crossentropy',
          optimizer=opt,
          metrics=['accuracy'])

history = model.fit(cd_train_inputs.shape, cd_train_labels.shape, 
                validation_data=(cd_test_inputs.shape, cd_test_labels.shape), \
                epochs=70)
print(model.summary())

字符串
当我运行上面的代码时,我得到以下错误:

ValueError: Data cardinality is ambiguous:
  x sizes: 4
  y sizes: 1
Make sure all arrays contain the same number of samples.


我怎么才能让它工作?

i7uq4tfw

i7uq4tfw1#

当调用model.fit()时,你希望传递底层的X和y数据,而不是Tensor的形状:

history = model.fit(
    cd_train_inputs,
    cd_train_labels, 
    validation_data=(cd_test_inputs, cd_test_labels),
    epochs=70
)

字符串

相关问题