gpt4 book ai didi

python - 检查目标 : sparse_categorical_crossentropy output shape 时出错

转载 作者:太空宇宙 更新时间:2023-11-04 05:03:30 25 4
gpt4 key购买 nike

我正在尝试使用迁移学习在一组新颖的图像上训练 InceptionV3。我遇到了这个问题——这显然与输入和输出维度不匹配有关(我认为),但我似乎无法确定问题)。之前关于 SO 的所有相关帖子都与 VGG16(我已经开始工作)相关。这是我的代码:

 from keras.applications.inception_v3 import InceptionV3
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras.callbacks import ModelCheckpoint, TensorBoard, CSVLogger, Callback
from keras.optimizers import SGD
from keras.preprocessing.image import ImageDataGenerator

base_model = InceptionV3(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(3, activation='softmax')(x)
model = Model(inputs=base_model.input, output=predictions)

for layer in base_model.layers:
layer.trainable = False

model.compile(optimizer=SGD(lr=0.001, momentum=0.9), loss='sparse_categorical_crossentropy')

train_dir = 'hrct_data/ExtractedHRCTs/Train'
validation_dir = 'hrct_data/ExtractedHRCTs/Validation'
nb_train_samples = 21903
nb_validation_samples = 6000
epochs = 30
batch_size = 256

train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)

validation_datagen = ImageDataGenerator(
rescale=1./255)

train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(512, 512),
batch_size=batch_size,
class_mode="categorical")

validation_generator = validation_datagen.flow_from_directory(
validation_dir,
target_size=(512, 512),
batch_size=batch_size,
class_mode="categorical")


model.fit_generator(
train_generator,
steps_per_epoch=21903 // batch_size,
epochs=30,
validation_data=validation_generator,
validation_steps=6000 // batch_size)

model.save_weights('hrct_inception.h5')

这里是错误:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-89-f79a107413cd> in <module>()
4 epochs=30,
5 validation_data=validation_generator,
6 validation_steps=6000 // batch_size)
7 model.save_weights('hrct_inception.h5')

/Users/simonalice/anaconda/lib/python3.5/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
86 warnings.warn('Update your `' + object_name +
87 '` call to the Keras 2 API: ' + signature, stacklevel=2)
88 return func(*args, **kwargs)
89 wrapper._legacy_support_signature = inspect.getargspec(func)
90 return wrapper

/Users/simonalice/anaconda/lib/python3.5/site-packages/keras/engine/training.py in fit_generator(self, generator, steps_per_epoch, epochs, verbose, callbacks, validation_data, validation_steps, class_weight, max_q_size, workers, pickle_safe, initial_epoch)
1888 outs = self.train_on_batch(x, y,
1889
sample_weight=sample_weight,
1890 class_weight=class_weight)
1891
1892 if not isinstance(outs, list):

/Users/simonalice/anaconda/lib/python3.5/site-packages/keras/engine/training.py in train_on_batch(self, x, y, sample_weight, class_weight)
1625 sample_weight=sample_weight,
1626 class_weight=class_weight,
1627 check_batch_axis=True)
1628 if self.uses_learning_phase and not
isinstance(K.learning_phase(), int):
1629 ins = x + y + sample_weights + [1.]

/Users/simonalice/anaconda/lib/python3.5/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size)
1307 output_shapes,
1308 check_batch_axis=False,
1309 exception_prefix='target')
1310 sample_weights = _standardize_sample_weights(sample_weight,
1311 self._feed_output_names)

/Users/simonalice/anaconda/lib/python3.5/site-packages/keras/engine/training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
137 ' to have shape ' + str(shapes[i]) +
138 ' but got array with shape ' +
139 str(array.shape))
140 return arrays
141

ValueError: Error when checking target: expected dense_12 to have shape (None, 1) but got array with shape (256, 3)

任何帮助——即使是让我朝着正确的方向前进,都会有所帮助。

最佳答案

我认为错误来自于您使用 sparse_categorical_crossentropy

这种损失是将您在训练期间提供的目标(“y”)自动编码为单热编码目标。因此它需要一个形状为 (256,1) 的目标,您只需要在其中提供索引。

您使用数据生成器提供的是已经编码的类。所以你将 (256,3) 作为目标......因此错误:

ValueError: Error when checking target: expected dense_12 to have shape (None, 1) but got array with shape (256, 3)

要修复它,请尝试使用“categorical_crossentropy”作为损失函数。这个期望生成器提供的单热编码向量。

我希望这对您有所帮助:-)

关于python - 检查目标 : sparse_categorical_crossentropy output shape 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45094625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com