Esempio n. 1
0
 void Optimize(arma::Cube<eT>& weights,
               arma::Cube<eT>& gradient,
               arma::Cube<eT>& momWeights)
 {
   for (size_t s = 0; s < weights.n_slices; s++)
     Optimize(weights.slice(s), gradient.slice(s), momWeights.slice(s));
 }
Esempio n. 2
0
  void Gradient(arma::Cube<eT>& gradient)
  {
    gradient = arma::zeros<arma::Cube<eT> >(weights.n_rows, weights.n_cols,
        weights.n_slices);

    for (size_t outputmap = 0; outputmap < outputLayer.OutputMaps(); outputmap++)
    {
      for (size_t inputmap = 0, s = outputmap; inputmap < inputLayer.OutputMaps();
        inputmap++, s += OutputLayer().OutputMaps())
      {
        arma::Cube<eT> inputSlices = inputLayer.InputActivation().slices(
            inputmap * inputLayer.LayerSlices(), (inputmap + 1) *
            inputLayer.LayerSlices() - 1);

        arma::Cube<eT> deltaSlices = outputLayer.Delta().slices(
            outputmap * inputLayer.LayerSlices(),
            (outputmap + 1) * inputLayer.LayerSlices() - 1);

        arma::Cube<eT> output;
        GradientConvolutionRule::Convolution(inputSlices, deltaSlices, output);

        for (size_t i = 0; i < output.n_slices; i++)
          gradient.slice(s) += output.slice(i);

        gradient.slice(s) /= inputLayer.LayerSlices();
      }
    }
  }
Esempio n. 3
0
void Rotate180(const arma::Cube<eT>& input, arma::Cube<eT>& output)
{
    output = arma::Cube<eT>(input.n_rows, input.n_cols, input.n_slices);

    // * left-right flip, up-down flip */
    for (size_t s = 0; s < output.n_slices; s++)
        output.slice(s) = arma::fliplr(arma::flipud(input.slice(s)));
}
Esempio n. 4
0
  void Forward(const arma::Cube<eT>& input, arma::Cube<eT>& output)
  {
    output = arma::zeros<arma::Cube<eT> >(input.n_rows / kSize,
                            input.n_cols / kSize, input.n_slices);

    for (size_t s = 0; s < input.n_slices; s++)
      Pooling(input.slice(s), output.slice(s));
  }
Esempio n. 5
0
 void Optimize(arma::Cube<eT>& weights,
               arma::Cube<eT>& gradient,
               arma::Cube<eT>& meanSquaredGradient,
               arma::Cube<eT>& meanSquaredGradientDx)
 {
   for (size_t s = 0; s < weights.n_slices; s++)
   {
     Optimize(weights.slice(s), gradient.slice(s), meanSquaredGradient.slice(s),
         meanSquaredGradientDx.slice(s));
   }
 }
Esempio n. 6
0
 void Optimize(arma::Cube<eT>& weights,
               arma::Cube<eT>& gradient,
               arma::Cube<eT>& mean,
               arma::Cube<eT>& variance)
 {
   for (size_t s = 0; s < weights.n_slices; s++)
   {
     Optimize(weights.slice(s), gradient.slice(s), mean.slice(s),
         variance.slice(s));
   }
 }
Esempio n. 7
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. 8
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. 9
0
 void Forward(const arma::Cube<eT>& input, arma::Cube<eT>& output)
 {
   output = input;
   for (size_t s = 0; s < input.n_slices; s++)
   {
     output.slice(s) += weights(s) * bias;
   }
 }
Esempio n. 10
0
 void Gradient(const arma::Cube<eT>& d, InputDataType& g)
 {
   g = arma::Mat<eT>(weights.n_rows, weights.n_cols);
   for (size_t s = 0; s < d.n_slices; s++)
   {
     g(s) = arma::accu(d.slice(s)) * bias;
   }
 }
Esempio n. 11
0
  static void Convolution(const arma::Cube<eT>& input,
                          const arma::Mat<eT>& filter,
                          arma::Cube<eT>& output)
  {
    arma::Mat<eT> convOutput;
    SVDConvolution<BorderMode>::Convolution(input.slice(0), filter, convOutput);

    output = arma::Cube<eT>(convOutput.n_rows, convOutput.n_cols,
        input.n_slices);
    output.slice(0) = convOutput;

    for (size_t i = 1; i < input.n_slices; i++)
    {
      SVDConvolution<BorderMode>::Convolution(input.slice(i), filter,
          convOutput);
      output.slice(i) = convOutput;
    }
  }
