mxnet 回归除以零误差

mxnet regression divide by zero error

尝试编辑 https://mxnet.incubator.apache.org/tutorials/python/linear-regression.html 的示例以开发用于求解二次方程的机器学习解决方案。我现在得到除以零的错误:

INFO:root:Epoch[0] Train-mse=49.961319
INFO:root:Epoch[0] Time cost=0.030
INFO:root:Epoch[0] Validation-mse=58229.367065
INFO:root:Epoch[1] Batch [2]    Speed: 2000.14 samples/sec  mse=361.597036
INFO:root:Epoch[1] Batch [4]    Speed: 2000.14 samples/sec  mse=1903.920013
INFO:root:Epoch[1] Batch [6]    Speed: 2000.14 samples/sec  mse=6117.729675
INFO:root:Epoch[1] Batch [8]    Speed: 1999.67 samples/sec  mse=4203.171875
INFO:root:Epoch[1] Batch [10]   Speed: 2000.14 samples/sec  mse=31765.921204
INFO:root:Epoch[1] Batch [12]   Speed: 2000.14 samples/sec  mse=6946.003112
Traceback (most recent call last):  File "C:\Users\ibraheem\.vscode\extensions\ms-python.python-0.9.1\pythonFiles\PythonTools\visualstudio_py_launcher_nodebug.py", line 74, in run
    _vspu.exec_file(file, globals_obj)
  File "C:\Users\ibraheem\.vscode\extensions\ms-python.python-0.9.1\pythonFiles\PythonTools\visualstudio_py_util.py", line 119, in exec_file

    exec_code(code, file, global_variables)
  File "C:\Users\ibraheem\.vscode\extensions\ms-python.python-0.9.1\pythonFiles\PythonTools\visualstudio_py_util.py", line 95, in exec_code
    exec(code_obj, global_variables)
  File "c:\Users\ibraheem\Desktop\OtherProjects\python_AI_ML\Untitled-1.py", line 37, in <module>
    batch_end_callback = mx.callback.Speedometer(batch_size, 2))
  File "C:\Python27amd64\lib\site-packages\mxnet\module\base_module.py", line 506, in fit
    callback(batch_end_params)
  File "C:\Python27amd64\lib\site-packages\mxnet\callback.py", line 159, in __call__
    speed = self.frequent * self.batch_size / (time.time() - self.tic)
ZeroDivisionError: float division by zero

完整来源:

import mxnet as mx
import numpy as np
import math
import logging
logging.getLogger().setLevel(logging.DEBUG)

train_size = 50
train_data = np.random.uniform(0.1, 1, [train_size, 3])
for i in range(0, train_size):
    train_data[i, 1] *= 25
train_label = np.array([(-train_data[i][1] + math.sqrt(train_data[i][1] ** 2 - 4 * train_data[i][0] * train_data[i][2])) / (2 * train_data[i][0]) for i in range(train_size)])

#Evaluation Data
eval_data = np.array([[7,3,-34],[6,1,-57],[12,5,-3152]])
eval_label = np.array([2,3,16])

#training
batch_size = 1
train_iter = mx.io.NDArrayIter(train_data,train_label, batch_size, shuffle=True,label_name='lin_reg_label')
eval_iter = mx.io.NDArrayIter(eval_data, eval_label, batch_size, shuffle=False)

X = mx.sym.Variable('data')
Y = mx.symbol.Variable('lin_reg_label')
fully_connected_layer = mx.sym.FullyConnected(data=X, name='fc1', num_hidden = 1)
lro = mx.sym.LinearRegressionOutput(data=fully_connected_layer, label=Y, name="lro")

model = mx.mod.Module(
    symbol = lro ,
    data_names=['data'],
    label_names = ['lin_reg_label']# network structure
)
mx.viz.plot_network(symbol=lro)
model.fit(train_iter, eval_iter,
            optimizer_params={'learning_rate':0.005, 'momentum': 0.9},
            num_epoch=50,
            eval_metric='mse',
            batch_end_callback = mx.callback.Speedometer(batch_size, 2))

print model.predict(eval_iter).asnumpy()

我乘以火车数据的 2 因子以避免方程的非实数解

我找到了问题的原因。将 Speedometer 参数的值从 2 增加到 50。它速度较慢但有效:

model.fit(train_iter, eval_iter,
            optimizer_params={'learning_rate':0.005, 'momentum': 0.9},
            num_epoch=50,
            eval_metric='mse',
            batch_end_callback = mx.callback.Speedometer(batch_size, 50))