0
我正在解決一個問題,並試圖使用MXNet解決問題。我試圖在代碼中使用自定義指標。 爲同一代碼是:MXNet中自定義eval_metric的可能錯誤使用
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
在這種情況下,不論列車custom_metric的批次變化,列車custom_metric仍一樣。 就像在第一個時期和第二個時期的批量1000的情況一樣。
我相信這是一個問題,因爲Train-custom_metric和Validation-custom_metric不會改變,而與時代步驟的值無關。 我是MXNet的初學者,在這個假設中我可能是錯的。
你能否確認我是否以正確的方式傳遞了eval_metric?