Xgb.save setting a file path

Platform is R, I am saving XGBoost model using XGBoost.save. However, if I provide a path (similar as one would save using RDS) it seems I cannot specify a path in XGB.save. Am I missing something? -Glenn

Can you post an example code?

In general, you should pass to xgb.save a string representing a file path

This works
xgb.save(xgboost_model, ‘xgboost.hlb’)

This does not work - giving a path like one would saveRDS does not save the model to a directory
xgb.save(xgboost_model, ‘c:/users/prepaymentmodels/xgboost.hlb’)

Please post the full error message for the second example.

Here is the error message. This path saves plot and other objects fine. Operating system is MAC

xgb.save(xgboost_model, ‘~/Documents/bondlabprepaymentmodel/xgboost_hlb’)
Error in xgb.save(xgboost_model, “~/Documents/bondlabprepaymentmodel/xgboost_hlb”) :
[13:40:07] amalgamation/…/dmlc-core/src/io/local_filesys.cc:209: Check failed: allow_null: LocalFileSystem::Open “~/Documents/bondlabprepaymentmodel/xgboost_hlb”: No such file or directory
Stack trace:
[bt] (0) 1 xgboost.so 0x0000000114b3d090 dmlc::LogMessageFatal::~LogMessageFatal() + 112
[bt] (1) 2 xgboost.so 0x0000000114cb8215 dmlc::io::LocalFileSystem::Open(dmlc::io::URI const&, char const*, bool) + 917
[bt] (2) 3 xgboost.so 0x0000000114cc0aa1 dmlc::Stream::Create(char const*, char const*, bool) + 65
[bt] (3) 4 xgboost.so 0x0000000114bb336a XGBoosterSaveModel + 122
[bt] (4) 5 xgboost.so 0x0000000114b3b12b XGBoosterSaveModel_R + 59
[bt] (5) 6 libR.dylib 0x0000000107c9562d R_doDotCall + 1437
[bt] (6) 7 libR.dylib 0x0000000107ce151a bcEval + 105338
[bt] (7) 8 libR.dylib

There could be an issue with using the shorthand ~? How about using the fully qualified path /Users/[user_name]/Documents/... instead?

@glenn Hello, I just experimented with XGBoost on my Macbook Pro.

  • Specifying a relative path works:
xgb.save(bst, './test.model')
  • Specifying the fully qualified path works:
xgb.save(bst, '/Users/phcho/Desktop/xgboost/test.model')
  • On the other hand, using the ~ shorthand for the home directory causes error:
xgb.save(bst, '~/Desktop/xgboost/test.model')  # FAILS

Error log:

Error in xgb.save(bst, "~/Desktop/xgboost/test.model") : 
  [13:59:06] ../dmlc-core/src/io/local_filesys.cc:209: Check failed: allow_null:  LocalFileSystem::Open "~/Desktop/xgboost/test.model": No such file or directory
Stack trace:
  [bt] (0) 1   xgboost.so                          0x0000000115505e0e dmlc::LogMessageFatal::~LogMessageFatal() + 110
  [bt] (1) 2   xgboost.so                          0x00000001156fb1bf dmlc::io::LocalFileSystem::Open(dmlc::io::URI const&, char const*, bool) + 1039
  [bt] (2) 3   xgboost.so                          0x00000001156df49e dmlc::Stream::Create(char const*, char const*, bool) + 62
  [bt] (3) 4   xgboost.so                          0x0000000115512efa XGBoosterSaveModel + 122
  [bt] (4) 5   xgboost.so                          0x0000000115503f2b XGBoosterSaveModel_R + 59
  [bt] (5) 6   libR.dylib                          0x00000001031def8c R_doDotCall + 2926
  [bt] (6) 7   libR.dylib                          0x0000000103221f08 bcEval + 104761
  [bt] (7) 8   libR.dylib                          0x00
Execution halted

@glenn Do you see a similar issue on the Windows platform? A quick search suggests that, when specifying a full path, you should use double backslashes (\\): https://stackoverflow.com/a/11466233

I submitted https://github.com/dmlc/xgboost/pull/6531 to handle paths containing ~.

@hcho3, I just re-ran the command to save using the full path instead of the shortcut and it works
/users/glennschultz/Documents/bondlabprepaymentmodel/xgboost.hlb. I guess I should have thought of that myself just so conditioned to shortcut I suppose. Thank you for your help. _Glenn

@hcho I don’t have a windows machine so I can’t say.

Got it. I’ll probably come back to Windows paths later then. As for the ~ letter, future releases of XGBoost will be able to handle ~ in paths.

@hcho3 great, thank you very much. - Glenn