我正在运行一个 CNN 来检查图像但不分类。实际上,输出层是一个密集层,其参数为 1d 中标签中图像的大小。
如下代码所示,我使用的是 model.fit_generator() 而不是 model.fit 开始训练模型时出现以下错误:
ValueError: Error when checking target: expected dense_10 to have shape
(1,) but got array with shape (19316,)
为什么这是一个错误?我的密集输出是一个包含 19316 个元素的数组,为什么它期望它具有 (1,) 的形状?
这里还附上了模型的摘要:
conv2d_28 (Conv2D)(无、26、877、32)544
activation_37(激活)(无、26、877、32)0
max_pooling2d_28 (MaxPooling (None, 13, 438, 32) 0
conv2d_29 (Conv2D)(无、12、437、16)2064
activation_38(激活)(无、12、437、16)0
max_pooling2d_29 (MaxPooling (None, 6, 218, 16) 0
conv2d_30 (Conv2D) (None, 5, 217, 8) 520
activation_39(激活)(无、5、217、8)0
max_pooling2d_30 (MaxPooling (None, 2, 108, 8) 0
activation_40(激活)(无、2、108、8)0
flatten_10(扁平化)(无,1728)0
dropout_10(辍学)(无,1728)0
密集_10(密集)(无,19316)33397364
================================================== ================
总参数:33,400,492 可训练参数:33,400,492 不可训练参数:0
有什么建议么?
非常感谢!
def generator(data_arr, batch_size = 10):
num = len(data_arr)
if num % batch_size != 0 :
num = int(num/batch_size)
# Loop forever so the generator never terminates
while True:
for offset in range(0, num, batch_size):
batch_samples = (data_arr[offset:offset+batch_size])
samples = []
labels = []
for batch_sample in batch_samples:
samples.append(batch_sample[0])
labels.append((np.array(batch_sample[1].flatten)).transpose())
X_ = np.array(samples)
Y_ = np.array(labels)
X_ = X_[:, :, :, newaxis]
print(X_.shape)
print(Y_.shape)
yield (X_, Y_)
# compile and train the model using the generator function
train_generator = generator(training_data, batch_size = 10)
validation_generator = generator(val_data, batch_size = 10)
run_opts = tf.RunOptions(report_tensor_allocations_upon_oom = True)
model = Sequential()
model.add(Conv2D(32, (4, 4), strides=(2, 2), input_shape = (55, 1756,
1)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(16, (2, 2)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Conv2D(8, (2, 2)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
model.add(Activation('softmax'))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature
vectors
model.add(Dropout(0.3))
model.add(Dense(19316))
model.compile(loss = 'sparse_categorical_crossentropy',
optimizer = 'adam',
metrics = ['accuracy'],
options = run_opts)
model.summary()
batch_size = 20
nb_epoch = 6
model.fit_generator(train_generator,
steps_per_epoch = len(training_data) ,
epochs = nb_epoch,
validation_data = validation_generator,
validation_steps = len(val_data))
您当前正在使用sparse_categorical_crossentropy
损失,它需要整数标签并在内部进行单热编码,但您的标签已经是单热编码。
因此,对于这种情况,您应该恢复到categorical_crossentropy
损失。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句