如何用PyTorch加载预训练模型进行推理操作?

2026-05-29 00:031阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计244个文字,预计阅读时间需要1分钟。

如何用PyTorch加载预训练模型进行推理操作?

前提:模型参数和结构是分别保存的。

1. 模型参数保存为1。

2.构建模型结构保存为2。

前提: 模型参数和结构是分别保存的

1、 构建模型(# load model graph)

如何用PyTorch加载预训练模型进行推理操作?

model = MODEL()

2、加载模型参数(# load model state_dict)

model.load_state_dict ( { k.replace('module.',''):v for k,v in  torch.load(config.model_path, map_location=config.device).items() } ) model = self.model.to(config.device) * config.device 指定使用哪块GPU或者CPU   *k.replace('module.',''):v 防止torch.DataParallel训练的模型出现加载错误

(解决RuntimeError: module must have its parameters and buffers on device cuda:0 (device_ids[0]) but found one of them on device: cuda:1问题)

3、设置当前阶段为inference(# predict)

model.eval()

以上这篇pytorch 使用加载训练好的模型做inference就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

本文共计244个文字,预计阅读时间需要1分钟。

如何用PyTorch加载预训练模型进行推理操作?

前提:模型参数和结构是分别保存的。

1. 模型参数保存为1。

2.构建模型结构保存为2。

前提: 模型参数和结构是分别保存的

1、 构建模型(# load model graph)

如何用PyTorch加载预训练模型进行推理操作?

model = MODEL()

2、加载模型参数(# load model state_dict)

model.load_state_dict ( { k.replace('module.',''):v for k,v in  torch.load(config.model_path, map_location=config.device).items() } ) model = self.model.to(config.device) * config.device 指定使用哪块GPU或者CPU   *k.replace('module.',''):v 防止torch.DataParallel训练的模型出现加载错误

(解决RuntimeError: module must have its parameters and buffers on device cuda:0 (device_ids[0]) but found one of them on device: cuda:1问题)

3、设置当前阶段为inference(# predict)

model.eval()

以上这篇pytorch 使用加载训练好的模型做inference就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。