Tensorflow Precision、Recall、F1 - 多标签分类

2024-01-07

我正在尝试使用张量流实现多标签句子分类模型。大约有 1500 个标签。 该模型运行得很好,但我不确定它生成的指标。

这是生成指标的代码段:

    with tf.name_scope('loss'):
        losses = tf.nn.softmax_cross_entropy_with_logits(labels=self.input_y, logits=self.scores) #  only named arguments accepted
        self.loss = tf.reduce_mean(losses) + l2_reg_lambda * l2_loss

    with tf.name_scope('accuracy'):
        correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1))
        self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, "float"), name='accuracy')

    with tf.name_scope('num_correct'):
        correct = tf.equal(self.predictions, tf.argmax(self.input_y, 1))
        self.num_correct = tf.reduce_sum(tf.cast(correct, 'float'))

    with tf.name_scope('fp'):
        fp = tf.metrics.false_positives(labels=tf.argmax(self.input_y, 1), predictions=self.predictions)
        self.fp = tf.reduce_sum(tf.cast(fp, 'float'), name='fp')

    with tf.name_scope('fn'):
        fn = tf.metrics.false_negatives(labels=tf.argmax(self.input_y, 1), predictions=self.predictions)
        self.fn = tf.reduce_sum(tf.cast(fn, 'float'), name='fn')

    with tf.name_scope('recall'):
        self.recall = self.num_correct / (self.num_correct + self.fn)

    with tf.name_scope('precision'):
        self.precision = self.num_correct / (self.num_correct + self.fp)

    with tf.name_scope('F1'):
        self.F1 = (2 * self.precision * self.recall) / (self.precision + self.recall)

    with tf.name_scope('merged_summary'):
        tf.summary.scalar("loss", self.loss)
        tf.summary.scalar("accuracy", self.accuracy)
        tf.summary.scalar("recall", self.recall)
        tf.summary.scalar("precision", self.precision)
        tf.summary.scalar("f-measure", self.F1)
        self.merged_summary = tf.summary.merge_all()

然后,在训练部分,我为 Tensorboard 创建保护程序:

summary_writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph())

最后,训练保存指标如下:

for train_batch in train_batches:
            x_train_batch, y_train_batch = zip(*train_batch)
            train_step(x_train_batch, y_train_batch)
            current_step = tf.train.global_step(sess, global_step)

            # Evaluate the model with x_dev and y_dev
            if current_step % params['evaluate_every'] == 0:
                dev_batches = data_helper.batch_iter(list(zip(x_dev, y_dev)), params['batch_size'], 1)

                total_dev_correct = 0
                for dev_batch in dev_batches:
                    x_dev_batch, y_dev_batch = zip(*dev_batch)
                    acc, loss, num_dev_correct, predictions, recall, precision, f1, summary = dev_step(x_dev_batch, y_dev_batch)
                    total_dev_correct += num_dev_correct
                accuracy = float(total_dev_correct) / len(y_dev)
                logging.info('Accuracy on dev set: {}'.format(accuracy))
                # added loss
                logging.info('Loss on dev set: {}'.format(loss))
                # adding more measures
                logging.info('Recall on dev set: {}'.format(recall))
                logging.info('Precision on dev set: {}'.format(precision))
                logging.info('F1 on dev set: {}'.format(f1))
                summary_writer.add_summary(summary, current_step)

                if accuracy >= best_accuracy:
                    best_accuracy, best_loss, best_at_step, best_recall, best_precision, best_f1 = accuracy, loss, current_step, recall, precision, f1
                    path = saver.save(sess, checkpoint_prefix, global_step=current_step)
                    logging.critical('Saved model {} at step {}'.format(path, best_at_step))
                    logging.critical('Best accuracy {} at step {}'.format(best_accuracy, best_at_step))
                    logging.critical('Best loss {} at step {}'.format(best_loss, best_at_step))
                    logging.critical('Best recall {} at step {}'.format(best_recall, best_at_step))
                    logging.critical('Best precision {} at step {}'.format(best_precision, best_at_step))
                    logging.critical('Best F1 {} at step {}'.format(best_f1, best_at_step))
        logging.critical('Training is complete, testing the best model on x_test and y_test')

