Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 自定义keras真正度量是否应始终返回整数?_Tensorflow_Keras_Metrics_Training Data_Tf.keras - Fatal编程技术网

Tensorflow 自定义keras真正度量是否应始终返回整数?

Tensorflow 自定义keras真正度量是否应始终返回整数?,tensorflow,keras,metrics,training-data,tf.keras,Tensorflow,Keras,Metrics,Training Data,Tf.keras,我正在使用一个非标准数据集,其中我的y_true是(批次x5x1),而y_pred是(批次x5x1)。如果任何值y\u true[i]>0,则批次样本i为“真”。如果y\u pred[i]>=b其中b为介于0和1之间的阈值,则预测为“真” 我已经定义了这个自定义keras度量来计算一批中的真正数: def TP(阈值=0.0): def TP_u(Y_真,Y_pred): Y_真=tf.其中(Y_真>0.,tf.one(tf.shape(Y_真)),tf.zero(tf.shape(Y_真)))

我正在使用一个非标准数据集,其中我的
y_true
是(批次x5x1),而
y_pred
是(批次x5x1)。如果任何值
y\u true[i]>0,则批次样本
i
为“真”。如果
y\u pred[i]>=b
其中
b
为介于0和1之间的阈值,则预测为“真”

我已经定义了这个自定义keras度量来计算一批中的真正数:

def TP(阈值=0.0):
def TP_u(Y_真,Y_pred):
Y_真=tf.其中(Y_真>0.,tf.one(tf.shape(Y_真)),tf.zero(tf.shape(Y_真)))
Y_pred_true=tf.where(Y_pred>=阈值,tf.one(tf.shape(Y_pred)),tf.zero(tf.shape(Y_pred)))
Y_真=K.sum(Y_真,轴=1)
Y_pred_true=K.sum(Y_pred_true,轴=1)
Y_真=tf.其中(Y_真>0.,tf.one(tf.shape(Y_真)),tf.zero(tf.shape(Y_真)))
Y_pred_true=tf.其中(Y_pred_true>0.,tf.one(tf.shape(Y_pred_true)),tf.zero(tf.shape(Y_pred_true)))
Y=tf.math.add(Y_true,Y_pred_true)
tp=tf.where(Y==2,tf.one(tf.shape(Y)),tf.zero(tf.shape(Y)))
tp=K.总和(tp)
返回tp
返回TP_
在训练时,我有时会得到非整数值。这是因为keras对所有批次的值取平均值吗


对于真阴性、假阳性和假阴性,我有类似的自定义指标。在培训期间,这四个值的总和是否应该是一个整数?

由两部分组成的回答:是的,度量值是批次的平均值。您将看到内置度量的相同行为,例如
tensorflow.keras.metrics.TruePositive
,但在每个历元结束时,它将是一个整数

但是,您并没有持久化度量的状态,因此TensorFlow只获取返回度量的平均值。考虑子类<代码> tf.kas。度量。度量< /代码>如下:

class TP(tf.keras.metrics.Metric):
    
    def __init__(self, threshold=0.5, **kwargs):
        super().__init__(**kwargs)

        self.threshold = threshold
        self.true_positives = self.add_weight(name='true_positives', initializer='zeros',
                                              dtype=tf.int32)

    def update_state(self, y_true, y_pred, sample_weight=None):

        y_true = tf.where(y_true > self.threshold,
                          tf.ones(tf.shape(y_true)),
                          tf.zeros(tf.shape(y_true)))
        y_pred_true = tf.where(y_pred >= self.threshold,
                               tf.ones(tf.shape(y_pred)),
                               tf.zeros(tf.shape(y_pred)))

        y_true = K.sum(y_true, axis=1)
        y_pred_true = K.sum(y_pred_true, axis=1)

        y_true = tf.where(y_true > self.threshold, tf.ones(tf.shape(y_true)),
                          tf.zeros(tf.shape(y_true)))
        y_pred_true = tf.where(y_pred_true > self.threshold,
                               tf.ones(tf.shape(y_pred_true)),
                               tf.zeros(tf.shape(y_pred_true)))

        Y = tf.math.add(y_true, y_pred_true)
        tp = tf.where(Y == 2, tf.ones(tf.shape(Y), dtype=tf.int32),
                      tf.zeros(tf.shape(Y), dtype=tf.int32))
        tp = K.sum(tp)
        self.true_positives.assign_add(tp)

    def result(self):
        return self.true_positives

    def get_config(self):
        return {'threshold': self.threshold}

这非常有效,但不幸的是,在保存模型后,我无法重新加载它。我将我的原始度量保存为自定义对象,并尝试在这里执行相同的操作,但tensorflow不喜欢它。可能是因为它是一个类而不是一个函数?“tensorflow不喜欢它”非常模糊,您可能应该提出一个新问题,但我会尝试这样加载模型:
tf.keras.models.load(model_path,custom_objects={TP':TP)
。我还更新了答案,使用了一个
get\u config
方法,因为我们希望在恢复度量值后使用相同的阈值。这是非常模糊的。我没有足够的时间花在这个问题上。我正在处理许多其他自定义对象,因此我可能可以自己解决这个问题。我只是想确保您能够要使用此度量重新加载模型。您好,我想跟进一下,我能够使用此度量格式并重新加载预测值。我认为是
get\u config
方法造成了差异。谢谢!