简体   繁体   English

如何在 CNN 中找到模型的准确性?

[英]How to find accuracy of a model in CNN?

I want to find the accuracy of a customised CNN model.我想找到自定义 CNN 模型的准确性。 I have weights(w), loss value(l) and test data(x_test) with the class variable(y_test).我有类变量(y_test)的权重(w)、损失值(l)和测试数据(x_test)。 The weights can't be adjusted, they should remain the same.权重不能调整,它们应该保持不变。 It will be like a single layer feedforward neural network.它就像一个单层前馈神经网络。

I want to complete this function to give the same accuracy as in Keras.我想完成此功能以提供与 Keras 相同的准确性。

Edit 1: Binary class classification problem.编辑 1:二元类分类问题。

  def accuracy(x_test,y_test,w,l):
     y_pred=numpy.dot(x_test,w)
     acc=...
     return acc

How to complete the accuracy statement as they do in Keras or any other API?.如何像在 Keras 或任何其他 API 中那样完成准确性声明?

If you want something similar to keras , you just care about y_test and y_pred ( y_test is y_true ):如果你想要类似于keras 的东西,你只关心y_testy_predy_testy_true ):

def acc(y_true, y_pred):
    return np.equal(np.argmax(y_true, axis=-1), np.argmax(y_pred, axis=-1)).mean()

This is my POC:这是我的 POC:

import numpy as np

# y_test onehot encoded
y_test = np.array([[1, 0, 0],[0, 1, 0],[0, 0, 1], [0, 1, 0], [1, 0, 0]])
y_pred = np.random.random((5,3))

print("y_true: " + str(np.argmax(y_test, axis=-1)))
print("y_pred: " + str(np.argmax(y_pred, axis=-1)))

def acc(y_true, y_pred):
    return np.equal(np.argmax(y_true, axis=-1), np.argmax(y_pred, axis=-1)).mean()

print("accuracy: " + str(acc(y_test, y_pred)))

Result:结果:

y_real: [0 1 2 1 0]
y_pred: [1 1 0 1 0]
accuracy: 0.6

Update 1: Since it is for binary classification the function will be this:更新 1:由于它用于二进制分类,因此函数将是:

def acc(y_true, y_pred):
    return np.equal(y_true, np.round(y_pred)).mean()

POC:概念验证:

import numpy as np

y_test = np.array([1, 0, 0, 1, 0])
y_pred = np.random.random((5))

print("y_true: " + str(y_test))
print("y_pred: " + str(np.round(y_pred).astype(int)))

def acc(y_true, y_pred):
    return np.equal(y_true, np.round(y_pred)).mean()

print("accuracy: " + str(acc(y_test, y_pred)))

If I'm not wrong you can pass your actual and predicted values to scoring function available inside mlxtend library.如果我没错,您可以将actual值和predicted值传递给mlxtend库中可用的scoring函数。

Look Scoring - mlxtend for more explanation.查看评分 - mlxtend以获得更多解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM