pytorch tnt meters使用

import math
import torch
import torchnet.meter as meter
import numpy as np
        m = meter.AverageValueMeter()
        for i in range(1, 10):
            m.add(i)
        mean, std = m.value()
mean,std
(5.0, 2.7386127875258306)
        m = meter.AverageValueMeter()
        for i in range(1, 11):
            m.add(i * i, n=i)
        mean, std = m.value()
mean,std
(7.0, 19.939569196777708)
        mtr = meter.ClassErrorMeter(topk=[1])
        output = torch.eye(3)
        if hasattr(torch, "arange"):
            target = torch.arange(0, 3)
        else:
            target = torch.range(0, 2)
        mtr.add(output, target)
        err = mtr.value()
target
 0
 1
 2
[torch.FloatTensor of size 3]
output
 1  0  0
 0  1  0
 0  0  1
[torch.FloatTensor of size 3x3]
err
[0.0]
        target[0] = 1
        target[1] = 0
        target[2] = 0
        mtr.add(output, target)
        err = mtr.value()
target
 1
 0
 0
[torch.FloatTensor of size 3]
output
 1  0  0
 0  1  0
 0  0  1
[torch.FloatTensor of size 3x3]
err
[50.0]
        mtr = meter.ConfusionMeter(k=3)

        output = torch.Tensor([[.8, 0.1, 0.1], [10, 11, 10], [0.2, 0.2, .3]])
        if hasattr(torch, "arange"):
            target = torch.arange(0, 3)
        else:
            target = torch.range(0, 2)
        mtr.add(output, target)
output
  0.8000   0.1000   0.1000
 10.0000  11.0000  10.0000
  0.2000   0.2000   0.3000
[torch.FloatTensor of size 3x3]
output.shape
torch.Size([3, 3])
target
 0
 1
 2
[torch.FloatTensor of size 3]
mtr.value()
array([[1, 0, 0],
       [0, 1, 0],
       [0, 0, 1]], dtype=int32)
        mtr = meter.ConfusionMeter(k=4, normalized=True)
        output = torch.Tensor([
            [.8, 0.1, 0.1, 0],
            [10, 11, 10, 0],
            [0.2, 0.2, .3, 0],
            [0, 0, 0, 1],
        ])

        target = torch.Tensor([0, 1, 2, 3])
        mtr.add(output, target)
        conf_mtrx = mtr.value()
conf_mtrx
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]], dtype=float32)
output
  0.8000   0.1000   0.1000   0.0000
 10.0000  11.0000  10.0000   0.0000
  0.2000   0.2000   0.3000   0.0000
  0.0000   0.0000   0.0000   1.0000
[torch.FloatTensor of size 4x4]
        target[0] = 1
        target[1] = 0
        target[2] = 0
        target[3]=2
        mtr.reset()
        mtr.add(output, target)
        conf_mtrx = mtr.value()
target
 1
 0
 0
 2
[torch.FloatTensor of size 4]
conf_mtrx
array([[0. , 0.5, 0.5, 0. ],
       [1. , 0. , 0. , 0. ],
       [0. , 0. , 0. , 1. ],
       [0. , 0. , 0. , 0. ]], dtype=float32)

output 0,1,2,3,可以看出,target为0的时候有俩个,其中有一个被判为1一个被判为2,所以都是0.5

conf_mtrx.ravel()
array([0. , 0.5, 0.5, 0. , 1. , 0. , 0. , 0. , 0. , 0. , 0. , 1. , 0. ,
       0. , 0. , 0. ], dtype=float32)

ravel是拆开的意思,2*2的混淆矩阵的时候,拆开分别为tn,fp,fn,tp,这时1代表正样本

        Confustion matrix of K rows and K columns, where rows corresponds
        to ground-truth targets and columns corresponds to predicted
        targets.
        mtr = meter.AUCMeter()

        test_size = 1000
        mtr.add(torch.rand(test_size), torch.zeros(test_size))
        mtr.add(torch.rand(test_size), torch.Tensor(test_size).fill_(1))

        val, tpr, fpr = mtr.value()
