用于在 keras 中调用的自定义宏

编程入门 行业动态 更新时间:2024-10-24 04:42:14
本文介绍了用于在 keras 中调用的自定义宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试为 recall = (recall of class1 + Recall of class2)/2 创建一个自定义宏.我想出了以下代码,但我不确定如何计算第 0 类的真阳性.

I am trying to create a custom macro for recall = (recall of class1 + recall of class2)/2. I came up with the following code but I am not sure how to calculate the true positive of class 0.

def unweightedRecall(): def recall(y_true, y_pred): # recall of class 1 true_positives1 = K.sum(K.round(K.clip(y_pred * y_true, 0, 1))) possible_positives1 = K.sum(K.round(K.clip(y_true, 0, 1))) recall1 = true_positives1 / (possible_positives1 + K.epsilon()) # --- get true positive of class 0 in true_positives0 here --- # Also, is there a cleaner way to get possible_positives0 possible_positives0 = K.int_shape(y_true)[0] - possible_positives1 recall0 = true_positives0 / (possible_positives0 + K.epsilon()) return (recall0 + recall1)/2 return recall

看来我将不得不使用 Keras.backend.equal(x, y),但是我如何创建一个形状为 K.int_shape(y_true)[0] 和所有值,比如 x?

It seems I will have to use Keras.backend.equal(x, y), but how do i create a tensor with shape K.int_shape(y_true)[0] and all values, say x?

编辑 1

根据 Marcin 的评论,我想创建一个基于 keras 回调的自定义指标.虽然 在 Keras 中浏览问题,但我遇到了以下 f1 指标代码:

Based on Marcin's comments, I wanted to create a custom metric based on callback in keras. While browsing issues in Keras, I came across the following code for f1 metric:

class Metrics(keras.callbacks.Callback): def on_epoch_end(self, batch, logs={}): predict = np.asarray(self.model.predict(self.validation_data[0])) targ = self.validation_data[1] self.f1s=f1(targ, predict) return metrics = Metrics() model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=[X_test,y_test], verbose=1, callbacks=[metrics])

但是回调如何返回准确性?我想实现未加权召回=(召回class1 +召回class2)/2.我可以想到以下代码,但希望您能帮我完成它

But how is the callback returning the accuracy? I wanted to implement unweighted recall = (recall class1 + recall class2)/2. I can think of the following code but would appreciate any help to complete it

from sklearn.metrics import recall_score class Metrics(keras.callbacks.Callback): def on_epoch_end(self, batch, logs={}): predict = np.asarray(self.model.predict(self.validation_data[0])) targ = self.validation_data[1] # --- what to store the result in?? --- self.XXXX=recall_score(targ, predict, average='macro') # we really dont need to return anything ?? return metrics = Metrics() model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, validation_data=[X_test,y_test], verbose=1, callbacks=[metrics])

编辑 2:模型:

Edit 2: model:

def createModelHelper(numNeurons=40, optimizer='adam'): inputLayer = Input(shape=(data.shape[1],)) denseLayer1 = Dense(numNeurons)(inputLayer) outputLayer = Dense(1, activation='sigmoid')(denseLayer1) model = Model(input=inputLayer, output=outputLayer) modelpile(loss=unweightedRecall, optimizer=optimizer) return model

推荐答案

keras 版本(有平均问题).

keras version (with the mean problem).

你的两个类实际上只有一维输出(0 或 1)吗?

Are your two classes actually only one dimension output (0 or 1)?

如果是这样:

def recall(y_true, y_pred): # recall of class 1 #do not use "round" here if you're going to use this as a loss function true_positives = K.sum(K.round(y_pred) * y_true) possible_positives = K.sum(y_true) return true_positives / (possible_positives + K.epsilon()) def unweightedRecall(y_true, y_pred): return (recall(y_true,y_pred) + recall(1-y_true,1-y_pred))/2.

现在,如果您的两个类实际上是一个 2 元素输出:

Now, if your two classes are actually a 2-element output:

def unweightedRecall(y_true, y_pred): return (recall(y_true[:,0],y_pred[:,0]) + recall(y_true[:,1],y_pred[:,1]))/2.

回调版本:

对于回调,您可以使用 LambdaCallback,并手动打印或存储结果:

For the callback, you can use a LambdaCallback, and you manually print or store the results:

myCallBack = LambdaCallback(on_epoch_end=unweightedRecall) stored_metrics = [] def unweightedRecall(epoch,logs): predict = model.predict(self.validation_data[0]) targ = self.validation_data[1] result = (recall(targ,predict) + recall(1-targ,1-predict))/2. print("recall for epoch " + str(epoch) + ": " + str(result)) stored_metrics.append(result)

其中 recall 是一个使用 np 而不是 K 的函数.和 epsilon = np.finfo(float).eps 或 epsilon = np.finfo(np.float32).eps)

Where recall is a function using np instead of K. And epsilon = np.finfo(float).eps or epsilon = np.finfo(np.float32).eps)

更多推荐

用于在 keras 中调用的自定义宏

本文发布于:2023-11-23 18:32:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622464.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   keras

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!