【问题标题】:Kernel dies when I apply a class weights on a CNN with Tensorflow for Imbalanced Data当我使用 Tensorflow 对不平衡数据的 CNN 应用类权重时,内核死亡
【发布时间】:2021-03-04 10:31:12
【问题描述】:

我目前正在努力解决使用类权重字典执行 CNN 训练算法时出现的问题。我关注this tutorial。代码没有问题,但是,当我开始训练网络时,突然出现以下铭文:

内核死机,正在重启

有人遇到过我同样的问题吗? 会不会是损失函数的问题?

loss_fun = 'categorical_crossentropy'
Optimizer = 'Adam with lr=0.0001'

model = create_model_fun(choice, H, W, input_size)
model.compile(Adam(lr=.0001), loss=loss_fun, metrics=['accuracy'])

# Class Weights
output_shape = len(PD_Dict)
total = sum(N_PD_Samples)
weight_for_i = [(1 / N_PD_Samples[ii])*(total)/output_shape for ii in range(output_shape)]
class_weight = {}
for ii in range(output_shape):
    class_weight[ii] = weight_for_i[ii]
    
history = model.fit(train_map, np.array(train_lab_Matr), validation_data=(valid_map, np.array(
    valid_lab_Matr)), epochs=N_epochs, shuffle=True, batch_size=Dim_Batch, callbacks=[es, mc], class_weight=class_weight)

【问题讨论】:

  • 我这里也有同样的问题。我读到只有在您使用 Mac M1 时才会出现此问题,这也是我的情况。您找到解决此问题的方法了吗?

标签: python tensorflow conv-neural-network


【解决方案1】:

要找到类权重,首先遍历所有类并找到样本数量最多的类。让我们将该值称为 most_samples。然后遍历类并确定第 i 个类中的样本数,称为 samples[i]。每个类的权重是 most_samples/samples[i]。一直使用它,没有任何问题。例如假设您有 3 个类,如下所示

class 0  samples = 500
class 1  samples = 1000
class 2  samples = 2000

大多数样本的值为 2000,因此权重为

class 0 weight=2000/500=4
class 1 weight=2000/1000=2
class 2 weight=2000/2000=1
so the class weight dictionary looks like
(0:4, 1:2, 2:1)

【讨论】:

    猜你喜欢
    • 2021-06-19
    • 2021-01-27
    • 2022-10-24
    • 2020-04-06
    • 2020-02-28
    • 2019-01-22
    • 1970-01-01
    • 2022-11-27
    • 2019-12-08
    相关资源
    最近更新 更多