val,tpr,fpr
(0.505625,
 array([0.   , 0.001, 0.001, ..., 0.998, 0.999, 1.   ]),
 array([0.   , 0.   , 0.001, ..., 1.   , 1.   , 1.   ]))
        mtr.reset()
        mtr.add(torch.Tensor(test_size).fill_(0), torch.zeros(test_size))
        mtr.add(torch.Tensor(test_size).fill_(0.1), torch.zeros(test_size))
        mtr.add(torch.Tensor(test_size).fill_(0.2), torch.zeros(test_size))
        mtr.add(torch.Tensor(test_size).fill_(0.3), torch.zeros(test_size))
        mtr.add(torch.Tensor(test_size).fill_(0.4), torch.zeros(test_size))
        mtr.add(torch.Tensor(test_size).fill_(1),
                torch.Tensor(test_size).fill_(1))
        val, tpr, fpr = mtr.value()
val,tpr,fpr
(1.0,
 array([0.   , 0.001, 0.002, ..., 1.   , 1.   , 1.   ]),
 array([0.    , 0.    , 0.    , ..., 0.9996, 0.9998, 1.    ]))
        mtr.reset()
        mtr.add(torch.Tensor(test_size).fill_(0), torch.zeros(test_size))
        mtr.add(torch.Tensor(test_size).fill_(0.1), torch.zeros(test_size))
        mtr.add(torch.Tensor(test_size).fill_(0.2), torch.zeros(test_size))
        mtr.add(torch.Tensor(test_size).fill_(0.3), torch.zeros(test_size))
        mtr.add(torch.Tensor(test_size).fill_(0.4), torch.Tensor(test_size).fill_(1))
        mtr.add(torch.Tensor(test_size).fill_(1),
                torch.Tensor(test_size).fill_(1))
        val, tpr, fpr = mtr.value()
val,tpr,fpr
(1.0,
 array([0.e+00, 5.e-04, 1.e-03, ..., 1.e+00, 1.e+00, 1.e+00]),
 array([0.     , 0.     , 0.     , ..., 0.9995 , 0.99975, 1.     ]))
        mtr.reset()
        mtr.add(torch.Tensor(test_size).fill_(0), torch.Tensor(test_size).fill_(1))
        mtr.add(torch.Tensor(test_size).fill_(0.1), torch.zeros(test_size))
        mtr.add(torch.Tensor(test_size).fill_(0.2), torch.zeros(test_size))
        mtr.add(torch.Tensor(test_size).fill_(0.3), torch.zeros(test_size))
        mtr.add(torch.Tensor(test_size).fill_(0.4), torch.Tensor(test_size).fill_(1))
        mtr.add(torch.Tensor(test_size).fill_(1),
                torch.Tensor(test_size).fill_(1))
        val, tpr, fpr = mtr.value()
val,tpr,fpr
(0.6666666666666667,
 array([0.00000000e+00, 3.33333333e-04, 6.66666667e-04, ...,
        9.99333333e-01, 9.99666667e-01, 1.00000000e+00]),
 array([0., 0., 0., ..., 1., 1., 1.]))
        mtr = meter.APMeter()

        target = torch.Tensor([0, 1, 0, 1])
        output = torch.Tensor([0.1, 0.2, 0.3, 4])
        weight = torch.Tensor([0.5, 1.0, 2.0, 0.1])
        mtr.add(output, target, weight)

        ap = mtr.value()
ap
 0.6774
[torch.FloatTensor of size 1]
        mtr = meter.APMeter()

        target = torch.Tensor([0, 1, 0, 1])
        output = torch.Tensor([0.1, 5, 0.3, 4])
        weight = torch.Tensor([0.5, 1.0, 2.0, 0.1])
        mtr.add(output, target, weight)

        ap = mtr.value()
ap
 1
[torch.FloatTensor of size 1]
        mtr = meter.mAPMeter()
        target = torch.Tensor([0, 1, 0, 1])
        output = torch.Tensor([0.1, 0.2, 0.3, 4])
        weight = torch.Tensor([0.5, 1.0, 2.0, 0.1])
        mtr.add(output, target)

        ap = mtr.value()
ap
0.8333333730697632
        val = (1 * 1.0 / 1.0 + 0 * 1.0 / 2.0 +
               2.0 * 1.0 / 3.0 + 0 * 1.0 / 4.0) / 2.0

平均精度就是各个recall下的平均精度,但是算着感觉不大对啊?
http://nooverfit.com/wp/david9%E7%9A%84%E6%99%AE%E5%8F%8A%E8%B4%B4%EF%BC%9A%E6%9C%BA%E5%99%A8%E8%A7%86%E8%A7%89%E4%B8%AD%E7%9A%84%E5%B9%B3%E5%9D%87%E7%B2%BE%E5%BA%A6ap-%E5%B9%B3%E5%9D%87%E7%B2%BE%E5%BA%A6%E5%9D%87/

val
0.8333333333333333
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页