You need to enable JavaScript to run this app.
文档中心
对象存储

对象存储

复制全文
下载 pdf
使用TOS PyTorch Connector
TosCheckpoint
复制全文
下载 pdf
TosCheckpoint
本文为您介绍如何通过 TosCheckpoint 直接从 TOS 中读写检查点。​
前提条件​
已安装并配置 TOS PyTorch Connector。具体操作,请参见安装 TOS PyTorch Connector和配置TOS PyTorch Connector。​
TosObjectWriter​
TosCheckpoint 的 TosObjectWriter() 方法可以把数据写入检查点。​
参数描述​
使用 TosCheckpoint 时需要进行相应配置,具体配置项说明请参见2. 传入凭证。​
代码示例​
以下示例展示了如何使用 PyTorch 通过 TosCheckpoint 写入检查点,region、endpoint、ak/sk 等参数需要替换为您所需参数。​
​
import torch​
import torchvision​
from tostorchconnector import TosCheckpoint, CredentialProvider​
​
checkpoint_uri = 'tos://bucket/key'​
region = 'cn-beijing'​
endpoint = 'http://tos-cn-beijing.ivolces.com'​
ak = '<your-access-key>'​
sk = '<your-secret-key>'​
​
with TosCheckpoint(region, endpoint, cred=CredentialProvider(ak, sk)) as checkpoint:​
model = torchvision.models.resnet18()​
with checkpoint.writer(checkpoint_uri) as writer:​
torch.save(model, writer)​
​
TosObjectReader​
TosCheckpoint 的 TosObjectReader() 方法可以从 TOS 中读取检查点。​
参数描述​
使用 TosCheckpoint 时需要进行相应配置,具体配置项说明请参见2. 传入凭证。​
代码示例​
以下示例展示了如何使用 PyTorch 通过 TosCheckpoint 从 TOS 中读取检查点,region、endpoint、ak/sk 等参数需要替换为您所需参数。​
​
import torch​
from tostorchconnector import TosCheckpoint, CredentialProvider​
​
checkpoint_uri = 'tos://bucket/key'​
region = 'cn-beijing'​
endpoint = 'http://tos-cn-beijing.ivolces.com'​
ak = '<your-access-key>'​
sk = '<your-secret-key>'​
​
with TosCheckpoint(region, endpoint, cred=CredentialProvider(ak, sk)) as checkpoint:​
with checkpoint.reader(checkpoint_uri) as reader:​
model = torch.load(reader)​
print(model)​
​
最近更新时间:2026.02.02 14:44:15
这个页面对您有帮助吗?
有用
有用
无用
无用