Giter Site home page Giter Site logo

Exception AttributeError about xgboost HOT 16 CLOSED

liubenyuan avatar liubenyuan commented on May 6, 2024
Exception AttributeError

from xgboost.

Comments (16)

tqchen avatar tqchen commented on May 6, 2024

can u provide a code producing the error?

from xgboost.

liubenyuan avatar liubenyuan commented on May 6, 2024
#!/usr/bin/python2

import sys
import pandas as pd
sys.path.append('/home/postgres/research/ktools/xgboost/python/')
import xgboost as xgb

""" here is an example of how to use converters """
data = pd.read_csv('data/training.csv',converters={32: lambda x:int(x=='s'.encode('utf-8')) })
print 'parse training dataset, shape=', data.shape

weight = data['Weight'].as_matrix()
train_Y = data['Label'].as_matrix()
train_X = data.ix[:,1:31].as_matrix()

""" parse test data """
data = pd.read_csv('data/test.csv')
test_X = data.ix[:,1:31].as_matrix()
idx = data.ix[:,0].as_matrix()
print 'parse test dataset, shape=', test_X.shape

#
test_size = 550000
weight = weight * float(test_size) / len(train_Y)
sum_wpos = sum( weight[i] for i in range(len(train_Y)) if train_Y[i] == 1.0  )
sum_wneg = sum( weight[i] for i in range(len(train_Y)) if train_Y[i] == 0.0  )
# print weight statistics 
print ('weight statistics: wpos=%g, wneg=%g, ratio=%g' % ( sum_wpos, sum_wneg, sum_wneg/sum_wpos ))

# construct xgboost.DMatrix from numpy array, treat -999.0 as missing value
xgmat = xgb.DMatrix( train_X, label=train_Y, missing = -999.0, weight=weight )
xgmat_test = xgb.DMatrix( test_X, missing = -999.0 )
watchlist = [ (xgmat,'train') ]

# setup parameters for xgboost
param = {}
# use logistic regression loss, use raw prediction before logistic transformation
# since we only need the rank
param['objective'] = 'binary:logitraw'
# scale weight of positive examples
param['scale_pos_weight'] = sum_wneg/sum_wpos
param['bst:eta'] = 0.1
param['bst:max_depth'] = 6
param['eval_metric'] = 'auc'
param['silent'] = 1
param['nthread'] = 16

# you can directly throw param in, though we want to watch multiple metrics here 
plst = list(param.items())+[('eval_metric', '[email protected]')]

# boost 120 tres
num_round = 20
print ('loading data end, start to boost trees')
bst = xgb.train( plst, xgmat, num_round, watchlist );
bst.save_model('higgs.model')

print ('finish training')

threshold_ratio = 0.15
outfile = 'higgs.pred.csv'
ypred = bst.predict( xgmat_test )

res  = [ ( int(idx[i]), ypred[i] ) for i in range(len(ypred)) ] 

rorder = {}
for k, v in sorted( res, key = lambda x:-x[1] ):
    rorder[ k ] = len(rorder) + 1

# write out predictions
ntop = int( threshold_ratio * len(rorder ) )
fo = open(outfile, 'w')
nhit = 0
ntot = 0
fo.write('EventId,RankOrder,Class\n')
for k, v in res:
    if rorder[k] <= ntop:
        lb = 's'
        nhit += 1
    else:
        lb = 'b'
    # change output rank order to follow Kaggle convention
    fo.write('%s,%d,%s\n' % ( k,  len(rorder)+1-rorder[k], lb ) )
    ntot += 1
fo.close()

print ('finished writing into prediction file')

from xgboost.

liubenyuan avatar liubenyuan commented on May 6, 2024

it's your startup code using pandas IO, running in spyder-2.3.0 (maybe it is a spyder related issue?)

with

  • numpy-1.8.1
  • pandas-0.14.1
  • python-2.7.8

from xgboost.

tqchen avatar tqchen commented on May 6, 2024

Is it possible to simplify this code that produces the error? I would run for 1 round, and delete the write csv code. The error seems to occur in destructor of Booster, see.
https://github.com/tqchen/xgboost/blob/master/wrapper/xgboost.py#L79

My suspect is that somehow your run makes xgblib become None? If you can help to take a look what happened in the destructor, it would also be great

from xgboost.

tqchen avatar tqchen commented on May 6, 2024

I tried to run on my own machine, seems no such error occurs. I am using the most recent code from master, where python folder is renamed to wrapper

from xgboost.

tqchen avatar tqchen commented on May 6, 2024

OK, I suspect that this is due to the destructing order of python during exit. Maybe xglib in xgboost.py get destroyed before the destructor of DMatrix is called, and thus the error occurs, to validate if this is the cause of the problem, you can validate by print the xglib variable in destructor https://github.com/tqchen/xgboost/blob/master/wrapper/xgboost.py#L79

from xgboost.

liubenyuan avatar liubenyuan commented on May 6, 2024

