torch学习(四) using GPU

本文详细介绍了如何利用GPU提升深度学习模型的训练效率,包括获取GPU信息、GPU与CPU之间的数据互通以及如何在神经网络中充分利用GPU资源。通过实践示例,展示了如何将模型和数据迁移至GPU,实现高性能的计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、获取GPU信息
require 'cutorch'print(  cutorch.getDeviceProperties(cutorch.getDevice()) )
二、GPU和CPU之间的数据互通
1.构建GPU数据
t1 = torch.CudaTensor(100):fill(0.5)
t2 = torch.CudaTensor(100):fill(1)
t1:add(t2)
2.GPU=>CPU
t1_cpu = t1:float()
3.CPU=>GPU
t1:zero()
t1[{}] = t1_cpu -- copies the data back to the GPU, with no new alloc
t1_new = t1_cpu:cuda() -- allocates a new tensor
三、nn使用GPU
Module参数是Torch的默认类型,如果需要Cuda的module,需要把默认类型修改为CUda。
1.构建基于Cuda的nn
require 'cunn'
-- we define an MLP
mlp = nn.Sequential()
mlp:add(nn.Linear(ninput, 1000))
mlp:add(nn.Tanh())
mlp:add(nn.Linear(10001000))
mlp:add(nn.Tanh())
mlp:add(nn.Linear(10001000))
mlp:add(nn.Tanh())
mlp:add(nn.Linear(1000, noutput))
 
-- and move it to the GPU:
mlp:cuda()
输入数据需要首先转化为cuda
-- input
input = torch.randn(ninput)
 
-- retype and feed to network:
result = mlp:forward( input:cuda() )
 
-- the result is a CudaTensor, if your loss is CPU-based, then you will-- need to bring it back:
result_cpu = result:float()
可以增加Copy层自动进行数据类型转化
-- we put the mlp in a new container:
mlp_auto = nn.Sequential()
mlp_auto:add(nn.Copy('torch.FloatTensor''torch.CudaTensor'))
mlp_auto:add(mlp)
mlp_auto:add(nn.Copy('torch.CudaTensor''torch.FloatTensor'))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值