【发布时间】:2021-05-07 14:45:37
【问题描述】:
xlearn predict 函数提供的 mse 与您通过查看预测并自行计算得到的结果不同。这是执行此操作的代码;您可以通过克隆xlearn repository 并将以下代码复制到存储库中的demo/regression/house_price 中来运行它
# Copyright (c) 2018 by contributors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import xlearn as xl
import pandas as pd
from sklearn.metrics import mean_squared_error
# Training task
# param:
# 0. regression task
# 1. learning rate: 0.2
# 2. regular lambda: 0.002
# 3. evaluation metric: mae
fm_model = xl.create_linear() # Use factorization machine
fm_model.setTrain("./house_price_train.txt") # Training data
fm_model.setValidate("./house_price_test.txt") # Validation data
fm_model.disableNorm()
# fm_model.setSigmoid()
# fm_model.disableEarlyStop()
param = {'task':'reg', 'lr':0.0002,
'lambda':0.00001, 'metric':'rmse', 'epoch':100}
# Start to train
# The trained model will be stored in model.out
print("here")
fm_model.fit(param, './model.out')
fm_model.setTest("./house_price_test.txt") # Test data
# Prediction task
# Start to predict
# The output result will be stored in output.txt
outs = fm_model.predict("./model.out", "./output.txt")
true = pd.read_csv("./house_price_test.txt", sep='\t', header=None)[0]
# print(true)
preds = pd.read_csv("./output.txt", header=None)[0]
# Calculate using sklearn
print(mean_squared_error(true, preds))
# Self calculate
sq = 0.0
for t, p in zip(true, preds):
sq += (t - p) ** 2
print(sq/len(true))
如果你把它保存为 min_eg.py,运行它(在安装 xlearn 之后)
python min_eg.py 简单。
这是你得到的输出:
有趣的是,您得到的 MSE 始终是 predict 函数报告的 mse 的两倍。
非常感谢任何帮助;我想知道其他人是否遇到过同样的问题。
【问题讨论】:
-
您的意思是“测试丢失”和 MSE 之间的比较。 sklearn 代码是python,但是xlearn 是用C 编写的,也许是C 实现?向在该软件包中工作的人询问也许很好。
-
我在他们的 github 上发布了这个 - github.com/aksnzhy/xlearn/issues/357
标签: python machine-learning scikit-learn loss-function