void ActivationFunctionsTestCase::normaltanh() { const int N = 1000; Eigen::MatrixXd a = Eigen::VectorXd::Random(N) * 10.0; Eigen::MatrixXd z = Eigen::VectorXd::Zero(N); OpenANN::normaltanh(a, z); ASSERT_WITHIN(z.minCoeff(), -1.0, -0.5); ASSERT_WITHIN(z.maxCoeff(), 0.5, 1.0); Eigen::MatrixXd gd = Eigen::VectorXd::Zero(N); OpenANN::normaltanhDerivative(z, gd); ASSERT_WITHIN(gd.minCoeff(), 0.0, 1.0); ASSERT_WITHIN(gd.maxCoeff(), 0.0, 1.0); }
void ActivationFunctionsTestCase::linear() { const int N = 1000; Eigen::MatrixXd a = Eigen::VectorXd::Random(N) * 10.0; Eigen::MatrixXd z = Eigen::VectorXd::Zero(N); OpenANN::linear(a, z); ASSERT_EQUALS(a.minCoeff(), z.minCoeff()); ASSERT_EQUALS(a.maxCoeff(), z.maxCoeff()); Eigen::MatrixXd gd = Eigen::VectorXd::Zero(N); Eigen::MatrixXd expected = Eigen::VectorXd::Ones(N); OpenANN::linearDerivative(gd); ASSERT_EQUALS(gd.sum(), expected.sum()); }
void ActivationFunctionsTestCase::logistic() { const int N = 1000; Eigen::MatrixXd a = Eigen::VectorXd::Random(N) * 10.0; Eigen::MatrixXd z = Eigen::VectorXd::Zero(N); OpenANN::logistic(a, z); ASSERT_WITHIN(z.minCoeff(), 0.0, 0.2); ASSERT_WITHIN(z.maxCoeff(), 0.8, 1.0); Eigen::MatrixXd gd = Eigen::VectorXd::Zero(N); OpenANN::logisticDerivative(z, gd); ASSERT_WITHIN(gd.minCoeff(), 0.0, 1.0); ASSERT_WITHIN(gd.maxCoeff(), 0.0, 1.0); }
void ActivationFunctionsTestCase::rectifier() { const int N = 1000; Eigen::MatrixXd a = Eigen::MatrixXd::Random(1, N) * 10.0; Eigen::MatrixXd z = Eigen::MatrixXd::Zero(1, N); OpenANN::rectifier(a, z); ASSERT_EQUALS(0.0, z.minCoeff()); ASSERT_EQUALS(a.maxCoeff(), z.maxCoeff()); Eigen::MatrixXd gd = Eigen::MatrixXd::Zero(1, N); Eigen::MatrixXd expected = Eigen::MatrixXd::Ones(1, N); for(int i = 0; i < N; i++) expected(i) *= (double)(z(i) > 0.0); OpenANN::rectifierDerivative(z, gd); ASSERT_EQUALS(gd.sum(), expected.sum()); }
void ActivationFunctionsTestCase::softmax() { const int N = 1000; Eigen::MatrixXd a = Eigen::VectorXd::Random(N).transpose(); OpenANN::softmax(a); ASSERT_EQUALS_DELTA(1.0, a.sum(), 1e-3); ASSERT_WITHIN(a.minCoeff(), 0.0, 1.0); ASSERT_WITHIN(a.maxCoeff(), 0.0, 1.0); }
BowResult BoWClassifier::ComputeClass(const std::string& data) { auto toks = Tokenizer::FR(data); ngram_.Annotate(toks); Eigen::MatrixXd probas = bow_.ComputeClass(toks); Eigen::MatrixXd::Index label_res, dummy_zero; probas.maxCoeff(&label_res, &dummy_zero); return {probas, label_res, toks}; }
int main(int argc, char * argv[]) { using namespace Eigen; using namespace std; using namespace igl; VectorXd D; if(!read_triangle_mesh("../shared/beetle.off",V,F)) { cout<<"failed to load mesh"<<endl; } twod = V.col(2).minCoeff()==V.col(2).maxCoeff(); bbd = (V.colwise().maxCoeff()-V.colwise().minCoeff()).norm(); SparseMatrix<double> L,M; cotmatrix(V,F,L); L = (-L).eval(); massmatrix(V,F,MASSMATRIX_TYPE_DEFAULT,M); const size_t k = 5; if(!eigs(L,M,k+1,EIGS_TYPE_SM,U,D)) { cout<<"failed."<<endl; } U = ((U.array()-U.minCoeff())/(U.maxCoeff()-U.minCoeff())).eval(); igl::viewer::Viewer viewer; viewer.callback_key_down = [&](igl::viewer::Viewer & viewer,unsigned char key,int)->bool { switch(key) { default: return false; case ' ': { U = U.rightCols(k).eval(); // Rescale eigen vectors for visualization VectorXd Z = bbd*0.5*U.col(c); Eigen::MatrixXd C; igl::parula(U.col(c).eval(),false,C); c = (c+1)%U.cols(); if(twod) { V.col(2) = Z; } viewer.data.set_mesh(V,F); viewer.data.compute_normals(); viewer.data.set_colors(C); return true; } } }; viewer.callback_key_down(viewer,' ',0); viewer.core.show_lines = false; viewer.launch(); }
void scaleData(Eigen::MatrixXd& data, double min, double max) { if(min >= max) throw OpenANNException("Scaling failed: max has to be greater than min!"); const double minData = data.minCoeff(); const double maxData = data.maxCoeff(); const double dataRange = maxData - minData; const double desiredRange = max - min; const double scaling = desiredRange / dataRange; data = data.array() * scaling + (min - minData * scaling); }
void Locator::buildPathDistMat( const QHash<SymbolPath, SymbolData>& symbolWordList, Eigen::MatrixXd& distMat, Eigen::VectorXd& radiusVec, Eigen::VectorXd& alignVec) { // add to path index map QHash<SymbolPath, int> pathIdxMap; QList<QString> pathStrVec; int ithPath = 0; for (QHash<SymbolPath, SymbolData>::ConstIterator pPath = symbolWordList.constBegin(); pPath != symbolWordList.constEnd(); ++pPath, ++ithPath) { pathIdxMap[pPath.key()] = ithPath; pathStrVec.push_back(pPath.key().getLastSymbol()->toString()); } // build adjencency matrix int n = ithPath; ithPath = 0; radiusVec.resize(n); alignVec = Eigen::VectorXd::Ones(n); Eigen::MatrixXf adjMat = Eigen::MatrixXf::Constant(n,n,-1); for (QHash<SymbolPath, SymbolData>::ConstIterator pPath = symbolWordList.constBegin(); pPath != symbolWordList.constEnd(); ++pPath, ++ithPath) { const SymbolData& symData = pPath.value(); const SymbolPath& symPath = pPath.key(); int srcIdx = pathIdxMap[pPath.key()]; for (QSet<SymbolPath>::ConstIterator pChild = symData.m_lowLevelSym.constBegin(); pChild != symData.m_lowLevelSym.constEnd(); ++pChild) { QHash<SymbolPath, int>::ConstIterator pTar = pathIdxMap.find(*pChild); if (pTar == pathIdxMap.constEnd()) continue; int tarIdx = pTar.value(); adjMat(srcIdx, tarIdx) = adjMat(tarIdx, srcIdx) = 1; } radiusVec(ithPath) = symData.getRadius(); if(const SymbolInfo* info = symPath.getLastSymbol()) { alignVec(ithPath) = MDSSolver::getAlignVal(info->name()); } } // build distance matrix MatrixXf dMat; GraphUtility::computePairDist(adjMat, dMat); distMat = dMat.cast<double>(); // save mats for debug QString matStr = GlobalUtility::toMatlabString(adjMat, "adjMat"); matStr += GlobalUtility::toMatlabString(dMat, "distMat"); matStr += GlobalUtility::toMatlabString(pathStrVec, "entityName"); matStr += "adjMat(find(adjMat<0)) = 0;\n"; GlobalUtility::saveString("H:\\Programs\\QtCreator\\qt-creator_master\\src\\plugins\\MyPlugin\\CodeAtlas\\tests\\2-20\\matStr.m", matStr); // fill distances of different connect components float maxDist= distMat.maxCoeff() * 2.f; for (int i = 0; i < distMat.rows(); i++) for (int j = 0; j < distMat.cols(); j++) if (distMat(i,j) == -1) distMat(i,j) = maxDist; }