aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/torch/nn/TemporalSubSampling.lua
blob: e9287d63dfc1888a0c92fe486c106f2e2a0c9f47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
local TemporalSubSampling, parent = torch.class('nn.TemporalSubSampling', 'nn.Module')

function TemporalSubSampling:__init(inputFrameSize, kW, dW)
   parent.__init(self)

   dW = dW or 1

   self.inputFrameSize = inputFrameSize
   self.kW = kW
   self.dW = dW

   self.weight = torch.Tensor(inputFrameSize)
   self.bias = torch.Tensor(inputFrameSize)
   self.gradWeight = torch.Tensor(inputFrameSize)
   self.gradBias = torch.Tensor(inputFrameSize)

   self:reset()
end

function TemporalSubSampling:reset(stdv)
   if stdv then
      stdv = stdv * math.sqrt(3)
   else
      stdv = 1/math.sqrt(self.kW)
   end
   if nn.oldSeed then
      self.weight:apply(function()
         return torch.uniform(-stdv, stdv)
      end)
      self.bias:apply(function()
         return torch.uniform(-stdv, stdv)
      end)
   else
      self.weight:uniform(-stdv, stdv)
      self.bias:uniform(-stdv, stdv)
   end
end

function TemporalSubSampling:updateOutput(input)
    input.THNN.TemporalSubSampling_updateOutput(
	input:cdata(), self.output:cdata(),
	self.weight:cdata(), self.bias:cdata(),
	self.kW, self.dW, self.inputFrameSize
    )
   return self.output
end

function TemporalSubSampling:updateGradInput(input, gradOutput)
    if self.gradInput then
	input.THNN.TemporalSubSampling_updateGradInput(
	    input:cdata(), gradOutput:cdata(), self.gradInput:cdata(),
	    self.weight:cdata(), self.kW, self.dW
	)
	return self.gradInput
   end
end

function TemporalSubSampling:accGradParameters(input, gradOutput, scale)
    scale = scale or 1
    input.THNN.TemporalSubSampling_accGradParameters(
	input:cdata(), gradOutput:cdata(), self.gradWeight:cdata(),
	self.gradBias:cdata(), self.kW, self.dW, scale
    )
end