 
   THCUNN_argCheck(state, input->nDimension == 2 || input->nDimension == 3, 2, input,
                   "2D or 3D (batch mode) tensor expected for input, but got: %s");
-
-  int dimT = input->nDimension == 3 ? 1 : 0;
   THArgCheck(input->size[dimT] >= kW, 2,
              "input sequence smaller than kernel size. Got: %d, Expected: %d",
              input->size[dimT], kW);
+
+  input_w = input->size[dimT];
+  input_n = input->size[dimF];
+  output_w = (input_w - kW) / dW + 1;
+
+  if (gradOutput != NULL) {
+    THCUNN_check_dim_size(state, gradOutput, ndims, dimT, output_w);
+    THCUNN_check_dim_size(state, gradOutput, ndims, dimF, input_n)
+  }
+  if (indices != NULL) {
+    THCUNN_check_dim_size_indices(state, indices, ndims, dimT, output_w);
+    THCUNN_check_dim_size_indices(state, indices, ndims, dimF, input_n);
+  }
 }
 
 void THNN_(TemporalMaxPooling_updateOutput)(

  THCIndex_t *indices_data;

  THCUNN_assertSameGPU(state, 3, input, output, indices);
  THNN_(TemporalMaxPooling_shapeCheck)(state, input, kW, dW);
  if (input->nDimension == 3)
  {
    dimT = 1;

  THCIndex_t *indices_data;

  THCUNN_assertSameGPU(state, 4, input, gradOutput, gradInput, indices);
  THNN_(TemporalMaxPooling_shapeCheck)(state, input, gradOutput, indices, kW, dW);
  THCTensor_(resizeAs)(state, gradInput, input);
  THCTensor_(zero)(state, gradInput);

