chainer.functions.accuracy(y, t, ignore_label=None)[source]

多クラス分類のミニバッチの正解率を計算する。

 

Parameters:
  • y (Variable or numpy.ndarray or cupy.ndarray) – 配列。この配列の(i, j, k, ...)番目の要素は (i, k, ...)番目のサンプルでクラスjのスコアを示す。予測ラベルt̂ t^ は以下の式で計算される。t̂ (i,k,...)=argmaxjy(i,j,k,...)t^(i,k,...)=argmaxj⁡y(i,j,k,...)
  • t (Variable or numpy.ndarray or cupy.ndarray of numpy.int32) – 正解率データ(Ground Truth)ラベルの配列。
  • ignore_label (int or None) – trueラベルが  ignore_label の場合、正解率計算をスキップする。
Returns:

正解率のスカラ配列を保持しているVariable 。

Return type:

Variable

Note

この関数は微分不可能です。

 

Example

 

最も一般的な、 y が2次元配列の場合です。


>>> y = np.array([[0.1, 0.7, 0.2], # prediction label is 1

... [8.0, 1.0, 2.0], # prediction label is 0
... [-8.0, 1.0, 2.0], # prediction label is 2
... [-8.0, -1.0, -2.0]]) # prediction label is 1
>>> t = np.array([1, 0, 2, 1], 'i')
>>> F.accuracy(y, t).data # 100% accuracy because all samples are correct
array(1.0)
>>> t = np.array([1, 0, 0, 0], 'i')
>>> F.accuracy(y, t).data # 50% accuracy because 1st and 2nd samples are correct.
array(0.5)
>>> F.accuracy(y, t, ignore_label=0).data
# 100% accuracy because of ignoring the 2nd,3rd and 4th samples.
array(1.0)