Esempio n. 1
0
 void FeedBackward(const arma::Cube<eT>& error)
 {
   for (size_t s = 0; s < error.n_slices; s++)
   {
     Unpooling(inputLayer.InputActivation().slice(s), error.slice(s),
         delta.slice(s));
   }
 }
Esempio n. 2
0
  void Backward(const arma::Cube<eT>& /* unused */,
                const arma::Cube<eT>& gy,
                arma::Cube<eT>& g)
  {
    g = arma::zeros<arma::Cube<eT> >(inputParameter.n_rows,
        inputParameter.n_cols, inputParameter.n_slices);

    for (size_t s = 0; s < gy.n_slices; s++)
    {
      Unpooling(inputParameter.slice(s), gy.slice(s), g.slice(s));
    }
  }
Esempio n. 3
0
void MeanPooling<InputDataType, OutputDataType>::Backward(
  const arma::Mat<eT>&& /* input */,
  arma::Mat<eT>&& gy,
  arma::Mat<eT>&& g)
{
  arma::cube mappedError = arma::cube(gy.memptr(), outputWidth,
      outputHeight, outSize);

  gTemp = arma::zeros<arma::cube>(inputTemp.n_rows,
      inputTemp.n_cols, inputTemp.n_slices);

  for (size_t s = 0; s < mappedError.n_slices; s++)
  {
    Unpooling(inputTemp.slice(s), mappedError.slice(s), gTemp.slice(s));
  }

  g = arma::mat(gTemp.memptr(), gTemp.n_elem, 1);
}
Esempio n. 4
0
 void FeedBackward(const arma::Mat<eT>& error)
 {
   Unpooling(inputLayer.InputActivation(), error, inputLayer.Delta());
 }
Esempio n. 5
0
void Glimpse<InputDataType, OutputDataType>::Backward(
    const arma::Mat<eT>&& /* input */, arma::Mat<eT>&& gy, arma::Mat<eT>&& g)
{
  // Generate a cube using the backpropagated error matrix.
  arma::Cube<eT> mappedError = arma::zeros<arma::cube>(outputWidth,
      outputHeight, 1);

  location = locationParameter.back();
  locationParameter.pop_back();

  for (size_t s = 0, j = 0; s < mappedError.n_slices; s+= gy.n_cols, j++)
  {
    for (size_t i = 0; i < gy.n_cols; i++)
    {
      mappedError.slice(s + i) = arma::Mat<eT>(gy.memptr(),
          outputWidth, outputHeight);
    }
  }

  gTemp = arma::zeros<arma::cube>(inputTemp.n_rows, inputTemp.n_cols,
      inputTemp.n_slices);

  for (size_t inputIdx = 0; inputIdx < inSize; inputIdx++)
  {
    for (size_t depthIdx = 0, glimpseSize = size;
        depthIdx < depth; depthIdx++, glimpseSize *= scale)
    {
      size_t padSize = std::floor((glimpseSize - 1) / 2);

      arma::Cube<eT> inputPadded = arma::zeros<arma::Cube<eT> >(
          inputTemp.n_rows + padSize * 2, inputTemp.n_cols +
          padSize * 2, inputTemp.n_slices / inSize);

      size_t h = inputPadded.n_rows - glimpseSize;
      size_t w = inputPadded.n_cols - glimpseSize;

      size_t x = std::min(h, (size_t) std::max(0.0,
          (location(0, inputIdx) + 1) / 2.0 * h));
      size_t y = std::min(w, (size_t) std::max(0.0,
          (location(1, inputIdx) + 1) / 2.0 * w));

      if (depthIdx == 0)
      {
        for (size_t j = (inputIdx + depthIdx), paddedSlice = 0;
            j < mappedError.n_slices; j += (inSize * depth), paddedSlice++)
        {
          inputPadded.subcube(x, y,
              paddedSlice, x + glimpseSize - 1, y + glimpseSize - 1,
              paddedSlice) = mappedError.slice(j);
        }
      }
      else
      {
        for (size_t j = (inputIdx + depthIdx * (depth - 1)), paddedSlice = 0;
            j < mappedError.n_slices; j += (inSize * depth), paddedSlice++)
        {
          arma::Mat<eT> poolingOutput = inputPadded.subcube(x, y,
               paddedSlice, x + glimpseSize - 1, y + glimpseSize - 1,
               paddedSlice);

          if (scale == 2)
          {
            Unpooling(inputTemp.slice(paddedSlice), mappedError.slice(j),
                poolingOutput);
          }
          else
          {
            DownwardReSampling(inputTemp.slice(paddedSlice),
                mappedError.slice(j), poolingOutput);
          }

          inputPadded.subcube(x, y,
              paddedSlice, x + glimpseSize - 1, y + glimpseSize - 1,
              paddedSlice) = poolingOutput;
        }
      }

      gTemp += inputPadded.tube(padSize, padSize, padSize +
          inputTemp.n_rows - 1, padSize + inputTemp.n_cols - 1);
    }
  }

  Transform(gTemp);
  g = arma::mat(gTemp.memptr(), gTemp.n_elem, 1);
}