我正在训练一个图像分类器来区分猫和狗,以及Kaggle套装。
这是我的相关代码:
FINAL_ACTIVATION = "softmax"
OPTIMIZER = keras.optimizers.Adamax()
STRIDES = (2, 2)
DROPOUT = 0.5
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape = INPUT_SHAPE))
model.add(keras.layers.ELU())
model.add(MaxPooling2D(pool_size = (2, 2), strides = STRIDES))
model.add(Conv2D(64, (3, 3)))
model.add(keras.layers.ELU())
model.add(MaxPooling2D(pool_size = (2, 2), strides = STRIDES))
model.add(Conv2D(128, (3, 3)))
model.add(keras.layers.ELU())
model.add(MaxPooling2D(pool_size = (2, 2), strides = STRIDES))
model.add(Conv2D(128, (3, 3)))
model.add(keras.layers.ELU())
model.add(MaxPooling2D(pool_size = (2, 2), strides = STRIDES))
model.add(Flatten())
model.add(Dense(100))
model.add(keras.layers.ELU())
model.add(Dropout(DROPOUT))
model.add(Dense(50))
model.add(keras.layers.ELU())
model.add(Dropout(DROPOUT))
model.add(Dense(2))
model.add(Activation(FINAL_ACTIVATION))
model.compile(
loss="categorical_crossentropy",
optimizer = OPTIMIZER,
metrics = ["accuracy"]
)
train_datagen = ImageDataGenerator(
rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True
)
train_generator = train_datagen.flow(
x_train,
y_train,
batch_size = BATCH_SIZE
)
model.summary()
model.fit_generator(
train_generator,
steps_per_epoch = x_train.shape[0] // BATCH_SIZE,
epochs = EPOCHS
)
由于培训文件数量众多,因此没有测试集。但是,当我尝试训练这个网络时,我得到错误" ValueError:检查目标时出错:期望activation_1具有形状(2,)但是得到了具有形状(1,)的数组。"我在这做错了什么?
卷积神经网络对我来说仍然是一个黑魔法,所以我可能会在这里犯一些初学者的错误。我想我可能是。
这是我的模特:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 98, 98, 32) 896
_________________________________________________________________
elu_1 (ELU) (None, 98, 98, 32) 0
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 49, 49, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 47, 47, 64) 18496
_________________________________________________________________
elu_2 (ELU) (None, 47, 47, 64) 0
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 23, 23, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 21, 21, 128) 73856
_________________________________________________________________
elu_3 (ELU) (None, 21, 21, 128) 0
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 10, 10, 128) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 8, 8, 128) 147584
_________________________________________________________________
elu_4 (ELU) (None, 8, 8, 128) 0
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 4, 4, 128) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 2048) 0
_________________________________________________________________
dense_1 (Dense) (None, 100) 204900
_________________________________________________________________
elu_5 (ELU) (None, 100) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 100) 0
_________________________________________________________________
dense_2 (Dense) (None, 50) 5050
_________________________________________________________________
elu_6 (ELU) (None, 50) 0
_________________________________________________________________
dropout_2 (Dropout) (None, 50) 0
_________________________________________________________________
dense_3 (Dense) (None, 2) 102
_________________________________________________________________
activation_1 (Activation) (None, 2) 0
=================================================================
Total params: 450,884
Trainable params: 450,884
Non-trainable params: 0
_________________________________________________________________
答案 0 :(得分:1)
由于培训文件数量众多,因此没有测试集。
虽然这不是你的问题,但我觉得应该解决这个问题:我知道有些团体采用了一种非常不同的方法而且没有保留有效/测试集,但这听起来非常不正统且不确定。就个人而言,我很少看到没有测试或某种列车后验证的文章。
测试集非常有用,因为它们是您的模型适用于看不见的样本的经验证据。使用它们+统计显着性/置信度测试,您可以用统计基础断言您的模型不仅是一个非常复杂的if-else机器。
最重要的是,如果你是该地区的新手,你就不应该遵守这样的风险策略。
关于您的问题:' y_train.shape
?我相信你正在流动稀疏标签([1., 0., 1., 2., 3., ..., 4.]
)而不是单热编码标签:
[[0. 1.]
[1. 0.]
[0. 1.]
...
[1. 0.]]
argmax(x, axis=1)
为您提供真正的类索引。
如何解决:
将loss="categorical_crossentropy"
替换为sparse_categorical_crossentropy
。这样可以提高内存效率,但要求每个样本都与单个标签相关联。
OR
单一热门编码y_train
,然后再将其传递给ImageDataGenerator#flow
:
from keras.utils import to_categorical
y_train = to_categorical(y_train)
train_datagen = ImageDataGenerator(...)
train_generator = train_datagen.flow(x_train, y_train, ...)
...
这消耗更多内存,但允许单个样本与多个标签关联:
[[1. 1. 0.]
[0. 0. 1.]
...
[1. 0. 1.]]