Пример #1
0
static void THNN_(SpatialConvolutionMM_updateGradInput_frame)(
          THTensor *gradInput,
          THTensor *gradOutput,
          THTensor *weight,
          THTensor *fgradInput,
          int kW,
          int kH,
          int dW,
          int dH,
          int padW,
          int padH)
{
  THTensor *gradOutput2d = THTensor_(newWithStorage2d)(gradOutput->storage, gradOutput->storageOffset,
                                                       gradOutput->size[0], -1,
                                                       gradOutput->size[1]*gradOutput->size[2], -1);
  THTensor_(addmm)(fgradInput, 0, fgradInput, 1, weight, gradOutput2d);
  THTensor_(free)(gradOutput2d);

  THTensor_(zero)(gradInput);

  THNN_(unfolded_acc)(fgradInput, gradInput, kW, kH, dW, dH, padW, padH, gradInput->size[0], gradInput->size[2], gradInput->size[1], gradOutput->size[2], gradOutput->size[1]);
}
Пример #2
0
/* expects a lua table as the 6th function argument */
static int cr_(AG_tbl_cmd_adagrad_step)(lua_State *L) {
    THTensor *x = luaT_checkudata(L, 1, torch_Tensor);
    THTensor *g = luaT_checkudata(L, 2, torch_Tensor);
    THTensor *var = luaT_checkudata(L, 3, torch_Tensor);
    double eta = luaL_checknumber(L,4);
    double lamb = luaL_checknumber(L,5);

    int h, d, feat_idx;
    if (THTensor_(nDimension)(x) > 1) {
        h = THTensor_(size)(x,0); // height
        d = THTensor_(size)(x,1); // width
    } else {
        h = 1;
        d = THTensor_(nElement)(x);
    }
    
    // if just one row, might as well use the table; otherwise we want to parallelize,
    // so we copy to an array that can easily be shared between threads
    if (h == 1) {
       tbl_cmd_adagrad_step(THTensor_(data)(x),THTensor_(data)(g),THTensor_(data)(var),eta,lamb,h,d,L,6);
    } else { // copy the feature idxs from the lua table
        feat_idx = 0;
        lua_pushnil(L); // push nil key (so we can pop something with lua_next)
        while (lua_next(L,6) != 0){
            // the above pushes the key to index -2 and val to index -1; we only care about the key
            // the following should convert to a signed integral type
            int feat = lua_tointeger(L,-2); // leave 1-based indexing for now.
            if (feat_idx >= MAX_NZ){
               printf("ERROR: expecting at most %d sparse features; about to seg-fault", MAX_NZ);
            }            
            global_nz[feat_idx] = feat;
            lua_pop(L,1); // removes val; key still on top
            feat_idx += 1;
        }       
        sp_cmd_adagrad_step(THTensor_(data)(x),THTensor_(data)(g),THTensor_(data)(var),global_nz,eta,lamb,h,d,feat_idx);         
    }        

    return 0;
}
Пример #3
0
static libsox_(AudioData) libsox_(read_audio_file)(const char *file_name)
{
  // Create sox objects and read into int32_t buffer
  sox_format_t *fd;
  fd = sox_open_read(file_name, NULL, NULL, NULL);
  if (fd == NULL)
    abort_("[read_audio_file] Failure to read file");
  
  int nchannels = fd->signal.channels;
  long buffer_size = fd->signal.length;
  double rate = fd->signal.rate;
  int32_t *buffer = (int32_t *)malloc(sizeof(int32_t) * buffer_size);
  size_t samples_read = sox_read(fd, buffer, buffer_size);
  if (samples_read == 0)
    abort_("[read_audio_file] Empty file or read failed in sox_read");
  // alloc tensor 
  THTensor *tensor = THTensor_(newWithSize2d)(nchannels, samples_read / nchannels );
  tensor = THTensor_(newContiguous)(tensor);
  real *tensor_data = THTensor_(data)(tensor);
  // convert audio to dest tensor 
  int x,k;
  for (k=0; k<nchannels; k++) {
    for (x=0; x<samples_read/nchannels; x++) {
      *tensor_data++ = (real)buffer[x*nchannels+k];
    }
  }
  // free buffer and sox structures
  sox_close(fd);
  free(buffer);
  THTensor_(free)(tensor);

  libsox_(AudioData) ret;
  ret.t = tensor;
  ret.nChannels = nchannels;
  ret.bufferSize = buffer_size;
  ret.rate = rate;
  ret.lenSecs = (samples_read / nchannels) / rate;
  return ret;
}
Пример #4
0
static int torch_(Tensor_resize)(lua_State *L)
{
  THTensor *tensor = luaT_checkudata(L, 1, torch_(Tensor_id));
  THLongStorage *size;

  torch_(Tensor_c_readSize)(L, 2, &size);

  THTensor_(resize)(tensor, size);
  THLongStorage_free(size);

  lua_settop(L, 1);
  return 1;
}
Пример #5
0
static void THNN_(VolumetricConvolutionMM_updateOutput_frame)(
  THTensor *input, THTensor *output, THTensor *weight, THTensor *bias, THTensor *finput,
  int kT, int kW, int kH,
  int dT, int dW, int dH,
  int pT,int pW, int pH,
  long nInputPlane, long inputDepth, long inputWidth, long inputHeight,
  long nOutputPlane, long outputDepth, long outputWidth, long outputHeight)
{
  long i;
  THTensor *output2d;

  THNN_(unfolded_copy_vol)(
    finput, input,
    kT, kW, kH,
    dT, dW, dH,
    pT, pW, pH,
    nInputPlane,
    inputDepth, inputWidth, inputHeight,
    outputDepth, outputWidth, outputHeight
  );

  output2d = THTensor_(newWithStorage2d)(
    output->storage, output->storageOffset, nOutputPlane, -1,
    outputDepth*outputHeight*outputWidth, -1
  );

  for (i = 0; i < nOutputPlane; i++)
  {
    THVector_(fill)(
      output->storage->data+output->storageOffset+output->stride[0]*i,
      THTensor_(get1d)(bias, i),
      outputDepth*outputHeight*outputWidth
    );
  }

  THTensor_(addmm)(output2d, 1, output2d, 1, weight, finput);

  THTensor_(free)(output2d);
}
Пример #6
0
void THNN_(SoftPlus_updateOutput)(
          THNNState *state,
          THTensor *input,
          THTensor *output,
          real beta,
          real threshold)
{
  THTensor_(resizeAs)(output, input);

  // f(x) = 1/beta * log(1 + exp(beta * x))
  TH_TENSOR_APPLY2(real, output, real, input,               \
    *output_data = (*input_data * beta) > threshold ? *input_data : THLog1p(exp(*input_data * beta)) / beta;
  );
Пример #7
0
static void THNN_(unfolded_acc_row)(
	THTensor *finput,
	THTensor *input,
	int kW,
	int dW,
	int padW,
	int64_t inputFrameSize,
	int64_t nInputFrame,
	int64_t nOutputFrame) {

	int64_t c;
	real *input_data = THTensor_(data)(input);
	real *finput_data = THTensor_(data)(finput);

// #pragma omp parallel for private(c)
	for (c = 0; c < inputFrameSize; c++) {
		int64_t kw, x;
		int64_t ix = 0;

		for (kw = 0; kw < kW; kw++) {
			real *src = finput_data
			            + c * (kW * nOutputFrame)
			            + kw * (nOutputFrame);
			real *dst = input_data + c * (nInputFrame);

			ix = (size_t)(kw);
			if (dW == 1) {
			  real *dst_slice = dst + (size_t)(ix);
			  THVector_(cadd)(dst_slice, dst_slice, src, 1, nOutputFrame);
			} else {
				for (x = 0; x < nOutputFrame; x++) {
				  real *dst_slice = dst + (size_t)(ix + x * dW);
				  THVector_(cadd)(dst_slice, dst_slice,
						  src + (size_t)(x), 1, 1);
				}
			}
		}
	}
}
Пример #8
0
int THTensor_(copyTransposeValid)(THTensor *tensor, THTensor *src) {
  const int MIN_SZ = 60 * 60;
  return THTensor_(isContiguous)(tensor) &&
         THTensor_(nDimension)(src) == 2 &&
         THTensor_(stride)(src, 0) == 1 &&
         THTensor_(stride)(src, 1) == THTensor_(size)(src, 0) &&
         THTensor_(nElement)(tensor) >= MIN_SZ;
}
Пример #9
0
// special case copy where tensor is contiguous and src is a transposed matrix
// This can be generalized to most copies, but it's tricker
void THTensor_(copyTranspose)(THTensor *tensor, THTensor *src) {
  #define MIN(x, y) (((x) < (y)) ? (x) : (y))
  #define MAX(x, y) (((x) > (y)) ? (x) : (y))

#ifdef TH_REAL_IS_BYTE
  const int BLOCK_SZ = 120;
#else
  const int BLOCK_SZ = 60;
#endif

  THTensor *buf = THTensor_(newWithSize2d)(BLOCK_SZ, BLOCK_SZ);
  real *sp = THTensor_(data)(src);
  real *rp = THTensor_(data)(tensor);
  real *bp = THTensor_(data)(buf);


  int64_t NR = THTensor_(size)(src, 0);
  int64_t NC = THTensor_(size)(src, 1);
  for (int64_t R = 0; R < NR; R += BLOCK_SZ) {
    for (int64_t C = 0; C < NC; C += BLOCK_SZ) {
      real *spo = sp + R + C * NR;
      real *rpo = rp + C + R * NC;

      int nr = MIN(NR - R, BLOCK_SZ);
      int nc = MIN(NC - C, BLOCK_SZ);

      // 1. copy columns from src to buf
      for (int c = 0; c < nc; c++) {
        memcpy(bp + c * BLOCK_SZ, spo + c * NR, nr * sizeof(real));
      }

      // 2. transpose buf in place
      int rc_max = MAX(nr, nc);
      int rc_min = MIN(nr, nc);
      for (int r = 0; r < rc_max; r++) {
        int end = MIN(r, rc_min);
        for (int c = 0; c < end; c++) {
          real tmp = bp[r + BLOCK_SZ * c];
          bp[r + BLOCK_SZ * c] = bp[r * BLOCK_SZ + c];
          bp[r * BLOCK_SZ + c] = tmp;
        }
      }

      // 3. copy rows from buf to dst
      for (int r = 0; r < nr; r++) {
        memcpy(rpo + r * NC, bp + r * BLOCK_SZ, nc * sizeof(real));
      }
    }
  }
  THTensor_(free)(buf);
  #undef MIN
  #undef MAX
}
Пример #10
0
TH_API void THLab_(gesv)(THTensor *a_, THTensor *b_)
{
  int n, nrhs, lda, ldb, info;
  THIntTensor *ipiv;
  THTensor *A, *B;
  
  THArgCheck(a_->nDimension == 2, 2, "A should be 2 dimensional");
  THArgCheck(a_->size[0] == a_->size[1], 2, "A should be symmetric");

  n = (int)a_->size[1];
  lda = n;
  ldb = n;
  if (b_->nDimension == 1)
  {
    nrhs = 1;
    THArgCheck(n == b_->size[0], 1, "size incompatible A,b");
  }
  else
  {
    nrhs = b_->size[0];
    THArgCheck(n == b_->size[1], 1, "size incompatible A,b");
  }

  A = THTensor_(newContiguous)(a_);
  B = THTensor_(newContiguous)(b_);
  ipiv = THIntTensor_newWithSize1d((long)n);
  THLapack_(gesv)(n, nrhs, 
		  THTensor_(data)(A), lda, THIntTensor_data(ipiv),
		  THTensor_(data)(B), ldb, &info);

  if(!THTensor_(isContiguous)(b_))
  {
    THTensor_(copy)(b_,B);
  }

  if (info < 0)
  {
    THError("Lapack gesv : Argument %d : illegal value", -info);
  }
  else if (info > 0)
  {
    THError("Lapack gesv : U(%d,%d) is zero, singular U.", info,info);
  }

  THIntTensor_free(ipiv);
  THTensor_(free)(A);
  THTensor_(free)(B);
}
Пример #11
0
static void THNN_(SpatialConvolutionMM_updateOutput_frame)(
          THTensor *input,
          THTensor *output,
          THTensor *weight,
          THTensor *bias,
          THTensor *finput,
          int kW,
          int kH,
          int dW,
          int dH,
          int padW,
          int padH,
          long nInputPlane,
          long inputWidth,
          long inputHeight,
          long nOutputPlane,
          long outputWidth,
          long outputHeight)
{
  long i;
  THTensor *output2d;

  THNN_(unfolded_copy)(finput, input, kW, kH, dW, dH, padW, padH, nInputPlane, inputWidth, inputHeight, outputWidth, outputHeight);

  output2d = THTensor_(newWithStorage2d)(output->storage, output->storageOffset,
                                         nOutputPlane, -1,
                                         outputHeight*outputWidth, -1);
  if (bias) {
    for(i = 0; i < nOutputPlane; i++)
        THVector_(fill)(output->storage->data+output->storageOffset+output->stride[0]*i, THTensor_(get1d)(bias, i), outputHeight*outputWidth);
  } else {
    THTensor_(zero)(output);
  }

  THTensor_(addmm)(output2d, 1, output2d, 1, weight, finput);

  THTensor_(free)(output2d);
}
Пример #12
0
static void THNN_(TemporalRowConvolution_updateOutput_frame)(
	THTensor *input,
	THTensor *output,
	THTensor *weight,
	THTensor *bias,
	THTensor *finput,
	int kW,
	int dW,
	int padW,
	int64_t inputFrameSize,
	int64_t nInputFrame,
	int64_t nOutputFrame) {

	int64_t i;

	THTensor *output3d = THTensor_(newWithStorage3d)(
		output->storage, output->storageOffset,
		inputFrameSize, -1,
		1, -1,
		nOutputFrame, -1);

	THNN_(unfolded_copy_row)(finput, input, kW, dW, padW,
	                         inputFrameSize, nInputFrame, nOutputFrame);

	THTensor_(zero)(output);

	if (bias != NULL) {
		for (i = 0; i < inputFrameSize; i++)
			THVector_(fill)
			        (THStorage_(data)(output->storage) + output->storageOffset
			        + output->stride[0] * i,
			        THTensor_(get1d)(bias, i), nOutputFrame);
	}

	THTensor_(baddbmm)(output3d, 1, output3d, 1, weight, finput);

	THTensor_(free)(output3d);
}
Пример #13
0
static void THNN_(TemporalRowConvolution_accGradParameters_frame)(
	THTensor *gradOutput, THTensor *gradWeight, THTensor *gradBias,
	THTensor *finput, real scale) {

	int64_t i;
	THTensor *gradOutput3d = THTensor_(newWithStorage3d)(
		gradOutput->storage, gradOutput->storageOffset,
		gradOutput->size[0], -1,
		1, -1,
		gradOutput->size[1], -1);

    THTensor *tfinput = THTensor_(new)();
	THTensor_(transpose)(tfinput, finput, 1, 2);
	// gradOutput3d:	inputFrameSize x 1 x nOutputFrame
	// finput:			inputFrameSize x nOutputFrame x kW
	THTensor_(baddbmm)(gradWeight, 1, gradWeight, scale, gradOutput3d, tfinput);
	// gradWeight:		inputFrameSize x 1 x kW
    THTensor_(free)(tfinput);

	if (gradBias != NULL) {
		for (i = 0; i < gradBias->size[0]; i++) {
			int64_t k;
			real sum = 0;
			real *data = THStorage_(data)(gradOutput3d->storage)
			             + gradOutput3d->storageOffset
			             + i * gradOutput3d->stride[0];
			for (k = 0; k < gradOutput3d->size[2]; k++) {
				sum += data[k];
			}
			(THStorage_(data)(gradBias->storage) + gradBias->storageOffset)[i]
			        += scale * sum;
		}
	}

	THTensor_(free)(gradOutput3d);

}
Пример #14
0
Файл: ELU.c Проект: ACB1337/nn
void THNN_(ELU_updateOutput)(
          THNNState *state,
          THTensor *input,
          THTensor *output,
          real alpha,
          bool inplace)
{
  if(inplace) {
    TH_TENSOR_APPLY(real, input,
      if(*input_data <= 0) {
        *input_data = (exp(*input_data) - 1) * alpha;
      }
    );
    THTensor_(set)(output, input);
  } else {
Пример #15
0
static void nn_(unfolded_copy)(THTensor *finput, THTensor *input,
                               int kW, int kH,
                               int nInputPlane,
                               int inputWidth, int inputHeight,
                               int outputWidth, int outputHeight)
{
  long k;
  real *input_data = THTensor_(data)(input);
  real *finput_data = THTensor_(data)(finput);

#pragma omp parallel for private(k)
  for(k = 0; k < nInputPlane*kH*kW; k++)
  {
    int nip = k / (kH*kW);
    int rest = k % (kH*kW);
    int kh = rest / kW;
    int kw = rest % kW;
    int y;
    real *dst = finput_data + nip*(kH*kW*outputHeight*outputWidth) + kh*(kW*outputHeight*outputWidth) + kw*(outputHeight*outputWidth);
    real *src = input_data + nip*(inputHeight*inputWidth) + kh*inputWidth + kw;
    for(y = 0; y < outputHeight; y++)
      memcpy(dst+y*outputWidth, src+y*inputWidth, sizeof(real)*outputWidth);
  }
}
static void THNN_(VolumetricConvolutionMM_accGradParameters_frame)(
          THTensor *gradOutput,
          THTensor *gradWeight,
          THTensor *gradBias,
          THTensor *finput,  // can be NULL if gradWeight = NULL
          real scale)
{
  int64_t i;
  THTensor *gradOutput2d = THTensor_(newWithStorage2d)(
    gradOutput->storage, gradOutput->storageOffset,
    gradOutput->size[0], -1,
    gradOutput->size[1]*gradOutput->size[2]*gradOutput->size[3], -1
  );

  if (gradWeight){
    THTensor *tfinput = THTensor_(new)();
    THTensor_(transpose)(tfinput, finput, 0, 1);
    THTensor_(addmm)(gradWeight, 1, gradWeight, scale, gradOutput2d, tfinput);
    THTensor_(free)(tfinput);
  }

  if (gradBias) {
    for (i = 0; i < gradBias->size[0]; i++)
    {
      int64_t k;
      real sum = 0;
      real *data = gradOutput2d->storage->data + gradOutput2d->storageOffset + i*gradOutput2d->stride[0];
      for (k = 0; k < gradOutput2d->size[1]; k++)
        sum += data[k];

      (gradBias->storage->data + gradBias->storageOffset)[i] += scale * sum;
    }
  }

  THTensor_(free)(gradOutput2d);
}
Пример #17
0
Файл: Rack.c Проект: howonlee/nn
void THNN_(Rack_updateOutput)(
          THNNState *state,
          THTensor *input,
          THTensor *output,
          real alpha)
{
  THTensor_(resizeAs)(output, input);
  TH_TENSOR_APPLY2(real, input, real, output,
    if (fmod(*input_data, alpha) <= (alpha * 0.66666)) {
      *output_data = *input_data + (fmod(*input_data, alpha));
    } else {
      *output_data = *input_data + (alpha * 2. - (2. * fmod(*input_data, alpha)));
    }
    // *output_data = rand() % 10 <= 5 ? *input_data + alpha : *input_data - alpha;
  );
Пример #18
0
static int nn_(SpatialConvolutionMM_accGradParameters)(lua_State *L)
{
  THTensor *input = luaT_checkudata(L, 2, torch_Tensor);
  THTensor *gradOutput = luaT_checkudata(L, 3, torch_Tensor);
  real scale = luaL_optnumber(L, 4, 1);
  int nOutputPlane = luaT_getfieldcheckint(L, 1, "nOutputPlane");

  THTensor *finput = luaT_getfieldcheckudata(L, 1, "finput", torch_Tensor);
  THTensor *gradWeight = luaT_getfieldcheckudata(L, 1, "gradWeight", torch_Tensor);
  THTensor *gradBias = luaT_getfieldcheckudata(L, 1, "gradBias", torch_Tensor);

  THArgCheck( nOutputPlane == gradOutput->size[input->nDimension == 4 ? 1 : 0], 1, "Number of output features is not equal to nOutputPlane" );

  if(input->nDimension == 3)
  {
    nn_(SpatialConvolutionMM_accGradParameters_frame)(gradOutput, gradWeight, gradBias, finput, scale);
  }
  else
  {
    long T = input->size[0];
    long t;

    for(t = 0; t < T; t++)
    {
      THTensor *gradOutput_t = THTensor_(newSelect)(gradOutput, 0, t);
      THTensor *finput_t = THTensor_(newSelect)(finput, 0, t);

      nn_(SpatialConvolutionMM_accGradParameters_frame)(gradOutput_t, gradWeight, gradBias, finput_t, scale);

      THTensor_(free)(gradOutput_t);
      THTensor_(free)(finput_t);
    }
  }

  return 0;
}
Пример #19
0
static void nn_(SpatialConvolutionMM_accGradParameters_frame)(THTensor *gradOutput, THTensor *gradWeight, THTensor *gradBias, THTensor *finput,
                                                              real scale)
{
  long i;
  THTensor *gradOutput2d = THTensor_(newWithStorage2d)(gradOutput->storage, gradOutput->storageOffset,
                                                       gradOutput->size[0], -1,
                                                       gradOutput->size[1]*gradOutput->size[2], -1);

  THTensor_(transpose)(finput, finput, 0, 1);
  THTensor_(addmm)(gradWeight, 1, gradWeight, scale, gradOutput2d, finput);
  THTensor_(transpose)(finput, finput, 0, 1);

  for(i = 0; i < gradBias->size[0]; i++)
  {
    long k;
    real sum = 0;
    real *data = gradOutput2d->storage->data + gradOutput2d->storageOffset + i*gradOutput2d->stride[0];
    for(k = 0; k < gradOutput2d->size[1]; k++)
      sum += data[k];
    (gradBias->storage->data + gradBias->storageOffset)[i] += scale*sum;
  }

  THTensor_(free)(gradOutput2d);
}
Пример #20
0
static int THNN_(view_weight_local)(THTensor **_weight)
{
  THTensor *weight = *_weight;
  THArgCheck(weight->nDimension == 3 || weight->nDimension == 6, 4,
          "weight tensor should be 3D or 6D - got %dD", weight->nDimension);
  if (weight->nDimension == 6) {
    long s1 = weight->size[0] * weight->size[1];
    long s2 = weight->size[2];
    long s3 = weight->size[3] * weight->size[4] * weight->size[5];
    *_weight = THTensor_(newWithStorage3d)(weight->storage, 
					   weight->storageOffset, 
					   s1, -1, s2, -1, s3, -1);
    return 1;
  }
  return 0;
}
Пример #21
0
void THNN_(SmoothL1Criterion_updateOutput)(
          THNNState *state,
          THTensor *input,
          THTensor *target,
          THTensor *output,
          bool sizeAverage,
          bool reduce)
{
  THNN_CHECK_SHAPE(input, target);

  if (!reduce) {
    THTensor_(resizeAs)(output, input);
    TH_TENSOR_APPLY3(real, input, real, target, real, output,
      real z = fabs(*input_data - *target_data);
      *output_data = z < 1 ? 0.5 * z * z : z - 0.5;
    );
static inline void THNN_(VolumetricUpSamplingNearest_shapeCheck)
     (THTensor *input, THTensor *gradOutput,
      int scale_factor) {
  THArgCheck(input != NULL, 2, "5D input tensor expected but got NULL");
  THArgCheck(scale_factor > 1, 4,
	     "scale_factor must be greater than 1, but got: %d", scale_factor);
  THNN_ARGCHECK(input->nDimension == 4 || input->nDimension == 5, 2, input,
		"4D or 5D input tensor expected but got: %s");
  if (input->nDimension == 4) {
    int nChannels    = THTensor_(size)(input, 0);
    int inputDepth   = THTensor_(size)(input, 1);
    int inputHeight  = THTensor_(size)(input, 2);
    int inputWidth   = THTensor_(size)(input, 3);
    int outputDepth  = inputDepth  * scale_factor;
    int outputHeight = inputHeight * scale_factor;
    int outputWidth  = inputWidth  * scale_factor;
    if (gradOutput != NULL) {
      THNN_CHECK_DIM_SIZE(gradOutput, 4, 0, nChannels);
      THNN_CHECK_DIM_SIZE(gradOutput, 4, 1, outputDepth);
      THNN_CHECK_DIM_SIZE(gradOutput, 4, 2, outputHeight);
      THNN_CHECK_DIM_SIZE(gradOutput, 4, 3, outputWidth);
    }
  } else {
    int nBatch       = THTensor_(size)(input, 0);
    int nChannels    = THTensor_(size)(input, 1);
    int inputDepth   = THTensor_(size)(input, 2);
    int inputHeight  = THTensor_(size)(input, 3);
    int inputWidth   = THTensor_(size)(input, 4);  
    int outputDepth  = inputDepth  * scale_factor;
    int outputHeight = inputHeight * scale_factor;
    int outputWidth  = inputWidth  * scale_factor;
    if (gradOutput != NULL) {
      THNN_CHECK_DIM_SIZE(gradOutput, 5, 0, nBatch);
      THNN_CHECK_DIM_SIZE(gradOutput, 5, 1, nChannels);
      THNN_CHECK_DIM_SIZE(gradOutput, 5, 2, outputDepth);
      THNN_CHECK_DIM_SIZE(gradOutput, 5, 3, outputHeight);
      THNN_CHECK_DIM_SIZE(gradOutput, 5, 4, outputWidth);
    }
  }
}
Пример #23
0
static int torch_(Tensor_new)(lua_State *L)
{
  THTensor *tensor;
  THStorage *storage = NULL;
  long storageOffset = 0;
  THLongStorage *size = NULL;

  torch_(Tensor_c_readTensorStorageSize)(L, 1, 1, 1, 1,
                                               &storage, &storageOffset, &size);

  tensor = THTensor_(newWithStorage)(storage, storageOffset, size);
  THLongStorage_free(size);

  luaT_pushudata(L, tensor, torch_(Tensor_id));
  return 1;
}
Пример #24
0
Файл: ReLU6.c Проект: JHoef/nn
void THNN_(ReLU6_updateOutput)(
          THNNState *state,
          THTensor *input,
          THTensor *output,
          bool inplace)
{
  if (inplace)
  {
    TH_TENSOR_APPLY(real, input,
      if (*input_data <= 0)
        *input_data = 0;
      else if (*input_data >= 6)
        *input_data = 6;
    );
    THTensor_(set)(output, input);
  }
Пример #25
0
static inline void THNN_(Col2Im_shapeCheck)(
                         THNNState *state,
                         THTensor *input,
                         THTensor *gradOutput,
                         int64_t outputHeight, int64_t outputWidth,
                         int64_t kH, int64_t kW, int64_t dilationH, int64_t dilationW,
                         int64_t padH, int64_t padW, int64_t dH, int64_t dW) {

  THArgCheck(kW > 0 && kH > 0, 6,
             "kernel size should be greater than zero, but got kH: %d kW: %d", kH, kW);
  THArgCheck(dW > 0 && dH > 0, 12,
             "stride should be greater than zero, but got dH: %d dW: %d", dH, dW);
  THArgCheck(dilationW > 0 && dilationH > 0, 8,
             "dilation should be greater than zero, but got dilationH: %d dilationW: %d", dilationH, dilationW);

  int64_t ndim = THTensor_(nDimensionLegacyNoScalars)(input);
  THNN_ARGCHECK(!input->is_empty() && (ndim == 2 || ndim == 3), 2, input,
                "Expected non-empty 2D or 3D input tensor, but got input of shape %s");

  int64_t batch_dim = (ndim == 3) ? 0 : -1;
  int64_t nInputPlane  = input->size(batch_dim + 1);

  if (nInputPlane % (kW * kH) != 0) {
    THError("Expected size of input's dimension 1 to be divisible by the "
            "product of kernel_size, but got input.size(1)=%lld and "
            "kernel_size=(%d, %d).", (long long) nInputPlane, kH, kW);
  }

  int64_t inputLength  = input->size(batch_dim + 2);
  int64_t nBlocksH = div_rtn<int64_t>(outputHeight + 2 * padH - dilationH * (kH - 1) - 1, dH) + 1;
  int64_t nBlocksW = div_rtn<int64_t>(outputWidth + 2 * padW - dilationW * (kW - 1) - 1, dW) + 1;

  if (inputLength != (nBlocksH * nBlocksW)) {
    THError("Given output_size=(%d, %d), kernel_size=(%d, %d), "
            "dilation=(%d, %d), padding=(%d, %d), stride=(%d, %d), expected "
            "size of input's dimension 2 to match the calculated number of "
            "sliding blocks %lld * %lld = %lld, but got input.size(2)=%lld.",
            outputHeight, outputWidth, kH, kW, dilationH, dilationW, padH, padW, dH, dW,
            (long long) nBlocksH, (long long) nBlocksW,
            (long long) (nBlocksH * nBlocksW), (long long) inputLength);
  }

  if (outputWidth < 1 || outputHeight < 1) {
    THError("Expected output spatial size to be positive, but got: output_size=(%d, %d).",
            outputHeight, outputWidth);
  }
}
Пример #26
0
void THNN_(SpatialConvolutionMM_accGradParameters)(
          THNNState *state,
          THTensor *input,
          THTensor *gradOutput,
          THTensor *gradWeight,
          THTensor *gradBias,
          THTensor *finput,
          THTensor *fgradInput,
          int kW,
          int kH,
          int dW,
          int dH,
          int padW,
          int padH,
          real scale)
{
  int freeWeight = 0;
  long nOutputPlane = gradWeight->size[0];
  THArgCheck( nOutputPlane == gradOutput->size[input->nDimension == 4 ? 1 : 0], 3, "Number of output features is not equal to nOutputPlane" );
  THArgCheck(kW > 0 && kH > 0, 8, "kernel size should be greater than zero");
  THArgCheck(dW > 0 && dH > 0, 10, "stride should be greater than zero");
  THArgCheck(gradWeight->nDimension == 2 || gradWeight->nDimension == 4, 4, "gradWeight tensor should be 2D or 4D");

  if (gradWeight->nDimension == 4) {
    long s1 = gradWeight->size[0];
    long s2 = gradWeight->size[1] * gradWeight->size[2] * gradWeight->size[3];
    gradWeight = THTensor_(newWithStorage2d)(gradWeight->storage, gradWeight->storageOffset, s1, -1, s2, -1);
    freeWeight = 1;
  }

  if(input->nDimension == 3)
  {
    THNN_(SpatialConvolutionMM_accGradParameters_frame)(gradOutput, gradWeight, gradBias, finput, scale);
  }
  else
  {
    long T = input->size[0];
    long t;

    for(t = 0; t < T; t++)
    {
      THTensor *gradOutput_t = THTensor_(newSelect)(gradOutput, 0, t);
      THTensor *finput_t = THTensor_(newSelect)(finput, 0, t);

      THNN_(SpatialConvolutionMM_accGradParameters_frame)(gradOutput_t, gradWeight, gradBias, finput_t, scale);

      THTensor_(free)(gradOutput_t);
      THTensor_(free)(finput_t);
    }
  }
  if (freeWeight)
    THTensor_(free)(gradWeight);
}
Пример #27
0
static int cr_(AG_sp_update_var)(lua_State *L) {
    THTensor *var = luaT_checkudata(L, 1, torch_Tensor);
    THTensor *g = luaT_checkudata(L, 2, torch_Tensor);
    THTensor *nz = luaT_checkudata(L, 3, torch_Tensor);

    int h,d;
    if (THTensor_(nDimension)(var) > 1) {
        h = THTensor_(size)(var,0); // height
        d = THTensor_(size)(var,1); // width
    } else {
        h = 1;
        d = THTensor_(nElement)(var);
    }
    int num_nz = THTensor_(nElement)(nz);
    
    sp_update_var_dnz(THTensor_(data)(var), THTensor_(data)(g), THTensor_(data)(nz), h, d, num_nz);

    return 0;
}
Пример #28
0
static int torch_Tensor_(stride)(lua_State *L)
{
  THTensor *tensor = luaT_checkudata(L, 1, torch_Tensor);
  if(lua_isnumber(L,2))
  {
    int dim = luaL_checkint(L, 2)-1;
    THArgCheck(dim >= 0 && dim < tensor->nDimension, 2, "dimension %d out of range of %dD tensor",
        dim+1, THTensor_(nDimension)(tensor));
    lua_pushnumber(L, tensor->stride[dim]);
  }
  else
  {
    THLongStorage *storage = THLongStorage_newWithSize(tensor->nDimension);
    memmove(storage->data, tensor->stride, sizeof(long)*tensor->nDimension);
    luaT_pushudata(L, storage, "torch.LongStorage");
  }
  return 1;
}
Пример #29
0
void THNN_(Threshold_updateOutput)(
          THNNState *state,
          THTensor *input,
          THTensor *output,
          accreal threshold_,
          accreal val_,
          bool inplace)
{
  real threshold = TH_CONVERT_ACCREAL_TO_REAL(threshold_);
  real val = TH_CONVERT_ACCREAL_TO_REAL(val_);
  if (inplace)
  {
    TH_TENSOR_APPLY(real, input,
      if (*input_data <= threshold)
        *input_data = val;
    );
    THTensor_(set)(output, input);
  }
Пример #30
0
TH_API void THTensor_(gesv)(THTensor *rb_, THTensor *ra_, THTensor *b, THTensor *a)
{
  int n, nrhs, lda, ldb, info;
  THIntTensor *ipiv;
  THTensor *ra__;
  THTensor *rb__;

  int clonea;
  int cloneb;
  int destroya;
  int destroyb;

  
  if (a == NULL || ra_ == a) /* possibly destroy the inputs  */
  {
    ra__ = THTensor_(new)();
    clonea = THTensor_(lapackClone)(ra__,ra_,0);
    destroya = 1;
  }