dev_step 和 train_step 如下所示:

def train_step(x_batch, y_batch):
            feed_dict = {
                cnn_rnn.input_x: x_batch,
                cnn_rnn.input_y: y_batch,
                cnn_rnn.dropout_keep_prob: params['dropout_keep_prob'],
                cnn_rnn.batch_size: len(x_batch),
                cnn_rnn.pad: np.zeros([len(x_batch), 1, params['embedding_dim'], 1]),
                cnn_rnn.real_len: real_len(x_batch),
            }
            _, step, loss, accuracy = sess.run([train_op, global_step, cnn_rnn.loss, cnn_rnn.accuracy], feed_dict)

        def dev_step(x_batch, y_batch):
            feed_dict = {
                cnn_rnn.input_x: x_batch,
                cnn_rnn.input_y: y_batch,
                cnn_rnn.dropout_keep_prob: 1.0,
                cnn_rnn.batch_size: len(x_batch),
                cnn_rnn.pad: np.zeros([len(x_batch), 1, params['embedding_dim'], 1]),
                cnn_rnn.real_len: real_len(x_batch),
            }
            step, loss, accuracy, num_correct, predictions, recall, precision, f1, summary = sess.run(
                [global_step, cnn_rnn.loss, cnn_rnn.accuracy, cnn_rnn.num_correct, cnn_rnn.predictions, cnn_rnn.recall, cnn_rnn.precision, cnn_rnn.F1, cnn_rnn.merged_summary], feed_dict)
            return accuracy, loss, num_correct, predictions, recall, precision, f1, summary

我的问题是,对于多标签分类问题生成的指标是否正确,或者我应该通过混淆矩阵来做到这一点? 如果我应该使用混淆矩阵,我应该添加:

tf.confusion_matrix(labels=, predictions=)

在代码的第一部分中我声明指标的地方?如果是,下一步我应该做什么才能获得精确度和召回率。

编辑:我已经添加了这个,但张量板中的图像只是黑屏。

batch_confusion = tf.confusion_matrix(labels=tf.argmax(self.input_y, 1), predictions=self.predictions, name='batch_confusion', num_classes=num_classes)
            confusion = tf.Variable(tf.zeros([num_classes, num_classes], dtype=tf.int32), name='confusion')
            confusion_image = tf.reshape(tf.cast(confusion, tf.float32), [1, num_classes, num_classes, 1])
            tf.summary.image('confusion', confusion_image)

感谢您的帮助,


多标签设置与单标签设置有很大不同,因为您必须定义您的含义Positive。是不是意味着all标签必须是True或者你算吗any Positive作为(部分)成功?

第一个案例 ->macroF1 得分 (axis=None in count_nonzero如你所愿all标签同意它是真阳性)

如果是第二种情况,那么您是否希望所有类别在衡量成功方面具有相同的权重?

是的->microF1 得分 (axis=1当您比较每个标签的结果时)

否 ->weightedF1分数,权重是各自的支持度class (idem for axis)

From my answer https://stackoverflow.com/a/50251763/3867406另一个问题:

f1s = [0, 0, 0]

y_true = tf.cast(y_true, tf.float64)
y_pred = tf.cast(y_pred, tf.float64)

for i, axis in enumerate([None, 0]):
    TP = tf.count_nonzero(y_pred * y_true, axis=axis)
    FP = tf.count_nonzero(y_pred * (y_true - 1), axis=axis)
    FN = tf.count_nonzero((y_pred - 1) * y_true, axis=axis)

    precision = TP / (TP + FP)
    recall = TP / (TP + FN)
    f1 = 2 * precision * recall / (precision + recall)

    f1s[i] = tf.reduce_mean(f1)

weights = tf.reduce_sum(y_true, axis=0)
weights /= tf.reduce_sum(weights)

f1s[2] = tf.reduce_sum(f1 * weights)

micro, macro, weighted = f1s

正确性

