You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中正确将CatBoost模型保存至Google Cloud Storage

解决CatBoost模型保存到Google Cloud Storage的报错问题

你遇到的报错是因为CatBoost的save_model方法本身不直接支持Google Cloud Storage(GS)的路径格式,可通过以下两种方式处理:

方法一:先存本地再上传到GS

先把模型保存到本地临时文件,再用GCP的存储SDK上传到桶中:

  1. 安装依赖:
pip install google-cloud-storage
  1. 修改代码:
from google.cloud import storage

# 先保存到本地文件
bst_model.save_model("bst_model.cbm", format="cbm")

# 初始化GS客户端并上传
client = storage.Client()
bucket = client.get_bucket("gchidam-tnt-input-data")  # 替换为你的桶名
blob = bucket.blob("bst_model.cbm")  # 桶内的目标文件名
blob.upload_from_filename("bst_model.cbm")

方法二:用fsspec让CatBoost直接支持GS路径

如果你的CatBoost版本较新(建议v1.0及以上),可以借助fsspec库直接识别GS路径:

  1. 安装依赖:
pip install fsspec gcsfs
  1. 直接使用原代码(建议给文件名加上.cbm后缀更规范):
bst_model.save_model(f"gs://gchidam-tnt-input-data/bst_model.cbm",
       format="cbm",
       export_parameters=None,
       pool=None)

关键注意事项

  • 认证配置:本地开发需设置GOOGLE_APPLICATION_CREDENTIALS环境变量,指向你的GCP服务账号密钥文件;如果在GCP托管服务(如AI平台、云函数)中运行,会自动获取服务账号权限。
  • 桶权限检查:确认你的账号/服务账号拥有目标GS桶的写入权限(需storage.objects.create权限)。

内容的提问来源于stack exchange,提问作者procrastinationmonkey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 09:27:16