【问题标题】:Adding class_weight to .fit_generator() breaks to_categorical()将 class_weight 添加到 .fit_generator() 会中断 to_categorical()
【发布时间】:2020-01-05 18:33:56
【问题描述】:

尝试使用 DataGenerator 类用一堆图像训练 CNN,模型正常工作得非常好。问题是训练数据集非常偏向于几个类,所以我想添加 class_weights。但是,每次我这样做时,我都会在将我的标记类转换为单热数组的代码部分中遇到索引错误。

如果 Keras 在 tensorflow 上运行。有问题的函数是 keras.utils.to_categorical()

这是分类函数:

for i, pdb_id in enumerate(list_enzymes_temp):
    mat = precomputed_distance_matrix(pdb_id, self.dim)

    X[i,] = mat.distance_matrix.reshape(*self.dim)

    y[i] = int(self.labels[pdb_id.upper()][1]) - 1

    return X, keras.utils.to_categorical(y, num_classes=self.n_classes)

这是我用来生成权重的函数

def get_class_weights(dictionary, training_enzymes, mode):
    'Gets class weights for Keras'
    # Initialization
    counter = [0 for i in range(6)]

    # Count classes
    for enzyme in training_enzymes:
        counter[int(dictionary[enzyme.upper()][1])-1] += 1
    majority = max(counter)

    # Make dictionary
    class_weights = {i: float(majority/count) for i, count in enumerate(counter)}

    # Value according to mode
    if mode == 'unbalanced':
        for key in class_weights:
            class_weights[key] = 1
    elif mode == 'balanced':
        pass
    elif mode == 'mean_1_balanced':
        for key in class_weights:
            class_weights[key] = (1+class_weights[key])/2

    return class_weights

还有我的 fit_generator 函数:

model.fit_generator(generator=training_generator,
                validation_data=validation_generator,
                epochs=max_epochs,
                max_queue_size=16,
                class_weight=class_weights,
                callbacks=[tensorboard])

在没有添加 class_weights 的情况下,不会出现 IndexError 消息并且模型可以完美运行:

File "C:\Users\Python\DMCNN\data_generator.py", line 73, in __getitem__
X, y = self.__data_generation(list_enzymes_temp)
File "C:\Users\Python\DMCNN\data_generator.py", line 59, in __data_generation
return X, keras.utils.to_categorical(y, num_classes=self.n_classes)
File "C:\Users\Python\Anaconda3\lib\site-packages\keras\utils\np_utils.py", line 34, in to_categorical
categorical[np.arange(n), y] = 1
IndexError: index 1065353216 is out of bounds for axis 1 with size 6

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    我在使用 keras.utils.to_categorical 时遇到了同样的错误。我得到的错误是“IndexError: index 1065353216 is out of bounds for axis 1 with size 2”因为我有 2 个类。

    我相信这是将 1.0 转换为 1.0f(32 位浮点数),因为 1065353216 是 32 位浮点值 1.0 的无符号 32 位整数表示(查看此处:Why is 1.0f in C code represented as 1065353216 in the generated assembly?)。在我的情况下,并非所有批次都具有相同的长度,最终会在 X 和 y 中出现一些未填充的空,这会导致问题。您可以提前检查您的 W(甚至 X 和 Y)中是否有一些未填充的元素。您还可以看到 keras.utils.to_categorical 具有默认值 dtype='float32'。您可以尝试指定 dtype,例如“return X, keras.utils.to_categorical(y, num_classes=self.n_classes, dtype='uint8')” 看看它是否有效。

    【讨论】:

      猜你喜欢
      • 2017-09-13
      • 2021-04-26
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 2016-12-24
      • 1970-01-01
      相关资源
      最近更新 更多