gpt4 book ai didi

python - 是否在 Imagedatagenerator 中将 class_mode 从二进制更改为分类模式感到困惑

转载 作者:行者123 更新时间:2023-12-02 02:50:25 25 4
gpt4 key购买 nike

当我在 CNN 模型中从 2 个类别转变为 4 个类别时。我有错误。很多人帮助我解决了这些问题,比如我需要将损失更改为 sparse_categorical_crossentropy 和最后一层激活函数。

model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])

history = model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples//batch_size,
epochs=epochs,
validation_data = validation_generator,
validation_steps = validation_generator.samples // batch_size,
)

我很困惑,因为我必须将损失函数从 binary_crossentropy 更改为 categorical_crossenpropy。我想知道是否应该将 4 个类的 class_mode 从二进制更改为分类模式,还是应该将其保留为二进制。

train_datagen=ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip = True,
#vertical_flip = True,
validation_split=0.2,
brightness_range=[0.5, 1.5]

)

#test_datagen = ImageDataGenerator(
# rescale=1./255,
#
#)

train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width,img_height),
batch_size=batch_size,
shuffle=True,
class_mode='binary',
subset='training'
)

validation_generator = train_datagen.flow_from_directory(
train_data_dir, # same directory as training data
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary',
subset='validation'
#validation_data_dir,
#target_size=(img_width,img_height),
#batch_size=batch_size,
#class_mode='binary'
)

最佳答案

我想阅读documentation将是一个很好的起点。它会回答你所有的问题,然后是一些问题。

class_mode: One of "categorical", "binary", "sparse", "input", or None. Default: "categorical". Determines the type of label arrays that are returned: - "categorical" will be 2D one-hot encoded labels, - "binary" will be 1D binary labels, "sparse" will be 1D integer labels, - "input" will be images identical to input images (mainly used to work with autoencoders). - If None, no labels are returned (the generator will only yield batches of image data, which is useful to use with model.predict_generator()). Please note that in case of class_mode None, the data still needs to reside in a subdirectory of directory for it to work correctly.

因此,如果您为class_mode选择categorical,则应使用categorical_crossentropy作为损失函数,如果您选择sparse_categorical_crossentropy,则应使用sparse_categorical_crossentropy作为损失函数选择稀疏

关于python - 是否在 Imagedatagenerator 中将 class_mode 从二进制更改为分类模式感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62090714/

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