李沐老师-accuracy()

记录自用:

上代码:

y=torch.tensor([0,2])   #创建一个长度为2的向量,是一个整数型,第一个元素是0,第二个元素是2.这里表示的是两个真实的标号,
y_hat=torch.tensor([[0.1,0.3,0.6],[0.3,0.2,0.5]]) #y_hat就是预测值,假设有3类,对两个样本做预测,就是一个2*3的矩阵。
                    #第0样本预测值,  第1样本预测值。
print("我对这个下标表示方法真是无语了")
print("这里我还能接受,第0行,第一个:")
print(f"y_hat[0,1]:{y_hat[0,1]}")
print("我对这个下标表示方法真是无语了")
print("第0行,第0个,第1行,第2个,我真的不理解:")
print(f"y_hat[[0,1],y]:{y_hat[[0,1],y]}")
print(f"y_hat[[0,1],[0,2]]:{y_hat[[0,1],[0,2]]}")

输出:

我对这个下标表示方法真是无语了
这里我还能接受,第0行,第一个:
y_hat[0,1]:0.30000001192092896
我对这个下标表示方法真是无语了
第0行,第0个,第1行,第2个,我真的不理解:
y_hat[[0,1],y]:tensor([0.1000, 0.5000])
y_hat[[0,1],[0,2]]:tensor([0.1000, 0.5000])

有篇文章:或许能说明点问题

看到这里,好像想起了些东西,又好像有点晕。

好了,上函数。

def accurancy(y_hat,y):
    """计算预测正确的数量"""
    if len(y_hat.shape)>1 and y_hat.shape[1]>1:   #y_hat是一个二维矩阵的话它的shape>1,它的列数也>1.
        y_hat=y_hat.argmax(axis=1)                #对每一行求argmax-每一行中元素最大的那个下标存到y_hat里面。
    cmp=y_hat.type(y.dtype)==y                    #y_hat和y的数据类型可能不一样,把y_hat转成y的数据类型,变成一个bool的tensor。
    return float(cmp.type(y.dtype).sum())         #转成跟y一样的形状,求和。

print(accurancy(y_hat,y)/len(y))                  #找出来预测正确的样本数,再除以y的长度-预测正确的概率。

 不慌,再来:

#将预测类别和真实y元素进行比较,因为做的是分类问题。
def accurancy(y_hat,y):                           #给定预测值y_hat和真实值y,计算分类正确的类别数.
    """计算预测正确的数量"""
    if len(y_hat.shape)>1 and y_hat.shape[1]>1:#如果y_hat是一个二维矩阵的话,它的shape>1,它的列数也>1.
        print("什么都没做的时候:")
        print(f"y_hat:{y_hat}")
        print(f"y:{y}")
        print(f"y_hat.shape:{y_hat.shape}")
        print(f"y.shape:{y.shape}")
        print(f"y_hat.type:{y_hat.type}")
        print(f"y_hat.dtype:{y_hat.dtype}")
        print(f"y.type:{y.type}")
        print(f"y.dtype:{y.dtype}")
        y_hat=y_hat.argmax(axis=1) #对每一行求argmax-每一行中元素最大的那个下标存到y_hat里面。
        print("对每一行求argmax-每一行中元素最大的那个下标存到y_hat里面:")
        print(f"y_hat:{y_hat}")
        print(f"y:{y}")
        print(f"y_hat.shape:{y_hat.shape}")
        print(f"y.shape:{y.shape}")
        print(f"y_hat.type:{y_hat.type}")
        print(f"y_hat.dtype:{y_hat.dtype}")
        print(f"y.type:{y.type}")
        print(f"y.dtype:{y.dtype}")
    cmp=y_hat.type(y.dtype)==y  #y_hat和y的数据类型可能不一样,把y_hat转成y的数据类型,变成一个bool的tensor。
    print("y_hat和y的数据类型可能不一样,把y_hat转成y的数据类型,变成一个bool的tensor。")
    print(f"y_hat.type:{y_hat.type}")
    print(f"y_hat.dtype:{y_hat.dtype}")
    print(f"y.type:{y.type}")
    print(f"y.dtype:{y.dtype}")
    print(f"cmp:{cmp}")
    print(f"float(cmp.type(y.dtype).sum()):{float(cmp.type(y.dtype).sum())}")
    return float(cmp.type(y.dtype).sum())         #转成跟y一样的形状,求和。

print(accurancy(y_hat,y)/len(y))                  #找出来预测正确的样本数,再除以y的长度-预测正确的概率。

输出:

什么都没做的时候:
y_hat:tensor([[0.1000, 0.3000, 0.6000],
        [0.3000, 0.2000, 0.5000]])
y:tensor([0, 2])
y_hat.shape:torch.Size([2, 3])
y.shape:torch.Size([2])
y_hat.type:<built-in method type of Tensor object at 0x0000020A0CBAF040>
y_hat.dtype:torch.float32
y.type:<built-in method type of Tensor object at 0x0000020A088EB590>
y.dtype:torch.int64
对每一行求argmax-每一行中元素最大的那个下标存到y_hat里面:
y_hat:tensor([2, 2])
y:tensor([0, 2])
y_hat.shape:torch.Size([2])
y.shape:torch.Size([2])
y_hat.type:<built-in method type of Tensor object at 0x0000020A0CB482C0>
y_hat.dtype:torch.int64
y.type:<built-in method type of Tensor object at 0x0000020A088EB590>
y.dtype:torch.int64
y_hat和y的数据类型可能不一样,把y_hat转成y的数据类型,变成一个bool的tensor。
y_hat.type:<built-in method type of Tensor object at 0x0000020A0CB482C0>
y_hat.dtype:torch.int64
y.type:<built-in method type of Tensor object at 0x0000020A088EB590>
y.dtype:torch.int64
cmp:tensor([False,  True])
float(cmp.type(y.dtype).sum()):1.0
0.5

就这样吧。

猜你喜欢

转载自blog.csdn.net/qq_45828494/article/details/126515819