def tf_f1_score(y_true, y_pred):
    """Computes 3 different f1 scores, micro macro
    weighted.
    micro: f1 score accross the classes, as 1
    macro: mean of f1 scores per class
    weighted: weighted average of f1 scores per class,
            weighted from the support of each class


    Args:
        y_true (Tensor): labels, with shape (batch, num_classes)
        y_pred (Tensor): model's predictions, same shape as y_true

    Returns:
        tuple(Tensor): (micro, macro, weighted)
                    tuple of the computed f1 scores
    """

    f1s = [0, 0, 0]

    y_true = tf.cast(y_true, tf.float64)
    y_pred = tf.cast(y_pred, tf.float64)

    for i, axis in enumerate([None, 0]):
        TP = tf.count_nonzero(y_pred * y_true, axis=axis)
        FP = tf.count_nonzero(y_pred * (y_true - 1), axis=axis)
        FN = tf.count_nonzero((y_pred - 1) * y_true, axis=axis)

        precision = TP / (TP + FP)
        recall = TP / (TP + FN)
        f1 = 2 * precision * recall / (precision + recall)

        f1s[i] = tf.reduce_mean(f1)

    weights = tf.reduce_sum(y_true, axis=0)
    weights /= tf.reduce_sum(weights)

    f1s[2] = tf.reduce_sum(f1 * weights)

    micro, macro, weighted = f1s
    return micro, macro, weighted


def compare(nb, dims):
    labels = (np.random.randn(nb, dims) > 0.5).astype(int)
    predictions = (np.random.randn(nb, dims) > 0.5).astype(int)

    stime = time()
    mic = f1_score(labels, predictions, average='micro')
    mac = f1_score(labels, predictions, average='macro')
    wei = f1_score(labels, predictions, average='weighted')

    print('sklearn in {:.4f}:\n    micro: {:.8f}\n    macro: {:.8f}\n    weighted: {:.8f}'.format(
        time() - stime, mic, mac, wei
    ))

    gtime = time()
    tf.reset_default_graph()
    y_true = tf.Variable(labels)
    y_pred = tf.Variable(predictions)
    micro, macro, weighted = tf_f1_score(y_true, y_pred)
    with tf.Session() as sess:
        tf.global_variables_initializer().run(session=sess)
        stime = time()
        mic, mac, wei = sess.run([micro, macro, weighted])
        print('tensorflow in {:.4f} ({:.4f} with graph time):\n    micro: {:.8f}\n    macro: {:.8f}\n    weighted: {:.8f}'.format(
            time() - stime, time()-gtime,  mic, mac, wei
        ))

compare(10 ** 6, 10)

outputs:

>> rows: 10^6 dimensions: 10
sklearn in 2.3939:
    micro: 0.30890287
    macro: 0.30890275
    weighted: 0.30890279
