可能在MXNet中错误地使用了自定义eval_metric

时间:2017-08-09 13:31:21

标签: machine-learning neural-network mxnet

我正在解决一个问题,正在尝试使用MXNet解决问题。我试图在代码中使用自定义指标。 相同的代码是:

def calculate_sales_from_bucket(bucketArray):
    return numpy.asarray(numpy.power(10, calculate_max_index_from_bucket(bucketArray)))

def calculate_max_index_from_bucket(bucketArray):
    answerArray = []
    for bucketValue in bucketArray:
        index, value = max(enumerate(bucketValue), key=operator.itemgetter(1))
        answerArray.append(index)
    return answerArray


def custom_metric(label, bucketArray):
    return numpy.mean(numpy.power(calculate_sales_from_bucket(label)-calculate_sales_from_bucket(bucketArray),2))

model.fit(
    train_iter,         # training data
    eval_data=val_iter, # validation data
    batch_end_callback = mx.callback.Speedometer(batch_size, 1000),    # output progress for each 1000 data batches
    num_epoch = 10,     # number of data passes for training 
    optimizer = 'adam',
    eval_metric = mx.metric.create(custom_metric),
    optimizer_params=(('learning_rate', 1),)
)

我得到的输出为:

INFO:root:Epoch[0] Validation-custom_metric=38263835679935.953125
INFO:root:Epoch[1] Batch [1000]      Speed: 91353.72 samples/sec        Train-custom_metric=39460550891.057487
INFO:root:Epoch[1] Batch [2000]        Speed: 96233.05 samples/sec  Train-custom_metric=9483.127650
INFO:root:Epoch[1] Batch [3000] Speed: 90828.09 samples/sec   Train-custom_metric=57538.891485
INFO:root:Epoch[1] Batch [4000] Speed: 93025.54 samples/sec   Train-custom_metric=59861.927745
INFO:root:Epoch[1] Train-custom_metric=8351.460495
INFO:root:Epoch[1] Time cost=9.466
INFO:root:Epoch[1] Validation-custom_metric=38268.250469
INFO:root:Epoch[2] Batch [1000]     Speed: 94028.96 samples/sec       Train-custom_metric=58864.659051
INFO:root:Epoch[2] Batch [2000]     Speed: 94562.38 samples/sec       Train-custom_metric=9482.873310
INFO:root:Epoch[2] Batch [3000]      Speed: 93198.68 samples/sec        Train-custom_metric=57538.891485
INFO:root:Epoch[2] Batch [4000]      Speed: 93722.89 samples/sec        Train-custom_metric=59861.927745
INFO:root:Epoch[2] Train-custom_metric=8351.460495
INFO:root:Epoch[2] Time cost=9.341
INFO:root:Epoch[2] Validation-custom_metric=38268.250469

在这种情况下,无论批次的train-custom_metric如何变化,train-custom_metric仍然是相同的。 就像纪录1和纪元2的批次1000的情况一样。

我认为这是一个问题,因为Train-custom_metric和Validation-custom_metric不会发生变化,而与epoch步骤的值无关。 我是MXNet的初学者,在这个假设中我可能错了。

您能否确认我是否以正确的方式传递eval_metric?

1 个答案:

答案 0 :(得分:1)

我不确定我是否理解这个问题。您的输出显示train-custom-metric给出不同的值,它恰好在每个时期的最后两个批次中给出了相同的结果。这可能只是你的模型融合的一个怪癖。

有一点需要明确的是,eval_metric仅用于提供调试输出 - 它在学习期间实际上并未用作损失函数:

https://github.com/apache/incubator-mxnet/issues/1915