Closed alexdyysp closed 4 years ago
Same problem.
It is caused by different default float types between pytorch (float32) and numpy/sklearn (float64).
I solved by turning the model output to float64 (.astype("float64")
). I modified two places in BaseModel.fit
and BaseModel.predict
methods and the problem is solved in my case.
fit():
train_result[name].append(metric_fun(
y.cpu().data.numpy(), y_pred.cpu().data.numpy().astype("float64")))
predict():
return np.concatenate(pred_ans).astype("float64")
Describe the bug(问题描述) /usr/local/anaconda3/lib/python3.7/site-packages/sklearn/metrics/classification.py:2174: RuntimeWarning: divide by zero encountered in log loss = -(transformed_labels * np.log(y_pred)).sum(axis=1)
To Reproduce(复现步骤) Steps to reproduce the behavior:
Operating environment(运行环境):
Additional context my model and data will predict very small value like 0.0001 and value close to 1 like 0.9998 even appear 0 and 1. so it will make skearn metrci caculate error.