optuna.artifacts
The artifacts 模块提供了在 Optuna 中管理工件(输出文件)的方法。请同时查看 Optuna 工件教程 和 我们的文章。artifacts 涵盖的存储如下:
类名 |
支持的存储 |
|---|---|
|
本地文件系统, 网络文件系统 |
Boto3ArtifactStore |
Amazon S3 兼容对象存储 |
GCSArtifactStore |
Google 云存储 |
备注
在每个 ArtifactStore 中定义的方法并不打算直接被库用户访问。
备注
由于 ArtifactStore 官方未提供用于删除工件的用户API,请参考 如何删除上传到研究中的所有工件? 获取相关技巧。
- class optuna.artifacts.FileSystemArtifactStore(base_path)[源代码]
用于文件系统的工件存储。
- 参数:
base_path (str | Path) – 存储工件的目录的基本路径。
示例
import os import optuna from optuna.artifacts import FileSystemArtifactStore from optuna.artifacts import upload_artifact base_path = "./artifacts" os.makedirs(base_path, exist_ok=True) artifact_store = FileSystemArtifactStore(base_path=base_path) def objective(trial: optuna.Trial) -> float: ... = trial.suggest_float("x", -10, 10) file_path = generate_example(...) upload_artifact( artifact_store=artifact_store, file_path=file_path, study_or_trial=trial, ) return ...
- class optuna.artifacts.Boto3ArtifactStore(bucket_name, client=None, *, avoid_buf_copy=False)[源代码]
一个用于 Boto3 的工件后端。
- 参数:
示例
import optuna from optuna.artifacts import upload_artifact from optuna.artifacts import Boto3ArtifactStore artifact_store = Boto3ArtifactStore("my-bucket") def objective(trial: optuna.Trial) -> float: ... = trial.suggest_float("x", -10, 10) file_path = generate_example(...) upload_artifact( artifact_store=artifact_store, file_path=file_path, study_or_trial=trial, ) return ...
- class optuna.artifacts.GCSArtifactStore(bucket_name, client=None)[源代码]
Google Cloud Storage (GCS) 的制品后端。
- 参数:
bucket_name (str) – 存储工件的桶的名称。
client (google.cloud.storage.Client | None) – 用于存储操作的 google-cloud-storage
客户端。如果未指定,将使用默认设置创建一个新的客户端。
示例
import optuna from optuna.artifacts import GCSArtifactStore, upload_artifact artifact_backend = GCSArtifactStore("my-bucket") def objective(trial: optuna.Trial) -> float: ... = trial.suggest_float("x", -10, 10) file_path = generate_example(...) upload_artifact( artifact_store=artifact_store, file_path=file_path, study_or_trial=trial, ) return ...
在运行此代码之前,您需要安装
gcloud并运行gcloud auth application-default login
以便 Cloud Storage 库可以自动找到凭证。
备注
在 v3.4.0 中作为实验性功能添加。接口可能会在更新版本中发生变化,恕不另行通知。请参阅 https://github.com/optuna/optuna/releases/tag/v3.4.0。
- class optuna.artifacts.Backoff(backend, *, max_retries=10, multiplier=2, min_delay=0.1, max_delay=30)[源代码]
用于指数退避的工件存储中间件。
示例
import optuna from optuna.artifacts import upload_artifact from optuna.artifacts import Boto3ArtifactStore from optuna.artifacts import Backoff artifact_store = Backoff(Boto3ArtifactStore("my-bucket")) def objective(trial: optuna.Trial) -> float: ... = trial.suggest_float("x", -10, 10) file_path = generate_example(...) upload_artifact( artifact_store=artifact_store, file_path=file_path, study_or_trial=trial, ) return ...
- class optuna.artifacts.ArtifactMeta(artifact_id, filename, mimetype, encoding)[源代码]
工件的元信息。
备注
所有与研究或试验关联的工件元数据可以通过
get_all_artifact_meta()列出。工件元数据可用于download_artifact()。
- optuna.artifacts.upload_artifact(*, artifact_store, file_path, study_or_trial, storage=None, mimetype=None, encoding=None)[源代码]
将工件上传到工件存储。
- 参数:
artifact_store (ArtifactStore) – 一个工件存储。
file_path (str) – 要上传的文件的路径。
study_or_trial (Trial | FrozenTrial | Study) – 一个
Trial对象,一个FrozenTrial对象,或一个Study对象。storage (BaseStorage | None) – 一个存储对象。只有当
study_or_trial是FrozenTrial时,此参数才是必需的。mimetype (str | None) – 工件的MIME类型。如果未指定,则从文件扩展名猜测MIME类型。
encoding (str | None) – 该工件的编码,适合用作
Content-Encoding头(例如 gzip)。如果未指定,则从文件扩展名猜测编码。
- 返回:
一个工件ID。
- 返回类型:
- optuna.artifacts.get_all_artifact_meta(study_or_trial, *, storage=None)[源代码]
列出所提供试验或研究的关联工件信息。
- 参数:
study_or_trial (Trial | FrozenTrial | Study) – 一个
Trial对象,一个FrozenTrial对象,或一个Study对象。storage (BaseStorage | None) – 一个存储对象。只有当
study_or_trial是FrozenTrial时,此参数才是必需的。
- 返回类型:
示例
这个函数有用的例子:
import os import optuna # Get the storage that contains the study of interest. storage = optuna.storages.get_storage(storage=...) # Instantiate the artifact store used for the study. # Optuna does not provide the API that stores the used artifact store information, so # please manage the information in the user side. artifact_store = ... # Load study that contains the artifacts of interest. study = optuna.load_study(study_name=..., storage=storage) # Fetch the best trial. best_trial = study.best_trial # Fetch all the artifact meta connected to the best trial. artifact_metas = optuna.artifacts.get_all_artifact_meta(best_trial, storage=storage) download_dir_path = "./best_trial_artifacts/" os.makedirs(download_dir_path, exist_ok=True) for artifact_meta in artifact_metas: download_file_path = os.path.join(download_dir_path, artifact_meta.filename) # Download the artifacts to ``download_file_path``. optuna.artifacts.download_artifact( artifact_store=artifact_store, artifact_id=artifact_meta.artifact_id, file_path=download_file_path, )
- 返回:
试验或研究中的工件元数据列表。每个工件元数据包括
artifact_id、filename、mimetype和encoding。请注意,如果提供了Study,我们返回上传到study的工件信息,而不是研究中所有试验的工件信息。- 参数:
study_or_trial (Trial | FrozenTrial | Study)
storage (BaseStorage | None)
- 返回类型: