Keras的to_categorical函数如何实现one hot编码转换?

2026-05-22 02:220阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计943个文字,预计阅读时间需要4分钟。

Keras的to_categorical函数如何实现one hot编码转换?

Keras的`to_categorical`方法,源码中这样描述:将类别向量(整数)转换为二进制类别矩阵。例如,用于与categorical_crossentropy配合使用。也就是说,它是将一个类型容器(整数)的转换。

keras.utils.to_categorical这个方法,源码中,它是这样写的:

Converts a class vector (integers) to binary class matrix.

E.g. for use with categorical_crossentropy.

也就是说它是对于一个类型的容器(整型)的转化为二元类型矩阵。比如用来计算多类别交叉熵来使用的。

其参数也很简单:

Keras的to_categorical函数如何实现one hot编码转换?

def to_categorical(y, num_classes=None): Arguments y: class vector to be converted into a matrix (integers from 0 to num_classes). num_classes: total number of classes.

说的很明白了,y就是待转换容器(其类型为从0到类型数目),而num_classes则是类型的总数。

阅读全文

本文共计943个文字,预计阅读时间需要4分钟。

Keras的to_categorical函数如何实现one hot编码转换?

Keras的`to_categorical`方法,源码中这样描述:将类别向量(整数)转换为二进制类别矩阵。例如,用于与categorical_crossentropy配合使用。也就是说,它是将一个类型容器(整数)的转换。

keras.utils.to_categorical这个方法,源码中,它是这样写的:

Converts a class vector (integers) to binary class matrix.

E.g. for use with categorical_crossentropy.

也就是说它是对于一个类型的容器(整型)的转化为二元类型矩阵。比如用来计算多类别交叉熵来使用的。

其参数也很简单:

Keras的to_categorical函数如何实现one hot编码转换?

def to_categorical(y, num_classes=None): Arguments y: class vector to be converted into a matrix (integers from 0 to num_classes). num_classes: total number of classes.

说的很明白了,y就是待转换容器(其类型为从0到类型数目),而num_classes则是类型的总数。

阅读全文