我使用相当标准的softmax输出,使用MXNet模型预测了100K可能的输出中的大约一个。我想将分配给真实标签的概率与模型下的顶级预测进行比较。为了获得前者,我使用了pick操作符;后者我尝试了廉价版(topk操作符)和昂贵版(sort/argsort + slice)。
在这两种情况下,我都得到了相互矛盾的结果。具体地说,在许多情况下,真实标签的概率(使用pick检索)明显高于最高概率输出(使用topk/sort检索)。我想这意味着我做错了什么,但不明白是什么。并不是所有的预测都会发生这种情况,但有相当一部分会发生。
有谁能给我一个提示,告诉我发生了什么事?
代码如下:
for batch in data_iter:
model.forward(batch, is_train=False)
predictions = model.get_outputs()[0]
labels = batch.label[0].as_in_context(predictions.context)
# scores = mx.nd.topk(predictions, axis=1, k=6, ret_typ='value')
scores = mx.nd.sort(predictions, axis=1, is_ascend=0)
scores = mx.nd.slice_axis(scores, axis=1, begin=0, end=6)
label_score = mx.nd.pick(predictions, labels, axis=1)
equal = label_score.asnumpy() <= scores.asnumpy()[:, 0]
if not np.all(equal):
#I think this should never happen but it does frequently发布于 2018-03-01 11:38:09
使用MXNet 1.1.0进行测试,以下代码显示问题没有发生:
for _ in range(10):
predictions = nd.random.uniform(shape=(100, 100000))
labels = nd.array(np.random.randint(0, 99999, size=(100, 1)))
scores = mx.nd.sort(predictions, axis=1, is_ascend=0)
scores = mx.nd.slice_axis(scores, axis=1, begin=0, end=6)
label_score = mx.nd.pick(predictions, labels, axis=1)
equal = label_score.asnumpy() <= scores.asnumpy()[:, 0]
if not np.all(equal):
print("ERROR")https://stackoverflow.com/questions/44910776
复制相似问题