Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. require 'nn'
  2. require 'cutorch'
  3. require 'cunn'
  4.  
  5. --[[
  6. -- A simple benchmark comparing fully-connected net times on CPU and GPU.
  7. --
  8. -- We construct a five-layer network with 100-D inputs, 4D outputs, and
  9. -- four hidden layers of 1024 units each with ReLU between layers.
  10. --
  11. -- For each datatype (float, double, cuda) we run 10 forward/backward
  12. -- passes of the network and report the elapsed time. Note the use of
  13. -- cutorch.synchronize to ensure that we are properly handling GPU timing.
  14. --]]
  15.  
  16. local layer_sizes = {100, 1024, 1024, 1024, 1024, 4}
  17. local model = nn.Sequential()
  18. for i = 2, #layer_sizes do
  19. model:add(nn.Linear(layer_sizes[i - 1], layer_sizes[i]))
  20. model:add(nn.ReLU(true))
  21. end
  22. model:float()
  23. local crit = nn.MSECriterion():float()
  24.  
  25. local batch_size = 1000
  26. local in_dim = layer_sizes[1]
  27. local out_dim = layer_sizes[#layer_sizes]
  28.  
  29. local dtypes = {'torch.DoubleTensor', 'torch.FloatTensor', 'torch.CudaTensor'}
  30. local timer = torch.Timer()
  31. for _, dtype in ipairs(dtypes) do
  32. print(string.format('Testing dtype %s', dtype))
  33. model:type(dtype)
  34. crit:type(dtype)
  35. for t = 1, 10 do
  36. local X = torch.randn(batch_size, in_dim):type(dtype)
  37. local y = torch.randn(batch_size, out_dim):type(dtype)
  38. timer:reset()
  39. cutorch.synchronize()
  40. local y_pred = model:forward(X)
  41. local loss = crit:forward(y_pred, y)
  42. local dy_pred = crit:backward(y_pred, y)
  43. model:backward(X, dy_pred)
  44. cutorch.synchronize()
  45. local t = timer:time().real
  46. print(t)
  47. end
  48. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement