diff options
author | Clement Farabet <clement.farabet@gmail.com> | 2011-09-13 21:49:05 +0400 |
---|---|---|
committer | Clement Farabet <clement.farabet@gmail.com> | 2011-09-13 21:49:05 +0400 |
commit | 626281a4e402ab797e97c713f9365ba2d0f11a74 (patch) | |
tree | fb247d0d29f91024d05699dd7de68ae6af4edf94 | |
parent | fcfea5cfbb171841082accd6df137910ff29fbbb (diff) |
Introducing: SpatialClassifier, to replace all Spatial modules.
The goal is simple: instead of writing tons of Spatial*** modules,
we make sure all modules (Linear,SoftMax,...) can handle batches,
and use SpatialClassifier to replicate these modules spatially.
-rw-r--r-- | SpatialLogSoftMax.lua | 17 | ||||
-rw-r--r-- | generic/SpatialLogSoftMax.c | 116 | ||||
-rw-r--r-- | init.c | 5 | ||||
-rw-r--r-- | init.lua | 2 | ||||
-rw-r--r-- | nnx-1.0-1.rockspec | 2 |
5 files changed, 2 insertions, 140 deletions
diff --git a/SpatialLogSoftMax.lua b/SpatialLogSoftMax.lua deleted file mode 100644 index d145caa..0000000 --- a/SpatialLogSoftMax.lua +++ /dev/null @@ -1,17 +0,0 @@ -local SpatialLogSoftMax, parent = torch.class('nn.SpatialLogSoftMax', 'nn.Module') - -function SpatialLogSoftMax:__init() - parent.__init(self) -end - -function SpatialLogSoftMax:forward(input) - self.output:resizeAs(input) - input.nn.SpatialLogSoftMax_forward(self, input) - return self.output -end - -function SpatialLogSoftMax:backward(input, gradOutput) - self.gradInput:resizeAs(input) - input.nn.SpatialLogSoftMax_backward(self, input, gradOutput) - return self.gradInput -end diff --git a/generic/SpatialLogSoftMax.c b/generic/SpatialLogSoftMax.c deleted file mode 100644 index d0ec07a..0000000 --- a/generic/SpatialLogSoftMax.c +++ /dev/null @@ -1,116 +0,0 @@ -#ifndef TH_GENERIC_FILE -#define TH_GENERIC_FILE "generic/SpatialLogSoftMax.c" -#else - -static int nn_(SpatialLogSoftMax_forward)(lua_State *L) -{ - // get all params - THTensor *input = luaT_checkudata(L, 2, torch_(Tensor_id)); - THTensor *output = luaT_getfieldcheckudata(L, 1, "output", torch_(Tensor_id)); - - // dims - int width = input->size[2]; - int height = input->size[1]; - - // select planes - THTensor *input_row = THTensor_(new)(); - THTensor *input_point = THTensor_(new)(); - THTensor *output_row = THTensor_(new)(); - THTensor *output_point = THTensor_(new)(); - - // process the whole plane - int x,y; - for (y=0; y<height; y++) { - THTensor_(select)(input_row, input, 1, y); - THTensor_(select)(output_row, output, 1, y); - for (x=0; x<width; x++) { - THTensor_(select)(input_point, input_row, 1, x); - THTensor_(select)(output_point, output_row, 1, x); - - real sum = THLogZero; - - TH_TENSOR_APPLY2(real, output_point, real, input_point, \ - real z = *input_point_data; \ - *output_point_data = z; \ - sum = THLogAdd(sum, z);) - - THTensor_(add)(output_point, -sum); - } - } - - // cleanup - THTensor_(free)(input_row); - THTensor_(free)(input_point); - THTensor_(free)(output_row); - THTensor_(free)(output_point); - return 1; -} - -static int nn_(SpatialLogSoftMax_backward)(lua_State *L) -{ - // get all params - THTensor *input = luaT_checkudata(L, 2, torch_(Tensor_id)); - THTensor *gradOutput = luaT_checkudata(L, 3, torch_(Tensor_id)); - THTensor *gradInput = luaT_getfieldcheckudata(L, 1, "gradInput", torch_(Tensor_id)); - THTensor *output = luaT_getfieldcheckudata(L, 1, "output", torch_(Tensor_id)); - - // dims - int width = input->size[2]; - int height = input->size[1]; - - // zero gradInput - THTensor_(zero)(gradInput); - - // select planes - THTensor *gradOutput_row = THTensor_(new)(); - THTensor *gradOutput_point = THTensor_(new)(); - THTensor *gradInput_row = THTensor_(new)(); - THTensor *gradInput_point = THTensor_(new)(); - THTensor *output_row = THTensor_(new)(); - THTensor *output_point = THTensor_(new)(); - - // compute gradients for each point - int x,y; - for (y=0; y<height; y++) { - THTensor_(select)(gradInput_row, gradInput, 1, y); - THTensor_(select)(gradOutput_row, gradOutput, 1, y); - THTensor_(select)(output_row, output, 1, y); - for (x=0; x<width; x++) { - THTensor_(select)(gradInput_point, gradInput_row, 1, x); - THTensor_(select)(gradOutput_point, gradOutput_row, 1, x); - THTensor_(select)(output_point, output_row, 1, x); - - real sum = THTensor_(sum)(gradOutput_point); - - TH_TENSOR_APPLY3(real, gradInput_point, \ - real, gradOutput_point, \ - real, output_point, \ - *gradInput_point_data = *gradOutput_point_data - exp(*output_point_data)*sum;); - } - } - - // cleanup - THTensor_(free)(gradInput_row); - THTensor_(free)(gradInput_point); - THTensor_(free)(gradOutput_row); - THTensor_(free)(gradOutput_point); - THTensor_(free)(output_row); - THTensor_(free)(output_point); - - return 1; -} - -static const struct luaL_Reg nn_(SpatialLogSoftMax__) [] = { - {"SpatialLogSoftMax_forward", nn_(SpatialLogSoftMax_forward)}, - {"SpatialLogSoftMax_backward", nn_(SpatialLogSoftMax_backward)}, - {NULL, NULL} -}; - -static void nn_(SpatialLogSoftMax_init)(lua_State *L) -{ - luaT_pushmetaclass(L, torch_(Tensor_id)); - luaT_registeratname(L, nn_(SpatialLogSoftMax__), "nn"); - lua_pop(L,1); -} - -#endif @@ -21,9 +21,6 @@ static const void* torch_DoubleTensor_id = NULL; #include "generic/SpatialMaxPooling.c" #include "THGenerateFloatTypes.h" -#include "generic/SpatialLogSoftMax.c" -#include "THGenerateFloatTypes.h" - #include "generic/SpatialUpSampling.c" #include "THGenerateFloatTypes.h" @@ -60,7 +57,6 @@ DLL_EXPORT int luaopen_libnnx(lua_State *L) nn_FloatHardShrink_init(L); nn_FloatAbs_init(L); nn_FloatThreshold_init(L); - nn_FloatSpatialLogSoftMax_init(L); nn_FloatSpatialMaxPooling_init(L); nn_FloatSpatialUpSampling_init(L); nn_FloatSpatialReSampling_init(L); @@ -75,7 +71,6 @@ DLL_EXPORT int luaopen_libnnx(lua_State *L) nn_DoubleHardShrink_init(L); nn_DoubleAbs_init(L); nn_DoubleThreshold_init(L); - nn_DoubleSpatialLogSoftMax_init(L); nn_DoubleSpatialMaxPooling_init(L); nn_DoubleSpatialUpSampling_init(L); nn_DoubleSpatialReSampling_init(L); @@ -78,7 +78,7 @@ end -- spatial (images) operators: torch.include('nnx', 'SpatialLinear.lua') -torch.include('nnx', 'SpatialLogSoftMax.lua') +torch.include('nnx', 'SpatialClassifier.lua') torch.include('nnx', 'SpatialMaxPooling.lua') torch.include('nnx', 'SpatialPadding.lua') torch.include('nnx', 'SpatialNormalization.lua') diff --git a/nnx-1.0-1.rockspec b/nnx-1.0-1.rockspec index a5ee7f6..1b90f21 100644 --- a/nnx-1.0-1.rockspec +++ b/nnx-1.0-1.rockspec @@ -72,7 +72,7 @@ build = { install_files(/lua/nnx Sqrt.lua) install_files(/lua/nnx Threshold.lua) install_files(/lua/nnx OmpModule.lua) - install_files(/lua/nnx SpatialLogSoftMax.lua) + install_files(/lua/nnx SpatialClassifier.lua) install_files(/lua/nnx SpatialMaxPooling.lua) install_files(/lua/nnx SpatialLinear.lua) install_files(/lua/nnx SpatialPadding.lua) |