Esempio n. 12
0
void TestArmadilloSerialization(arma::Cube<CubeType>& x)
{
  // First save it.
  std::ofstream ofs("test", std::ios::binary);
  OArchiveType o(ofs);

  bool success = true;
  try
  {
    o << BOOST_SERIALIZATION_NVP(x);
  }
  catch (boost::archive::archive_exception& e)
  {
    success = false;
  }

  BOOST_REQUIRE_EQUAL(success, true);
  ofs.close();

  // Now load it.
  arma::Cube<CubeType> orig(x);
  success = true;
  std::ifstream ifs("test", std::ios::binary);
  IArchiveType i(ifs);

  try
  {
    i >> BOOST_SERIALIZATION_NVP(x);
  }
  catch (boost::archive::archive_exception& e)
  {
    success = false;
  }

  BOOST_REQUIRE_EQUAL(success, true);

  BOOST_REQUIRE_EQUAL(x.n_rows, orig.n_rows);
  BOOST_REQUIRE_EQUAL(x.n_cols, orig.n_cols);
  BOOST_REQUIRE_EQUAL(x.n_elem_slice, orig.n_elem_slice);
  BOOST_REQUIRE_EQUAL(x.n_slices, orig.n_slices);
  BOOST_REQUIRE_EQUAL(x.n_elem, orig.n_elem);
   
  for(size_t slice = 0; slice != x.n_slices; ++slice){
	auto const &orig_slice = orig.slice(slice);
	auto const &x_slice = x.slice(slice);
    for (size_t i = 0; i < x.n_cols; ++i){
      for (size_t j = 0; j < x.n_rows; ++j){
        if (double(orig_slice(j, i)) == 0.0)
          BOOST_REQUIRE_SMALL(double(x_slice(j, i)), 1e-8);
        else
          BOOST_REQUIRE_CLOSE(double(orig_slice(j, i)), double(x_slice(j, i)), 1e-8);
	  }
	}
  }

  remove("test");
}
Esempio n. 13
0
void Forward(const arma::Cube<eT>& input, arma::Cube<eT>& output)
{
    const size_t wConv = ConvOutSize(input.n_rows, wfilter, xStride, wPad);
    const size_t hConv = ConvOutSize(input.n_cols, hfilter, yStride, hPad);

    output = arma::zeros<arma::Cube<eT> >(wConv, hConv, outMaps);
    for (size_t outMap = 0, outMapIdx = 0; outMap < outMaps; outMap++)
    {
        for (size_t inMap = 0; inMap < inMaps; inMap++, outMapIdx++)
        {
            arma::Mat<eT> convOutput;
            ForwardConvolutionRule::Convolution(input.slice(inMap),
                                                weights.slice(outMap), convOutput);

            output.slice(outMap) += convOutput;
        }
    }
}
  void Initialize(arma::Cube<eT>& W,
                  const size_t rows,
                  const size_t cols,
                  const size_t slices)
  {
    W = arma::Cube<eT>(rows, cols, slices);

    for (size_t i = 0; i < slices; i++)
      Initialize(W.slice(i), rows, cols);
  }
