本文参考mnist.py中dense_to_one_hot函数,展示标签生成one-hot向量的过程。
>>> import numpy as np
>>> labels_dense=np.asarray([0,3,2,3,1,1])
>>> num_labels = labels_dense.shape[0]
>>> num_classes=4
>>> index_offset = np.arange(num_labels) * num_classes
>>> labels_one_hot = np.zeros((num_labels, num_classes))
>>> labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
>>> labels_one_hot
array([[ 1., 0., 0., 0.],
[ 0., 0., 0., 1.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.],
[ 0., 1., 0., 0.],
[ 0., 1., 0., 0.]])
本文通过Python和NumPy实现了一个简单的例子,展示了如何将MNIST数据集中的标签转换为one-hot编码形式,这对于训练分类器至关重要。

3127

被折叠的 条评论
为什么被折叠?



