python - 如何计算准确率?
问题描述
我正在努力研究如何从我的神经网络计算准确性。我正在使用带有反向传播算法的 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 的某些部分?
谢谢你的帮助!
解决方案
我假设您的测试集(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)并执行相同操作。
祝你好运
推荐阅读
- javascript - 提供给文本输入预期字符串的类型号的无效道具值反应本机
- windows - 如何在 Windows 10 上更改功能区颜色
- python - 如何正确地将逻辑分离到单独的应用程序中?
- css - 这个 CSS 代码对 Wordpress 主页有什么影响?
- ansible - 使用 Ansible 设置 GlusterFS 集群
- html - Div 仅滚动到宽度
- ios - 如何在 Swift 中更改视频分辨率
- tensorflow - 对于 tensorflow,ResourceApplyAdam 和 ApplyAdam 有什么区别?
- javascript - 如何在 HTML 中显示 JavaScript 函数的结果?
- android - Pjsip Android:视频通话 - 窗口预览大小问题(SurfaceView 用于显示视频帧)