我正在处理一个问题,并试图使用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 answerArraydef custom_metric(label, bucketArray): return numpy.mean(numpy.power(calculate_sales_from_bucket(label)-calculate_sales_from_bucket(bucketArray),2))model.fit( train_iter, # 训练数据 eval_data=val_iter, # 验证数据 batch_end_callback = mx.callback.Speedometer(batch_size, 1000), # 每1000个数据批次输出进度 num_epoch = 10, # 训练数据的传递次数 optimizer = 'adam', eval_metric = mx.metric.create(custom_metric), optimizer_params=(('learning_rate', 1),))
我的输出结果是:
INFO:root:Epoch[0] Validation-custom_metric=38263835679935.953125INFO:root:Epoch[1] Batch [1000] Speed: 91353.72 samples/sec Train-custom_metric=39460550891.057487INFO:root:Epoch[1] Batch [2000] Speed: 96233.05 samples/sec Train-custom_metric=9483.127650INFO:root:Epoch[1] Batch [3000] Speed: 90828.09 samples/sec Train-custom_metric=57538.891485INFO:root:Epoch[1] Batch [4000] Speed: 93025.54 samples/sec Train-custom_metric=59861.927745INFO:root:Epoch[1] Train-custom_metric=8351.460495INFO:root:Epoch[1] Time cost=9.466INFO:root:Epoch[1] Validation-custom_metric=38268.250469INFO:root:Epoch[2] Batch [1000] Speed: 94028.96 samples/sec Train-custom_metric=58864.659051INFO:root:Epoch[2] Batch [2000] Speed: 94562.38 samples/sec Train-custom_metric=9482.873310INFO:root:Epoch[2] Batch [3000] Speed: 93198.68 samples/sec Train-custom_metric=57538.891485INFO:root:Epoch[2] Batch [4000] Speed: 93722.89 samples/sec Train-custom_metric=59861.927745INFO:root:Epoch[2] Train-custom_metric=8351.460495INFO:root:Epoch[2] Time cost=9.341INFO:root:Epoch[2] Validation-custom_metric=38268.250469
在这种情况下,尽管批次的训练自定义指标发生了变化,但训练自定义指标仍然保持不变。例如,在第1个和第2个epoch的第1000批次中就是如此。
我认为这是一个问题,因为无论epoch步骤的值如何,训练自定义指标和验证自定义指标都没有变化。我是MXNet的新手,我的假设可能有误。
你能确认我传递eval_metric的方式是否正确吗?
回答:
我不确定我是否理解了你的问题。你的输出显示训练自定义指标给出了不同的值,只是碰巧在每个epoch的最后两个批次中给出了相同的结果。这可能是你的模型收敛方式的一个特点。
需要明确的一点是,eval_metric仅用于提供调试输出——它实际上并不在学习过程中用作损失函数: