From dcaf75da2001c98043ecd27f8b12413dd27e75af Mon Sep 17 00:00:00 2001 From: Clement Farabet Date: Sat, 27 Oct 2012 15:11:06 -0400 Subject: Faster reset for 'nn' modules. --- Euclidean.lua | 7 +------ Linear.lua | 11 ++--------- LookupTable.lua | 4 +--- SparseLinear.lua | 11 ++--------- SpatialConvolution.lua | 8 ++------ SpatialConvolutionMM.lua | 8 ++------ SpatialConvolutionMap.lua | 16 ++++++---------- SpatialSubSampling.lua | 8 ++------ TemporalConvolution.lua | 8 ++------ TemporalSubSampling.lua | 10 ++-------- VolumetricConvolution.lua | 8 ++------ WeightedEuclidean.lua | 8 +------- 12 files changed, 25 insertions(+), 82 deletions(-) diff --git a/Euclidean.lua b/Euclidean.lua index 808b7ab..ca0dc33 100644 --- a/Euclidean.lua +++ b/Euclidean.lua @@ -20,12 +20,7 @@ function Euclidean:reset(stdv) else stdv = 1./math.sqrt(self.weight:size(1)) end - - for i=1,self.weight:size(2) do - self.weight:select(2, i):apply(function() - return torch.uniform(-stdv, stdv) - end) - end + self.weight:uniform(-stdv, stdv) end function Euclidean:updateOutput(input) diff --git a/Linear.lua b/Linear.lua index 953af78..756c9a4 100644 --- a/Linear.lua +++ b/Linear.lua @@ -17,15 +17,8 @@ function Linear:reset(stdv) else stdv = 1./math.sqrt(self.weight:size(2)) end - - -- we do this so the initialization is exactly - -- the same than in previous torch versions - for i=1,self.weight:size(1) do - self.weight:select(1, i):apply(function() - return torch.uniform(-stdv, stdv) - end) - self.bias[i] = torch.uniform(-stdv, stdv) - end + self.weight:uniform(-stdv, stdv) + self.bias:uniform(-stdv, stdv) end function Linear:updateOutput(input) diff --git a/LookupTable.lua b/LookupTable.lua index 7dfda7a..2594340 100644 --- a/LookupTable.lua +++ b/LookupTable.lua @@ -29,9 +29,7 @@ end function LookupTable:reset(stdv) stdv = stdv or 1 - self.weight:apply(function() - return torch.normal(0, stdv) - end) + self.weight:normal(0, stdv) end function LookupTable:updateOutput(input) diff --git a/SparseLinear.lua b/SparseLinear.lua index ec8845e..ef594ee 100644 --- a/SparseLinear.lua +++ b/SparseLinear.lua @@ -22,15 +22,8 @@ function SparseLinear:reset(stdv) else stdv = 1./math.sqrt(self.weight:size(1)) end - - -- we do this so the initialization is exactly - -- the same than in previous torch versions - for i=1,self.weight:size(1) do - self.weight:select(1, i):apply(function() - return torch.uniform(-stdv, stdv) - end) - self.bias[i] = torch.uniform(-stdv, stdv) * 0.000001 - end + self.weight:uniform(-stdv, stdv) + self.bias:uniform(-stdv, stdv):mul(0.000001) end function SparseLinear:updateOutput(input) diff --git a/SpatialConvolution.lua b/SpatialConvolution.lua index 38d2737..17b837d 100644 --- a/SpatialConvolution.lua +++ b/SpatialConvolution.lua @@ -27,12 +27,8 @@ function SpatialConvolution:reset(stdv) else stdv = 1/math.sqrt(self.kW*self.kH*self.nInputPlane) end - self.weight:apply(function() - return torch.uniform(-stdv, stdv) - end) - self.bias:apply(function() - return torch.uniform(-stdv, stdv) - end) + self.weight:uniform(-stdv, stdv) + self.bias:uniform(-stdv, stdv) end function SpatialConvolution:updateOutput(input) diff --git a/SpatialConvolutionMM.lua b/SpatialConvolutionMM.lua index aa358e5..7fff7b9 100644 --- a/SpatialConvolutionMM.lua +++ b/SpatialConvolutionMM.lua @@ -25,12 +25,8 @@ function SpatialConvolutionMM:reset(stdv) else stdv = 1/math.sqrt(self.kW*self.kH*self.nInputPlane) end - self.weight:apply(function() - return torch.uniform(-stdv, stdv) - end) - self.bias:apply(function() - return torch.uniform(-stdv, stdv) - end) + self.weight:uniform(-stdv, stdv) + self.bias:uniform(-stdv, stdv) end function SpatialConvolutionMM:updateOutput(input) diff --git a/SpatialConvolutionMap.lua b/SpatialConvolutionMap.lua index 4f6609e..5f8f56d 100644 --- a/SpatialConvolutionMap.lua +++ b/SpatialConvolutionMap.lua @@ -110,22 +110,18 @@ end function SpatialConvolutionMap:reset(stdv) if stdv then stdv = stdv * math.sqrt(3) - self.weight:apply(function() - return torch.uniform(-stdv, stdv) - end) - self.bias:apply(function() - return torch.uniform(-stdv, stdv) - end) + self.weight:uniform(-stdv, stdv) + self.bias:uniform(-stdv, stdv) else local ninp = torch.Tensor(self.nOutputPlane):zero() for i=1,self.connTable:size(1) do ninp[self.connTable[i][2]] = ninp[self.connTable[i][2]]+1 end for k=1,self.connTable:size(1) do - stdv = 1/math.sqrt(self.kW*self.kH*ninp[self.connTable[k][2]]) - self.weight:select(1,k):apply(function() return torch.uniform(-stdv,stdv) end) + stdv = 1/math.sqrt(self.kW*self.kH*ninp[self.connTable[k][2]]) + self.weight:select(1,k):uniform(-stdv,stdv) end for k=1,self.bias:size(1) do - stdv = 1/math.sqrt(self.kW*self.kH*ninp[k]) - self.bias[k] = torch.uniform(-stdv,stdv) + stdv = 1/math.sqrt(self.kW*self.kH*ninp[k]) + self.bias[k] = torch.uniform(-stdv,stdv) end end end diff --git a/SpatialSubSampling.lua b/SpatialSubSampling.lua index 48b32b9..b4c01f1 100644 --- a/SpatialSubSampling.lua +++ b/SpatialSubSampling.lua @@ -26,12 +26,8 @@ function SpatialSubSampling:reset(stdv) else stdv = 1/math.sqrt(self.kW*self.kH) end - self.weight:apply(function() - return torch.uniform(-stdv, stdv) - end) - self.bias:apply(function() - return torch.uniform(-stdv, stdv) - end) + self.weight:uniform(-stdv, stdv) + self.bias:uniform(-stdv, stdv) end function SpatialSubSampling:updateOutput(input) diff --git a/TemporalConvolution.lua b/TemporalConvolution.lua index a3aaa7f..dcd1628 100644 --- a/TemporalConvolution.lua +++ b/TemporalConvolution.lua @@ -24,12 +24,8 @@ function TemporalConvolution:reset(stdv) else stdv = 1/math.sqrt(self.kW*self.inputFrameSize) end - self.weight:apply(function() - return torch.uniform(-stdv, stdv) - end) - self.bias:apply(function() - return torch.uniform(-stdv, stdv) - end) + self.weight:uniform(-stdv, stdv) + self.bias:uniform(-stdv, stdv) end function TemporalConvolution:updateOutput(input) diff --git a/TemporalSubSampling.lua b/TemporalSubSampling.lua index 3d06f6e..f7c0efb 100644 --- a/TemporalSubSampling.lua +++ b/TemporalSubSampling.lua @@ -23,14 +23,8 @@ function TemporalSubSampling:reset(stdv) else stdv = 1/math.sqrt(self.kW) end - - self.weight:apply(function() - return torch.uniform(-stdv, stdv) - end) - - self.bias:apply(function() - return torch.uniform(-stdv, stdv) - end) + self.weight:uniform(-stdv, stdv) + self.bias:uniform(-stdv, stdv) end function TemporalSubSampling:updateOutput(input) diff --git a/VolumetricConvolution.lua b/VolumetricConvolution.lua index 4262199..80b8ec7 100644 --- a/VolumetricConvolution.lua +++ b/VolumetricConvolution.lua @@ -30,12 +30,8 @@ function VolumetricConvolution:reset(stdv) else stdv = 1/math.sqrt(self.kT*self.kW*self.kH*self.nInputPlane) end - self.weight:apply(function() - return torch.uniform(-stdv, stdv) - end) - self.bias:apply(function() - return torch.uniform(-stdv, stdv) - end) + self.weight:uniform(-stdv, stdv) + self.bias:uniform(-stdv, stdv) end function VolumetricConvolution:updateOutput(input) diff --git a/WeightedEuclidean.lua b/WeightedEuclidean.lua index 5337eec..f8da8f5 100644 --- a/WeightedEuclidean.lua +++ b/WeightedEuclidean.lua @@ -30,13 +30,7 @@ function WeightedEuclidean:reset(stdv) else stdv = 1./math.sqrt(self.templates:size(1)) end - - for i=1,self.templates:size(2) do - self.templates:select(2, i):apply(function() - return torch.uniform(-stdv, stdv) - end) - end - + self.templates:uniform(-stdv, stdv) self.diagCov:fill(1) end -- cgit v1.2.3