【问题标题】:Trouble using class weight with categorical cross Entropy使用分类交叉熵的类权重时遇到问题
【发布时间】:2019-02-01 17:56:13
【问题描述】:

我正在使用 Keras 在许多 256x256 2D 图像中执行逐像素多类分类。由于我拥有的数据量和类 (32),我在训练期间使用图像生成器将数据输入网络。生成器的主要目的是对我的基本事实进行一次热编码,最终大小为 256x256x32(如果我尝试事先进行一次热编码,则会收到 OOM 错误)。

但是,当我在拟合模型时尝试使用 class_weight 设置时,我收到一条错误消息,指出它不支持 3+ 维目标。我已经展平了我的图像,所以输出是大小(批量大小,65536、32)。我不明白我应该如何将它们设为 2D。我不能使用 sample_weight 设置,因为 model.fit_generator 没有那个设置。

这是我的数据生成器的代码。

def generator(features, labels, batch_size):
# Create empty arrays to contain batch of features and labels#
batch_features = np.zeros((batch_size, 256, 256, 1))
batch_labels = np.zeros((batch_size,256*256,32))
while True:
    for i in range(batch_size):
        # choose random index in features
        #index= random.choice(len(features),1)
        index=i
        batch_features[i] = features[index]
        batch_labels[i] = keras.utils.to_categorical(labels[index],num_classes=32)
    yield batch_features, batch_labels

【问题讨论】:

    标签: python class tensorflow keras


    【解决方案1】:

    您可以根据documentation 在您的生成器(自定义)中使用样本权重

    generator:生成器或 Sequence (keras.utils.Sequence) 对象的实例,以避免在使用多处理时出现重复数据。生成器的输出必须是

    a tuple (inputs, targets)
    a tuple (inputs, targets, sample_weights).
    

    【讨论】:

      猜你喜欢
      • 2021-03-19
      • 2021-03-19
      • 2020-01-03
      • 2018-12-23
      • 2019-07-13
      • 2021-07-17
      • 2021-02-15
      • 2017-11-11
      • 2019-06-19
      相关资源
      最近更新 更多