如何在MXNet中确定标签索引

时间:2017-02-28 10:59:02

标签: python mxnet

我试图理解python here

中MXNet的手写数字识别

创建训练数据和标签数据的代码如下所示:

def read_data(label_url, image_url):
    with gzip.open(download_data(label_url)) as flbl:
        magic, num = struct.unpack(">II", flbl.read(8))
        label = np.fromstring(flbl.read(), dtype=np.int8)
    with gzip.open(download_data(image_url), 'rb') as fimg:
        magic, num, rows, cols = struct.unpack(">IIII", fimg.read(16))
        image = np.fromstring(fimg.read(), dtype=np.uint8).reshape(len(label), rows, cols)
    return (label, image)

然后使用以下代码预测数字:

prob = model.predict(val_img[0:1].astype(np.float32)/255)[0]
assert max(prob) > 0.99, "Low prediction accuracy."
print 'Classified as %d with probability %f' % (prob.argmax(), max(prob))

输出为 - 分类为7,概率为0.999391。 我的问题是MXNet如何确定argmax函数返回的索引对应于标签-7

1 个答案:

答案 0 :(得分:1)

值7来自prob.argmax()。该方法返回最高得分值的索引。

https://mxnet.incubator.apache.org/api/python/ndarray.html#mxnet.ndarray.argmax