void
NumericalTestTrialIntegrator<BasisFunctionType, ResultType, GeometryFactory>::
integrate(const std::vector<int> &elementIndices,
          const Shapeset<BasisFunctionType> &testShapeset,
          const Shapeset<BasisFunctionType> &trialShapeset,
          arma::Cube<ResultType> &result) const {
    const size_t pointCount = m_localQuadPoints.n_cols;
    const size_t elementCount = elementIndices.size();

    if (pointCount == 0 || elementCount == 0)
        return;
    // TODO: in the (pathological) case that pointCount == 0 but
    // elementCount != 0, set elements of result to 0.

    // Evaluate constants
    const int testDofCount = testShapeset.size();
    const int trialDofCount = trialShapeset.size();

    BasisData<BasisFunctionType> testBasisData, trialBasisData;
    GeometricalData<CoordinateType> geomData;

    size_t testBasisDeps = 0, trialBasisDeps = 0;
    size_t geomDeps = 0; // INTEGRATION_ELEMENTS;

    m_testTransformations.addDependencies(testBasisDeps, geomDeps);
    m_trialTransformations.addDependencies(trialBasisDeps, geomDeps);
    m_integral.addGeometricalDependencies(geomDeps);

    typedef typename GeometryFactory::Geometry Geometry;
    std::unique_ptr<Geometry> geometry(m_geometryFactory.make());

    CollectionOf3dArrays<BasisFunctionType> testValues, trialValues;

    result.set_size(testDofCount, trialDofCount, elementCount);

    testShapeset.evaluate(testBasisDeps, m_localQuadPoints, ALL_DOFS,
                          testBasisData);
    trialShapeset.evaluate(trialBasisDeps, m_localQuadPoints, ALL_DOFS,
                           trialBasisData);

    // Iterate over the elements
    for (size_t e = 0; e < elementCount; ++e) {
        const int elementIndex = elementIndices[e];
        m_rawGeometry.setupGeometry(elementIndex, *geometry);
        geometry->getData(geomDeps, m_localQuadPoints, geomData);
        if (geomDeps & DOMAIN_INDEX)
            geomData.domainIndex = m_rawGeometry.domainIndex(elementIndex);
        m_testTransformations.evaluate(testBasisData, geomData, testValues);
        m_trialTransformations.evaluate(trialBasisData, geomData, trialValues);

        m_integral.evaluate(geomData, testValues, trialValues, m_quadWeights,
                            result.slice(e));
    }
}
Esempio n. 16
0
  static void Convolution(const arma::Cube<eT>& input,
                          const arma::Mat<eT>& filter,
                          arma::Cube<eT>& output,
                          const size_t dW = 1,
                          const size_t dH = 1)
  {
    arma::Mat<eT> convOutput;
    NaiveConvolution<BorderMode>::Convolution(input.slice(0), filter,
        convOutput, dW, dH);

    output = arma::Cube<eT>(convOutput.n_rows, convOutput.n_cols,
        input.n_slices);
    output.slice(0) = convOutput;

    for (size_t i = 1; i < input.n_slices; i++)
    {
      NaiveConvolution<BorderMode>::Convolution(input.slice(i), filter,
          output.slice(i), dW, dH);
    }
  }
Esempio n. 17
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 outMap = 0, outMapIdx = 0; outMap < inMaps; outMap++)
    {
        for (size_t inMap = 0; inMap < outMaps; inMap++, outMapIdx++)
        {
            arma::Mat<eT> rotatedFilter;
            Rotate180(weights.slice(outMap * outMaps + inMap), rotatedFilter);

            arma::Mat<eT> output;
            BackwardConvolutionRule::Convolution(gy.slice(inMap), rotatedFilter,
                                                 output);

            g.slice(outMap) += output;
        }
    }
}
Esempio n. 18
0
void Gradient(const arma::Cube<eT>& d, arma::Cube<eT>& g)
{
    g = arma::zeros<arma::Cube<eT> >(weights.n_rows, weights.n_cols,
                                     weights.n_slices);

    for (size_t outMap = 0; outMap < outMaps; outMap++)
    {
        for (size_t inMap = 0, s = outMap; inMap < inMaps; inMap++, s += outMaps)
        {
            arma::Cube<eT> inputSlices = inputParameter.slices(inMap, inMap);
            arma::Cube<eT> deltaSlices = d.slices(outMap, outMap);

            arma::Cube<eT> output;
            GradientConvolutionRule::Convolution(inputSlices, deltaSlices, output);

            for (size_t i = 0; i < output.n_slices; i++)
                g.slice(s) += output.slice(i);
        }
    }
}
Esempio n. 19
0
 void Fn(const arma::Cube<eT>& x, arma::Cube<eT>& y)
 {
   y = x;
   for (size_t s = 0; s < x.n_slices; s++)
     Fn(x.slice(s), y.slice(s));
 }
Esempio n. 20
0
 void FeedForward(const arma::Cube<eT>& input)
 {
   for (size_t s = 0; s < input.n_slices; s++)
     Pooling(input.slice(s), outputLayer.InputActivation().slice(s));
 }