0

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

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

工作正常,但是当我使用代码使用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'
4

1 回答 1

1

所以根据官方文档create_study

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

当您访问SQLAlchemy 的文档时,您会发现它使用绝对路径。

所以你所要做的就是改变

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

到绝对路径为:

storage = f"sqlite:///gdrive/My Drive/chapter_classification/output{name}.db"
于 2021-12-29T12:37:35.813 回答