2025年3月28日 星期五 甲辰(龙)年 月廿七 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > 人工智能

pytorch的代码 CPU改为GPU

时间:01-17来源:作者:点击数:32

pytorch的代码 CPU改为GPU

找一下几个地方改动

  • 网络模型
  • 数据(输入、标记)
  • 损失函数
  • cuda()

一、网络模型

  • mymodule = MyModule()
  • mymodule = mymodule.cuda()

二、数据(输入、标记)

  • images,targets = data
  • images = images.cuda()
  • targets = targets.cuda() #训练数据和测试数据集都要进行此操作

三、损失函数

  • # 损失函数
  • loss = nn.CrossEntropyLoss()
  • loss = loss.cuda()

上面的写法,在无GPU时会报错。更好的写法是有GPU用GPU,无GPU用CPU

  • mymodule = MyModule()
  • if torch.cuda.is_available():
  • mymodule = mymodule.cuda()
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门