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)); }
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(); } } }
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))); }
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)); }
arma::Cube<double> VanillaFeedForward::feedForward( const arma::Cube<double>& x) { const arma::Mat<double>& weightVector = mW.slice(0); const arma::Col<double>& xVector = arma::Col<double>( (const double*) x.begin(), x.size()); ; const arma::Col<double>& biasVector = mB; arma::Col<double> v = weightVector * xVector + biasVector; return arma::Cube<double>((const double*) v.begin(), 1, 1, v.size()); }
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)); } }
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)); } }
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)); } }
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)); } }
void FeedBackward(const arma::Cube<eT>& error) { delta = arma::zeros<arma::Cube<eT> >(inputLayer.InputActivation().n_rows, inputLayer.InputActivation().n_cols, inputLayer.InputActivation().n_slices); for (size_t outputmap = 0; outputmap < inputLayer.OutputMaps(); outputmap++) { for (size_t inputmap = 0; inputmap < outputLayer.OutputMaps(); inputmap++) { arma::Cube<eT> errorSlices = error.slices(inputmap * inputLayer.LayerSlices(), (inputmap * inputLayer.LayerSlices()) + inputLayer.LayerSlices() - 1); arma::Mat<eT> rotatedFilter; Rotate180(weights.slice( outputmap * outputLayer.OutputMaps() + inputmap), rotatedFilter); arma::Cube<eT> output; BackwardConvolutionRule::Convolution(errorSlices, rotatedFilter, output); delta.slices((outputmap * inputLayer.LayerSlices()), (outputmap * inputLayer.LayerSlices()) + inputLayer.LayerSlices() - 1) += output; } } }
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; } }
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; } }
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)); } }
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; } }
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 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"); }
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); }
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); } }
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); } } }
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; } } }
void FeedForward(const arma::Cube<eT>& input) { for (size_t outputmap = 0; outputmap < outputLayer.OutputMaps(); outputmap++) { for (size_t inputmap = 0; inputmap < inputLayer.OutputMaps(); inputmap++) { arma::Cube<eT> inputSlices = input.slices( inputmap * inputLayer.LayerSlices(), (inputmap * inputLayer.LayerSlices()) + inputLayer.LayerSlices() - 1); arma::Cube<eT> output; ForwardConvolutionRule::Convolution(inputSlices, weights.slice(inputmap * outputLayer.OutputMaps() + outputmap), output); outputLayer.InputActivation().slices( (outputmap * inputLayer.LayerSlices()), (outputmap * inputLayer.LayerSlices()) + inputLayer.LayerSlices() - 1) += output; } } }
const arma::Cube<eT> Element(arma::Cube<eT>& input, const size_t sliceNum) { return input.slices(sliceNum, sliceNum); }
void NumericalTestTrialIntegrator<BasisFunctionType, ResultType, GeometryFactory>::integrate( const std::vector<int>& elementIndices, const Basis<BasisFunctionType>& testBasis, const Basis<BasisFunctionType>& trialBasis, 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 componentCount = m_testTransformations.resultDimension(0); const int testDofCount = testBasis.size(); const int trialDofCount = trialBasis.size(); // if (m_trialTransformations.codomainDimension() != componentCount) // throw std::runtime_error("NumericalTestTrialIntegrator::integrate(): " // "test and trial functions " // "must have the same number of components"); BasisData<BasisFunctionType> testBasisData, trialBasisData; GeometricalData<CoordinateType> geomData; size_t testBasisDeps = 0, trialBasisDeps = 0; size_t geomDeps = INTEGRATION_ELEMENTS; m_testTransformations.addDependencies(testBasisDeps, geomDeps); m_trialTransformations.addDependencies(trialBasisDeps, geomDeps); typedef typename GeometryFactory::Geometry Geometry; std::auto_ptr<Geometry> geometry(m_geometryFactory.make()); CollectionOf3dArrays<BasisFunctionType> testValues, trialValues; result.set_size(testDofCount, trialDofCount, elementCount); testBasis.evaluate(testBasisDeps, m_localQuadPoints, ALL_DOFS, testBasisData); trialBasis.evaluate(trialBasisDeps, m_localQuadPoints, ALL_DOFS, trialBasisData); // Iterate over the elements for (size_t e = 0; e < elementCount; ++e) { m_rawGeometry.setupGeometry(elementIndices[e], *geometry); geometry->getData(geomDeps, m_localQuadPoints, geomData); m_testTransformations.evaluate(testBasisData, geomData, testValues); m_trialTransformations.evaluate(trialBasisData, geomData, trialValues); for (int trialDof = 0; trialDof < trialDofCount; ++trialDof) for (int testDof = 0; testDof < testDofCount; ++testDof) { ResultType sum = 0.; for (size_t point = 0; point < pointCount; ++point) for (int dim = 0; dim < componentCount; ++dim) sum += m_quadWeights[point] * geomData.integrationElements(point) * conjugate(testValues[0](dim, testDof, point)) * trialValues[0](dim, trialDof, point); result(testDof, trialDof, e) = sum; } } }
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)); }
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)); }
static void deriv(const arma::Cube<eT>& y, arma::Cube<eT>& x) { x.ones(y.n_rows, y.n_cols, y.n_slices); }