问题描述
我们正在做分类预测。我们的数据集是不平衡的,并且包含比零标签多得多的零标签。 我们试图通过我们的自定义损失函数来解决这个问题,该函数返回真零或更大的真误差百分比。 例如,如果真零有 5% 的误差,真零有 20% 的误差,那么损失函数会返回更大的误差,即 20。
def custom_function_max_incorrect(y_true,y_pred):
y_true = backend.round(backend.clip(y_true,1))
y_pred = backend.round(backend.clip(y_pred,1))
all_zeroes_count = backend.maximum(backend.sum(1.0 - y_true),1) # must be more than 0
incorrect_zeroes_count = backend.sum((1.0 - y_true) * y_pred)
incorrect_zeroes_percents = 100.0 * incorrect_zeroes_count / all_zeroes_count
all_ones_count = backend.maximum(backend.sum(y_true),1) # must be more than 0
incorrect_ones_count = backend.sum(y_true * (1.0 - y_pred))
incorrect_ones_percents = 100.0 * incorrect_ones_count / all_ones_count
incorrect_percents_max = backend.maximum(incorrect_ones_percents,incorrect_zeroes_percents)
return incorrect_percents_max
我们使用的是 AutoKeras 1.0.15(目前是最新版本),唯一的问题是 AutoKeras 中的 StructuredDataClassifier
似乎忽略了我们的损失参数,而是始终使用默认参数。
这是我们代码的一部分:
clf = autokeras.StructuredDataClassifier(
overwrite=True,max_trials=200,loss = custom_function_max_incorrect,objective = kerastuner.Objective("custom_function_max_incorrect",direction="min")
)
clf.fit(train_file_path,"CurrentHasOrder",epochs=100)
有趣的是,StructuredDataRegressor
正确使用了我们提供的 loss
参数,但 StructuredDataClassifier
似乎忽略了它。
我们也尝试声明自定义对象,但 StructuredDataClassifier
仍在使用默认损失函数并忽略我们的自定义损失函数:
with tf.keras.utils.custom_object_scope({'custom_function_max_incorrect': custom_function_max_incorrect}):
clf = autokeras.StructuredDataClassifier(
overwrite=True,direction="min")
)
clf.fit(train_file_path,epochs=100)
请您帮我们看看如何在StructuredDataClassifier
中使用自定义损失函数?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)