Python 维数错误的Keras多类模型
Keras新手,尝试从中重新实现以下二值图像分类示例: 对我来说,它适用于二进制分类。 将其重建为3级分类,我得到以下尺寸不匹配错误:Python 维数错误的Keras多类模型,python,deep-learning,keras,Python,Deep Learning,Keras,Keras新手,尝试从中重新实现以下二值图像分类示例: 对我来说,它适用于二进制分类。 将其重建为3级分类,我得到以下尺寸不匹配错误: 60 epochs=50, 61 validation_data=validation_generator, ---> 62 validation_steps=250 // batch_size) ValueError: Error when checking target: expect
60 epochs=50,
61 validation_data=validation_generator,
---> 62 validation_steps=250 // batch_size)
ValueError: Error when checking target: expected activation_50 to have shape (None, 1) but got array with shape (16, 3)
这是我当前的实现:
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
K.set_image_dim_ordering('th')
batch_size = 16
# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1./255)
# this is a generator that will read pictures found in
# subfolers of 'data/train', and indefinitely generate
# batches of augmented image data
train_generator = train_datagen.flow_from_directory(
'F://train_data//', # this is the target directory
target_size=(150, 150), # all images will be resized to 150x150
batch_size=batch_size,
class_mode='categorical') # since we use binary_crossentropy loss, we need binary labels
# this is a similar generator, for validation data
validation_generator = test_datagen.flow_from_directory(
'F://validation_data//',
target_size=(150, 150),
batch_size=batch_size,
class_mode='categorical')
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3, 150, 150)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first"))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first"))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first"))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('softmax')) # instead of sigmoid
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['accuracy'])
# another loss: sparse_categorical_crossentropy
model.fit_generator(
train_generator,
steps_per_epoch=1800 // batch_size,
epochs=50,
validation_data=validation_generator,
validation_steps=250 // batch_size)
到目前为止,我已经将输出层的激活功能从sigmoid更改为softmax。将class_模式从二进制更改为分类。似乎找不到问题
此外,我知道有关StackOverflow的类似问题:
但没有一个解决方案对我有帮助 您需要将最终的
densed
层更改为model.add(densed(3))
。Softmax激活希望密集
层中的单元
与类数匹配
另外,如果您要使用
loss='sparse'
,请记住将class\u模式更改为'sparse'
。您当前的设置,class\u mode='classifical'
,应该与loss='classifical\u crossentropy'
一起使用。您需要将最终的密集层更改为模型。添加(密集(3))
。Softmax激活希望密集
层中的单元
与类数匹配
另外,如果您要使用loss='sparse'
,请记住将class\u模式更改为'sparse'
。您当前的设置,class\u mode='classifical'
,应该与loss='classifical\u crossentropy'
一起使用