tensorflow in 0.2465 (3.3246 with graph time):
    micro: 0.30890287
    macro: 0.30890275
    weighted: 0.30890279
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Tensorflow Precision、Recall、F1 - 多标签分类 的相关文章

  • Python的unpack中的逗号是什么意思?

    我们可以简单地使用 crc struct unpack gt i data 为什么人们这样写 crc struct unpack gt i data 逗号是什么意思 第一个变体返回一个单元素元组 In 13 crc struct unpac
  • 我应该如何在 python 中使用 lxml 处理 XLink 引用?

    我被要求编写一些读取 XML 配置文件的脚本 这些文件充分利用 XLink 来包含存储在多个文件中的 XML 例如
  • 对 Python DataFrame 进行子集化

    我正在从 R 过渡到 Python 我刚刚开始使用 Pandas 我有一个可以很好地子集化的 R 代码 k1 lt subset data Product p id Month lt mn Year yr select c Time Pro
  • Python 按文件夹模块导入

    我有一个目录结构 example py templates init py a py b py a py and b py只有一个类 名称与文件相同 因为它们是猎豹模板 纯粹出于风格原因 我希望能够在中导入和使用这些类example py像
  • 如何在 pygame 中聚焦光线或如何仅绘制窗口的某些圆形部分?

    对于这一点 如果您熟悉它 请想想 超级马里奥制造2 中嘘关卡中的黑暗模式 我试图在角色周围创建一个圆形聚光灯 这也将使圆圈范围内的任何内容都可见 例如部分站在地板上 敌人或场景中的任何其他物体 我的计划是首先绘制圆圈 聚光灯 然后绘制场景
  • Accel 无法在 gedit 3 插件中工作

    我试图为 Gedit 3 编写一个使用 GObject 自省的小插件 下面显示的代码的相关部分只是为了建立一个环境 然后我可以将函数放入按钮的回调中 但是 该按钮的加速器不起作用 这段代码有什么问题 我正在使用教程here http www
  • Python MySQL 模块

    我正在开发一个需要与 MySQL 数据库交互的 Web 应用程序 但我似乎找不到任何真正适合 Python 的模块 我特别寻找快速模块 能够处理数十万个连接 和查询 所有这些都在短时间内完成 而不会对速度产生重大影响 我想我的答案将是游戏领
  • Python变量赋值问题

    a b 0 1 while b lt 50 print b a b b a b 输出 1 2 4 8 16 32 wheras a b 0 1 while b lt 50 print b a b b a b 输出 正确的斐波那契数列 1 1
  • Cython:为什么 size_t 比 int 快?

    更改某些 Cython 变量的类型int输入size t可以显着减少某些功能的时间 30 但我不明白为什么 例如 cimport numpy as cnp import numpy as np def sum int cnp int64 t
  • 不重复的Python组合

    我有一个数字列表 我想从中进行组合 如果我有清单 t 2 2 2 2 4 c list itertools combinations t 4 结果是 2 2 2 2 2 2 2 4 2 2 2 4 2 2 2 4 2 2 2 4 但我想得到
  • import numpy 和 import numpy as np 之间的区别

    我明白 如果可能的话 应该使用 import numpy as np 这有助于避免由于命名空间引起的任何冲突 但我注意到虽然下面的命令有效 import numpy f2py as myf2py 以下不 import numpy as np
  • 具有条件的重复行 pandas dataframe python

    我的数据框有问题 我的 df 是 product power brand product 1 3 x 1500W brand A product 2 2x1000W 1x100W product 3 1x1500W 1x500W brand
  • 在Python中引用不带换行符的长字符串

    我正在尝试在 Python 中编写一个长字符串 该字符串显示为 OptParser 选项的帮助项 在我的源代码 py 文件中 我想放置换行符 以便我的代码不会花费新行 但是 我不希望这些换行符影响代码运行时该字符串的显示方式 例如 我想写
  • 在 Django/python 中,如何将内存缓存设置为无限时间?

    cache set key value 9999999 但这并不是无限的时间 def get memcache timeout self timeout Memcached deals with long gt 30 days timeou
  • Tornado websocket handler , self.close() 正在关闭连接而不触发 on_close() 方法

    我是 python stackoverflow tornado 的新手 所以请耐心等待 纠正我 我正在使用龙卷风开发实时应用程序 当我在 Websocket 处理程序类中调用 self close 时 on close 方法不会启动 这次我
  • Beautiful Soup 获取动态表数据

    我有以下代码 url https www basketball reference com leagues NBA 2017 standings html all expanded standings html urlopen url so
  • 使用 Pandas 和 Group By 绘制堆叠直方图

    我正在使用如下所示的数据集 Gender Height Width Male 23 4 4 4 Female 45 4 4 5 我想可视化高度和宽度的堆叠直方图 我希望每个图有两个堆叠的直方图 每个性别一个 这是文档中的堆叠直方图 如果存在
  • 在 python 中使用递归替代 len()

    作为 CS1301 问题的一部分 我正在尝试使用递归编写一个函数 该函数将执行与 len 完全相同的操作 但是 我有两个问题 我正在使用全局变量 但我在课程中还没有学到这一点 cs1301 自动评分器告诉我 我的函数返回 26 而不是 13
  • 将 pandas 数据框中的多列更改为日期时间

    我有一个 13 列和 55 000 行的数据框 我正在尝试将其中 5 行转换为日期时间 现在它们返回类型 对象 我需要转换这些数据以进行机器学习 我知道如果我这样做 data birth date pd to datetime data b
  • Pandas 替换特定列上的值

    我知道这两个类似的问题 熊猫替换值 https stackoverflow com questions 27117773 pandas replace values Pandas 替换数据框中的列值 https stackoverflow

随机推荐