tf.keras.metrics.CategoricalAccuracy函数

函数原型
tf.keras.metrics.CategoricalAccuracy(
    name='categorical_accuracy', 
    dtype=None
)
函数说明

CategoricalAccuracy函数用于计算多分类问题的准确率。CategoricalAccuracy函数的计算过程如下:

def categorical_accuracy(y_true, y_pred):
    return K.cast(K.equal(K.argmax(y_true, axis=-1),
                          K.argmax(y_pred, axis=-1)),
                  K.floatx())

函数检查 y_ture 中最大值对应的index与y_pred 中最大值对应的index是否相等。其中,y_true 应为一个 one-hot向量。

另外,如果使用SparseCategoricalAccuracy函数,y_true 应为一个表示类型的整数。

函数使用
# y_true为one-hot形式
>>> y_true = [[0, 0, 1], [0, 1, 0], [0, 1, 0]]
>>> y_pred = [[0.2, 0.3, 0.5], [0.3, 0.5, 0.2], [0.1, 0.1, 0.8]]
>>> accuracy = tf.keras.metrics.CategoricalAccuracy()
>>> accuracy_calc = accuracy(y_true, y_pred)
>>> accuracy_calc
<tf.Tensor: shape=(), dtype=float32, numpy=0.6666667>

# 对比
# y_true为整数形式
>>> y_true = [2, 1, 1]
>>> y_pred = [[0.2, 0.3, 0.5], [0.3, 0.5, 0.2], [0.1, 0.1, 0.8]]
>>> accuracy = tf.keras.metrics.SparseCategoricalAccuracy()
>>> accuracy_calc = accuracy(y_true, y_pred)
>>> accuracy_calc
<tf.Tensor: shape=(), dtype=float32, numpy=0.6666667>
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
boston_housing module: Boston housing price regression dataset. cifar10 module: CIFAR10 small images classification dataset. cifar100 module: CIFAR100 small images classification dataset. fashion_mnist module: Fashion-MNIST dataset. imdb module: IMDB sentiment classification dataset. mnist module: MNIST handwritten digits dataset. reuters module: Reuters topic classification dataset. import tensorflow as tf from tensorflow import keras fashion_mnist = keras.datasets.fashion_mnist (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data() mnist = keras.datasets.mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() cifar100 = keras.datasets.cifar100 (x_train, y_train), (x_test, y_test) = cifar100.load_data() cifar10 = keras.datasets.cifar10 (x_train, y_train), (x_test, y_test) = cifar10.load_data() imdb = keras.datasets.imdb (x_train, y_train), (x_test, y_test) = imdb.load_data() # word_index is a dictionary mapping words to an integer index word_index = imdb.get_word_index() # We reverse it, mapping integer indices to words reverse_word_index = dict([(value, key) for (key, value) in word_index.items()]) # We decode the review; note that our indices were offset by 3 # because 0, 1 and 2 are reserved indices for "padding", "start of sequence", and "unknown". decoded_review = ' '.join([reverse_word_index.get(i - 3, '?') for i in x_train[0]]) print(decoded_review) boston_housing = keras.datasets.boston_housing (x_train, y_train), (x_test, y_test) = boston_housing.load_data() reuters= keras.datasets.reuters (x_train, y_train), (x_test, y_test) = reuters.load_data() tf.keras.datasets.reuters.get_word_index( path='reuters_word_index.json' )

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不负韶华ღ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值