1. 程式人生 > >torch學習(四) using GPU

torch學習(四) using GPU

一、獲取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'))