首页 > 解决方案 > 如何计算准确率?

问题描述

我正在努力研究如何从我的神经网络计算准确性。我正在使用带有反向传播算法的 MNIST 数据库。一切都是从头开始。

我的部分代码如下所示:

for x in range(1, epochs+1):
   #Compute Feedforward
   #...
   activationValueOfSoftmax = softmax(Z2)

   #Loss
   #Y are my labels
   loss = - np.sum((Y * np.log(activationValueOfSoftmax)), axis=0, keepdims=True)
   cost = np.sum(loss, axis=1) / m #m is 784

   #Backpropagation
   dZ2 = activationValueOfSoftmax - Y
   #the rest of the parameters
   #...
   #parameters update via Gradient Descent
   #...

我可以据此计算准确性,还是必须重做 NN 的某些部分?

谢谢你的帮助!

标签: pythonneural-network

解决方案


我假设您的测试集(10 位)有 10 大小的一个热 y 向量,并且您通过训练集的前向道具检索了您的假设。

correct = 0

    for i in range(np.shape(y)[0]):
        #argmax retrieves index of max element in hypothesis
        guess = np.argmax(hyp[i, :])
        ans= np.argmax(y[i, :])
        print("guess: ", guess, "| ans: ", ans)
        if guess == match:
            correct = correct + 1;

accuracy = (correct/np.shape(y)[0]) * 100

您必须再次使用您的权重和 TEST SET 数据进行前向 prop 以获得您的假设向量(应该是 10 大小),然后您可以循环遍历测试集中的所有 y 值,使用计数器变量(正确)来检索金额正确。要获得百分比,您只需将正确除以测试集示例的数量并乘以 100。

如果您想要训练集的准确性,只需使用您的假设(在您的情况下为activationValueOfSoftmax)并执行相同操作。

祝你好运


推荐阅读