cleanup_model.lua 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. -- ref: https://github.com/torch/nn/issues/112#issuecomment-64427049
  2. local function zeroDataSize(data)
  3. if type(data) == 'table' then
  4. for i = 1, #data do
  5. data[i] = zeroDataSize(data[i])
  6. end
  7. elseif type(data) == 'userdata' then
  8. data = torch.Tensor():typeAs(data)
  9. end
  10. return data
  11. end
  12. -- Resize the output, gradInput, etc temporary tensors to zero (so that the
  13. -- on disk size is smaller)
  14. local function cleanupModel(node)
  15. if node.output ~= nil then
  16. node.output = zeroDataSize(node.output)
  17. end
  18. if node.gradInput ~= nil then
  19. node.gradInput = zeroDataSize(node.gradInput)
  20. end
  21. if node.finput ~= nil then
  22. node.finput = zeroDataSize(node.finput)
  23. end
  24. if tostring(node) == "nn.LeakyReLU" or tostring(node) == "w2nn.LeakyReLU" then
  25. if node.negative ~= nil then
  26. node.negative = zeroDataSize(node.negative)
  27. end
  28. end
  29. if tostring(node) == "nn.Dropout" then
  30. if node.noise ~= nil then
  31. node.noise = zeroDataSize(node.noise)
  32. end
  33. end
  34. -- Recurse on nodes with 'modules'
  35. if (node.modules ~= nil) then
  36. if (type(node.modules) == 'table') then
  37. for i = 1, #node.modules do
  38. local child = node.modules[i]
  39. cleanupModel(child)
  40. end
  41. end
  42. end
  43. end
  44. function w2nn.cleanup_model(model)
  45. cleanupModel(model)
  46. return model
  47. end