torchnet的使用

原文链接:https://blog.csdn.net/a362682954/article/details/82926499

import torchnet

tnt.Meter

Meters provide a standardized way to measure a range of different measures, which makes it easy to measure a wide range of properties of your models.

有三个基础的方法:

  • add() which adds an observation to the meter.
  • value() which returns the value of the meter, taking into account all observations.
  • reset() which removes all previously added observations, resetting the meter.

tnt.AverageValueMeter(self)

The tnt.AverageValueMeter measures and returns the average value and the standard deviation of any collection of numbers that are added to it. It is useful, for instance, to measure the average loss over a collection of examples.

tnt.AUCMeter(self)

The tnt.AUCMeter measures the area under the receiver-operating characteristic (ROC) curve for binary classification problems. The area under the curve (AUC) can be interpreted as the probability that, given a randomly selected positive example and a randomly selected negative example, the positive example is assigned a higher score by the classification model than the negative example.

tnt.ConfusionMeter(self, k[, normalized])

The tnt.ConfusionMeter constructs a confusion matrix for a multi-class classification problems.

import torchnet.meter as meter

m = meter.AverageValueMeter()

for i in range(10):
    m.add(i)

print(m.value()[0])

m.reset()#重置数据

for i in range(10, 20):
    m.add(i)
    
print(m.value())
4.5
(14.5, 3.0276503540974917)
import torch
mtr = meter.ConfusionMeter(k=3)
output = torch.Tensor([[.01, 0.01, 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)
    
print(target)
print(output)
mtr.add(output, target)
tensor([0, 1, 2])
tensor([[1.0000e-02, 1.0000e-02, 1.0000e-01],
        [1.0000e+01, 1.1000e+01, 1.0000e+01],
        [2.0000e-01, 2.0000e-01, 3.0000e-01]])
mtr.value()
#生成一个onehat向量
array([[0, 0, 1],
       [0, 1, 0],
       [0, 0, 1]], dtype=int32)
meter.confusionmeter??
上一篇:Petrozavodsk Winter Training Camp 2018 Jagiellonian U Contest Problem A. XOR


下一篇:Transformer系列-----TNT详解