【问题标题】:Error while saving Optuna study to Google Drive from Colab将 Optuna 研究从 Colab 保存到 Google Drive 时出错
【发布时间】:2022-01-27 19:44:35
【问题描述】:

我可以将随机文件保存到我的驱动器 colab 中:

with open ("gdrive/My Drive/chapter_classification/output/hello.txt",'w')as f:
  f.write('hello')

工作正常,但是当我使用代码使用Official documentation approach of Optuna 时:


direction = 'minimize'
name = 'opt1'
study = optuna.create_study(sampler=optuna.samplers.TPESampler(),direction=direction,study_name=name, storage=f"gdrive/My Drive/chapter_classification/output/sqlite:///{name}.db",load_if_exists=True)
study.optimize(tune, n_trials=1000)

抛出错误:

ArgumentError                             Traceback (most recent call last)
<ipython-input-177-f32da2c0f69a> in <module>()
      2 direction = 'minimize'
      3 name = 'opt1'
----> 4 study = optuna.create_study(sampler=optuna.samplers.TPESampler(),direction=direction,study_name=name, storage="gdrive/My Drive/chapter_classification/output/sqlite:///opt1.db",load_if_exists=True)
      5 study.optimize(tune, n_trials=1000)

6 frames
/usr/local/lib/python3.7/dist-packages/optuna/study/study.py in create_study(storage, sampler, pruner, study_name, direction, load_if_exists, directions)
   1134     ]
   1135 
-> 1136     storage = storages.get_storage(storage)
   1137     try:
   1138         study_id = storage.create_new_study(study_name)

/usr/local/lib/python3.7/dist-packages/optuna/storages/__init__.py in get_storage(storage)
     29             return RedisStorage(storage)
     30         else:
---> 31             return _CachedStorage(RDBStorage(storage))
     32     elif isinstance(storage, RDBStorage):
     33         return _CachedStorage(storage)

/usr/local/lib/python3.7/dist-packages/optuna/storages/_rdb/storage.py in __init__(self, url, engine_kwargs, skip_compatibility_check, heartbeat_interval, grace_period, failed_trial_callback)
    173 
    174         try:
--> 175             self.engine = create_engine(self.url, **self.engine_kwargs)
    176         except ImportError as e:
    177             raise ImportError(

<string> in create_engine(url, **kwargs)

/usr/local/lib/python3.7/dist-packages/sqlalchemy/util/deprecations.py in warned(fn, *args, **kwargs)
    307                         stacklevel=3,
    308                     )
--> 309             return fn(*args, **kwargs)
    310 
    311         doc = fn.__doc__ is not None and fn.__doc__ or ""

/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/create.py in create_engine(url, **kwargs)
    528 
    529     # create url.URL object
--> 530     u = _url.make_url(url)
    531 
    532     u, plugins, kwargs = u._instantiate_plugins(kwargs)

/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/url.py in make_url(name_or_url)
    713 
    714     if isinstance(name_or_url, util.string_types):
--> 715         return _parse_rfc1738_args(name_or_url)
    716     else:
    717         return name_or_url

/usr/local/lib/python3.7/dist-packages/sqlalchemy/engine/url.py in _parse_rfc1738_args(name)
    775     else:
    776         raise exc.ArgumentError(
--> 777             "Could not parse rfc1738 URL from string '%s'" % name
    778         )
    779 

ArgumentError: Could not parse rfc1738 URL from string 'gdrive/My Drive/chapter_classification/output/sqlite:///opt1.db'

【问题讨论】:

    标签: python sqlite google-colaboratory optuna gdrive


    【解决方案1】:

    所以根据official Documentation of create_study

    当传递数据库 URL 时,Optuna 在内部使用 SQLAlchemy 来处理数据库。有关详细信息,请参阅 SQLAlchemy 的文档。如果你想为 SQLAlchemy Engine 指定非默认选项,你可以用你想要的选项来实例化 RDBStorage 并将它传递给 storage 参数而不是 URL。

    而当你访问documentation of SQLAlchemy时,你会发现它使用的是绝对路径。

    所以你要做的就是改变

    storage=f"gdrive/My Drive/chapter_classification/output/sqlite:///{name}.db"
    

    到绝对路径为:

    storage = f"sqlite:///gdrive/My Drive/chapter_classification/output{name}.db"
    

    【讨论】:

      猜你喜欢
      • 2021-02-24
      • 1970-01-01
      • 2021-01-30
      • 1970-01-01
      • 2020-06-19
      • 2019-08-30
      • 2020-03-29
      • 2021-08-03
      • 2019-06-11
      相关资源
      最近更新 更多