The code of ‘Sampler.lua’ in torch7 is there:

require ‘nn’


local Sampler, parent = torch.class(‘nn.Sampler’, ‘nn.Module’)


function Sampler:__init()
parent.__init(self)
self.gradInput = {}
end


function Sampler:updateOutput(input)
self.eps = self.eps or input[1].new()
self.eps:resizeAs(input[1]):copy(torch.randn(input[1]:size()))


self.ouput = self.output or self.output.new()
self.output:resizeAs(input[2]):copy(input[2])
self.output:mul(0.5):exp():cmul(self.eps)



self.output:add(input[1])



return self.output

end


function Sampler:updateGradInput(input, gradOutput)
self.gradInput[1] = self.gradInput[1] or input[1].new()
self.gradInput[1]:resizeAs(gradOutput):copy(gradOutput)
self.gradInput[2] = self.gradInput[2] or input[2].new()
self.gradInput[2]:resizeAs(gradOutput):copy(input[2])

self.gradInput[2]:mul(0.5):exp():mul(0.5):cmul(self.eps)
self.gradInput[2]:cmul(gradOutput)



return self.gradInput

end

the input has 2 elements:(In lua, elements start with 1 instead of 2)
input[1] = (batch_size, size)    stand for mean
input[2] = (batch_size, size)    strand for log_var
for example:
input[1] = (100, 20)
input[2] = (100, 20)
use the mean and log_var sampling. After that get output(100, 20)
It looks like there is no ‘nn.Sampler’ in pytorch.
So how to do it? Thanks~