我收到此错误消息:ValueError:检查目标时出错:预期density_20具有形状(23,)但具有形状(1,)的数组。我是深度学习的初学者,找不到解决方案。
datagen = ImageDataGenerator(rescale=1./255)
batch_size = 20
def extract_features(directory, sample_count):
try:
features = np.zeros(shape=(sample_count, 7, 7, 1280))
labels = np.zeros(shape=(sample_count))
generator = datagen.flow_from_directory(
directory,
target_size=(224, 224),
batch_size=batch_size,
class_mode='binary')
i = 0
for inputs_batch, labels_batch in generator:
features_batch = conv_base.predict(inputs_batch)
features[i * batch_size : (i + 1) * batch_size] = features_batch
labels[i * batch_size : (i + 1) * batch_size] = labels_batch
i += 1
if i * batch_size >= sample_count:
break
except OSError as e:
pass
return features, labels
train_features, train_labels = extract_features(train_dir, 2000)
validation_features, validation_labels = extract_features(validation_dir, 1000)
test_features, test_labels = extract_features(test_dir, 1000)
train_features = np.reshape(train_features, (2000, 7 * 7 * 1280))
validation_features = np.reshape(validation_features, (1000, 7 * 7 * 1280))
test_features = np.reshape(test_features, (1000, 7 * 7 * 1280))
from keras import models
from keras import layers
from keras import optimizers
model = models.Sequential()
model.add(layers.Dense(256, activation='relu', input_dim=7 * 7 * 1280))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(23, activation='sigmoid'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['acc'])
history = model.fit(train_features, train_labels,
epochs=100,
batch_size=30,
validation_data=(validation_features, validation_labels))
我遇到多标签问题。我的神经网络应该能够区分23种不同的类别。我在文献中找到了解决该问题的一些方法。这就是我的代码产生的方式。 也许有人可以帮助我。
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-18-d4f0dfbf741a> in <module>()
15 epochs=100,
16 batch_size=30,
---> 17 validation_data=(validation_features, validation_labels))
/usr/local/lib/python3.5/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
950 sample_weight=sample_weight,
951 class_weight=class_weight,
--> 952 batch_size=batch_size)
953 # Prepare validation data.
954 do_validation = False
/usr/local/lib/python3.5/dist-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
787 feed_output_shapes,
788 check_batch_axis=False, # Don't enforce the batch size.
--> 789 exception_prefix='target')
790
791 # Generate sample-wise weight values given the `sample_weight` and
/usr/local/lib/python3.5/dist-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
136 ': expected ' + names[i] + ' to have shape ' +
137 str(shape) + ' but got array with shape ' +
--> 138 str(data_shape))
139 return data
140
ValueError: Error when checking target: expected dense_20 to have shape (23,) but got array with shape (1,)