Hi,

I add print xglib in L79 of wrapper/xgboost.py

    # destructor
    def __del__(self):
        print xglib
        xglib.XGDMatrixFree(self.handle)

the error messages are

None
Exception AttributeError: "'NoneType' object has no attribute 'XGDMatrixFree'" in <bound method DMatrix.__del__ of <xgboost.DMatrix instance at 0x7f711be1b7e8>> ignored
Exception AttributeError: "'NoneType' object has no attribute 'XGDMatrixFree'" in <bound method DMatrix.__del__ of <xgboost.DMatrix instance at 0x7f711be1f440>> ignored

...

None
loading data end, start to boost trees
finish training
finished writing into prediction file
[19]    train-auc:0.929520      [email protected]:4.643400
Exception AttributeError: "'NoneType' object has no attribute 'XGBoosterFree'" in <bound method Booster.__del__ of <xgboost.Booster instance at 0x7f711be1b248>> ignored

from xgboost.

tqchen avatar tqchen commented on May 6, 2024

Seems this is indeed the problem I guessed. xgblib becomes None during Free ....

from xgboost.

tqchen avatar tqchen commented on May 6, 2024

Here is a possible fix. Add self.xglib = xglib in all the init function.
And call self.xglib.XGFree in all the dels.

On Monday, August 25, 2014, Liu Benyuan [email protected] wrote:

Hi,

I add print xglib in L79 of wrapper/xgboost.py

# destructor
def __del__(self):
    print xglib
    xglib.XGDMatrixFree(self.handle)

the error messages are

None
Exception AttributeError: "'NoneType' object has no attribute 'XGDMatrixFree'" in <bound method DMatrix.del of <xgboost.DMatrix instance at 0x7f711be1b7e8>> ignored
Exception AttributeError: "'NoneType' object has no attribute 'XGDMatrixFree'" in <bound method DMatrix.del of <xgboost.DMatrix instance at 0x7f711be1f440>> ignored

...

None
loading data end, start to boost trees
finish training
finished writing into prediction file
[19] train-auc:0.929520 [email protected]:4.643400
Exception AttributeError: "'NoneType' object has no attribute 'XGBoosterFree'" in <bound method Booster.del of <xgboost.Booster instance at 0x7f711be1b248>> ignored


Reply to this email directly or view it on GitHub
https://github.com/tqchen/xgboost/issues/40#issuecomment-53365576.

Sincerely,

Tianqi Chen
Computer Science & Engineering, University of Washington

from xgboost.

tqchen avatar tqchen commented on May 6, 2024

Can you try this fix?

Change the destructor to

     def __del__(self):    
          self.xglib.XGDMatrixFree(self.handle)

and in init https://github.com/tqchen/xgboost/blob/master/wrapper/xgboost.py#L41, add

     def __init__(self, ...):    
         self.xglib = xglib

from xgboost.

liubenyuan avatar liubenyuan commented on May 6, 2024

This error still exists.

<CDLL '/home/postgres/research/ktools/xgboost/wrapper/libxgboostwrapper.so', handle 227b3a0 at 7fe640fbf2d0>

<CDLL '/home/postgres/research/ktools/xgboost/wrapper/libxgboostwrapper.so', handle 227b3a0 at 7fe640fbf2d0>
Exception AttributeError: "'NoneType' object has no attribute 'XGBoosterFree'" in <bound method Booster.__del__ of <xgboost.Booster instance at 0x7fe640e8cdd0>> ignored

where I print self.xglib

from xgboost.

liubenyuan avatar liubenyuan commented on May 6, 2024

However this warning can be safely ignored ?

from xgboost.

tqchen avatar tqchen commented on May 6, 2024

Can you make sure you called self.xglib.XGBoosterFree instead of xglib.XGBoosterFree ?

Since from the printed message, self.xglib is not NoneType, and the error is still because the handle is NoneType

This means the destructor is not called, and the memory might not be freed. But it won't affect the performance of your code.

from xgboost.

liubenyuan avatar liubenyuan commented on May 6, 2024

yes, I confirmed that by adding:

    def __del__(self):
        print self.xglib
        print 'called __del__'
        self.xglib.XGDMatrixFree(self.handle)

However I think the error is related to spyder, but not your code. Open ipython2 and run higgs-play_36.py twice, and the messages will pump out without error.

<CDLL '/home/postgres/research/ktools/xgboost/wrapper/libxgboostwrapper.so', handle 1108d60 at 7f3caa42fc10>
called __del__
<CDLL '/home/postgres/research/ktools/xgboost/wrapper/libxgboostwrapper.so', handle 1108d60 at 7f3caa42fc10>
called __del__

from xgboost.

tqchen avatar tqchen commented on May 6, 2024

Thanks for clarifying the things!

from xgboost.

oldmonk101 avatar oldmonk101 commented on May 6, 2024

Still exists in LTR, only when I use set_group and pass a list

from xgboost.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.