【问题标题】:xlearn predictions error give a different mse than output by the functionxlearn 预测错误给出的 mse 与函数的输出不同
【发布时间】: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


【解决方案1】:

很多人使用 1/2 MSE 作为损失,因为它使导数“更容易”。鉴于他们使用“损失”这个词而不是“MSE”或类似的东西,我敢打赌这就是正在发生的事情。

为了清楚起见,如果您的损失是

1/2n * [(y_1 - p_1)^2 + ... + (y_n - p_n)^2]

那么导数(wrt p)将是

-1/n * [(y_1 - p_1) + ... + (y_n - p_n)]

2 消失了,因为你最终乘以 2 得到幂规则。

请原谅格式...我不知道如何在这里做数学。

【讨论】:

    【解决方案2】:

    正是MSE / 2

    长话短说:xlearn 库的 MSE 丢失,正如您所提到的,MSE / 2

    对您的用例进行取证后,我可以确认SquaredLoss(在核心库中命名)在其计算中包含*0.5 因素。

    下面你可以按照我的步骤自行验证这个结论。

    如何验证

    第 1 步。

    站在xlearn/src,跑 grep -r "The test loss is" *。它会将您指向solver/inference.cc62 行:

    https://github.com/aksnzhy/xlearn/blob/master/src/solver/inference.cc#L62

    您可以在其中看到打印的数字是loss_ 对象的GetLoss 方法。

    请注意,python 库只是这个底层 C 代码库的包装器。

    第 2 步。

    这个对象是一个损失的实例。这两个定义的损失可以在文件夹src/loss 下找到。它们是 cross-entropy 用于分类,squared loss 用于回归:

    https://github.com/aksnzhy/xlearn/tree/master/src/loss

    第 3 步。

    最后,您可以在这里查看squared loss 的标头和实现:

    在哪里可以看到,虽然in the .h file 说损失是:

    loss = sum_all_example( (y - pred) ^ 2 )

    文件.cc 中的实际实现在计算中具有*0.5 因子,在37100 行中。

    理论原因

    这是机器学习中的常规做法,基于梯度下降法使用损失函数的导数这一事实。拥有这个1/2 因子使得导数在某种程度上更整洁,因为在推导时,2 的幂变成了*2 因子,它与这个添加的1/2 相抵消。另一方面,添加此 1/2 因子不会影响训练目的的指标,因为形状和最小值被保留。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 2018-04-14
      • 2017-05-15
      • 2019-08-08
      • 2019-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多