コード例 #1
0
ファイル: matrix_MxN.cpp プロジェクト: JianpingCAI/Physika
void MatrixMxN<Scalar>::singularValueDecomposition(MatrixMxN<Scalar> &left_singular_vectors,
                                                   VectorND<Scalar> &singular_values,
                                                   MatrixMxN<Scalar> &right_singular_vectors) const
{
#ifdef PHYSIKA_USE_EIGEN_MATRIX
    //hack: Eigen::SVD does not support integer types, hence we cast Scalar to long double for decomposition
    unsigned int rows = this->rows(), cols = this->cols();
    Eigen::Matrix<long double,Eigen::Dynamic,Eigen::Dynamic> temp_matrix(rows,cols);
    for(unsigned int i = 0; i < rows; ++i)
        for(unsigned int j = 0; j < cols; ++j)          
                temp_matrix(i,j) = static_cast<long double>((*ptr_eigen_matrix_MxN_)(i,j));
    Eigen::JacobiSVD<Eigen::Matrix<long double,Eigen::Dynamic,Eigen::Dynamic> > svd(temp_matrix,Eigen::ComputeThinU|Eigen::ComputeThinV);
    const Eigen::Matrix<long double,Eigen::Dynamic,Eigen::Dynamic> &left = svd.matrixU(), &right = svd.matrixV();
    const Eigen::Matrix<long double,Eigen::Dynamic,1> &values = svd.singularValues();
    //resize if have to
    if(left_singular_vectors.rows() != left.rows() || left_singular_vectors.cols() != left.cols())
        left_singular_vectors.resize(left.rows(),left.cols());
    if(right_singular_vectors.rows() != right.rows() || right_singular_vectors.cols() != right.cols())
        right_singular_vectors.resize(right.rows(),right.cols());
    if(singular_values.dims() != values.rows())
        singular_values.resize(values.rows());
    //copy the result
    for(unsigned int i = 0; i < left.rows(); ++i)
        for(unsigned int j = 0; j < left.cols(); ++j)
            left_singular_vectors(i,j) = static_cast<Scalar>(left(i,j));
    for(unsigned int i = 0; i < right.rows(); ++i)
        for(unsigned int j = 0; j < right.cols(); ++j)
            right_singular_vectors(i,j) = static_cast<Scalar>(right(i,j));
    for(unsigned int i = 0; i < values.rows(); ++i)
        singular_values[i] = static_cast<Scalar>(values(i,0));
#elif defined(PHYSIKA_USE_BUILT_IN_MATRIX)
    std::cerr<<"SVD not implemeted for built in matrix!\n";
    std::exit(EXIT_FAILURE);
#endif
}
コード例 #2
0
ファイル: matrix_MxN.cpp プロジェクト: JianpingCAI/Physika
void MatrixMxN<Scalar>::eigenDecomposition(VectorND<Scalar> &eigen_values_real, VectorND<Scalar> &eigen_values_imag,
                                           MatrixMxN<Scalar> &eigen_vectors_real, MatrixMxN<Scalar> &eigen_vectors_imag)
{
    unsigned int rows = this->rows(), cols = this->cols();
    if(rows != cols)
    {
        std::cerr<<"Eigen decomposition is only valid for square matrix!\n";
        std::exit(EXIT_FAILURE);
    }
#ifdef PHYSIKA_USE_EIGEN_MATRIX
    //hack: Eigen::EigenSolver does not support integer types, hence we cast Scalar to long double for decomposition
    Eigen::Matrix<long double,Eigen::Dynamic,Eigen::Dynamic> temp_matrix(rows,cols);
    for(unsigned int i = 0; i < rows; ++i)
        for(unsigned int j = 0; j < cols; ++j)          
                temp_matrix(i,j) = static_cast<long double>((*ptr_eigen_matrix_MxN_)(i,j));
    Eigen::EigenSolver<Eigen::Matrix<long double,Eigen::Dynamic,Eigen::Dynamic> > eigen(temp_matrix);
    Eigen::Matrix<std::complex<long double>,Eigen::Dynamic,Eigen::Dynamic> vectors = eigen.eigenvectors();
    const Eigen::Matrix<std::complex<long double>,Eigen::Dynamic,1> &values = eigen.eigenvalues();
    //resize if have to
    if(eigen_vectors_real.rows() != vectors.rows() || eigen_vectors_real.cols() != vectors.cols())
        eigen_vectors_real.resize(vectors.rows(),vectors.cols());
    if(eigen_vectors_imag.rows() != vectors.rows() || eigen_vectors_imag.cols() != vectors.cols())
        eigen_vectors_imag.resize(vectors.rows(),vectors.cols());
    if(eigen_values_real.dims() != values.rows())
        eigen_values_real.resize(values.rows());
    if(eigen_values_imag.dims() != values.rows())
        eigen_values_imag.resize(values.rows());
    //copy the result
    for(unsigned int i = 0; i < vectors.rows(); ++i)
        for(unsigned int j = 0; j < vectors.cols(); ++j)
        {
            eigen_vectors_real(i,j) = static_cast<Scalar>(vectors(i,j).real());
            eigen_vectors_imag(i,j) = static_cast<Scalar>(vectors(i,j).imag());
        }
    for(unsigned int i = 0; i < values.rows(); ++i)
    {
        eigen_values_real[i] = static_cast<Scalar>(values(i,0).real());
        eigen_values_imag[i] = static_cast<Scalar>(values(i,0).imag());
    }
#elif defined(PHYSIKA_USE_BUILT_IN_MATRIX)
    std::cerr<<"Eigen decomposition not implemeted for built in matrix!\n";
    std::exit(EXIT_FAILURE);
#endif
}
コード例 #3
0
ファイル: aspect_tool.cpp プロジェクト: victormm88/AspTerm
//input from console and output the result predict by the model
void AspectTool::realTest(string model_file)
{
    //initialize word vector tool
    Word2VecTool word_vec_tool("../res/glove.42B.300d.txt", false);
    //word_vec_tool.initWordIndexDict();
    word_vec_tool.loadWordDict("word_index_dictionary");

    cout << "Please input the test sectence : " << endl;
    string sentence;
    getline(cin, sentence);

    cout << sentence << endl;
    StringTool st;
    vector<string> word_token = st.tokenize(sentence);

    MatrixXd *p_input_data = new MatrixXd(word_vec_tool.getVectorLength(), word_token.size());
    for (int i = 0; i < word_token.size(); ++i)
    {
        MatrixXd temp_matrix(word_vec_tool.getVectorLength(), 1);
        word_vec_tool.getWrodVect(word_token[i], temp_matrix);
        p_input_data -> col(i) = temp_matrix;
    }

    vector<MatrixXd*> input_datas;
    input_datas.push_back(p_input_data);

//cout << *p_input_data << endl;
    LSTM lstm(model_file);

    vector<MatrixXd*> predict_labels = lstm.predict(input_datas);

    MatrixXd temp_label = *(predict_labels[0]);

    cout << temp_label << endl;
    string aspect_term = "";
    for (int i = 0; i < temp_label.cols(); ++i)
    {
        if (temp_label(1, i) == 1)
        {
            aspect_term = aspect_term + " " + word_token[i];
        }
        else if (temp_label(2, i) == 1 && aspect_term.size() > 0)
        {
            aspect_term = aspect_term + " " + word_token[i];
        }
        else if (temp_label(0, i) == 1)
        {
            if (aspect_term.size() > 0)
            {
                cout << "Aspect Term : " << aspect_term << endl;
            }
            aspect_term = "";
        }
    }

    if (aspect_term.size() > 0)
    {
        cout << "Aspect Term : " << aspect_term << endl;
    }

    return;
}
コード例 #4
0
ファイル: aspect_tool.cpp プロジェクト: victormm88/AspTerm
void AspectTool::getInputData(string file_name, vector<MatrixXd*> &input_datas, vector<MatrixXd*> &input_labels)
{

    ifstream input_stream(file_name.c_str());

    if (!input_stream)
    {
        cout  << file_name << " dose not exist !" << endl;
        return;
    }

    Word2VecTool word_vec_tool("../res/glove.42B.300d.txt", false);
    //word_vec_tool.initWordIndexDict();
    word_vec_tool.loadWordDict("word_index_dictionary");

    string temp_word;

    vector<string> temp_word_list;
    vector<char> temp_label_list;

    //int count = 0;
    while (input_stream >> temp_word)
    {
        if (temp_word == "<end_of_sentence>")
        {
            //construct input matrix of current sequence
            MatrixXd *temp_input_mp = new MatrixXd(word_vec_tool.getVectorLength(), temp_word_list.size());
            for (int t = 0; t < temp_word_list.size(); ++t)
            {
                MatrixXd temp_matrix(word_vec_tool.getVectorLength(), 1);
                word_vec_tool.getWrodVect(temp_word_list[t], temp_matrix);
                temp_input_mp -> col(t) = temp_matrix;
            }

            //construct label matrix of current sequence
            MatrixXd *temp_label_mp = new MatrixXd(3, temp_label_list.size());
            *temp_label_mp = MatrixXd::Zero(3, temp_label_list.size());
            for (int t = 0; t < temp_label_list.size(); ++t)
            {
                if (temp_label_list[t] == 'O')
                {
                    (*temp_label_mp)(0, t) = 1;
                }
                else if (temp_label_list[t] == 'B')
                {
                    (*temp_label_mp)(1, t) = 1;
                }
                else if (temp_label_list[t] == 'I')
                {
                    (*temp_label_mp)(2, t) = 1;
                }
            }

            //insert current input data and label
            input_datas.push_back(temp_input_mp);
            input_labels.push_back(temp_label_mp);

            //clear the list
            temp_word_list.clear();
            temp_label_list.clear();
        }
        else
        {
            temp_word_list.push_back(temp_word);
            char temp_char;
            input_stream >> temp_char;
            temp_label_list.push_back(temp_char);
        }
    }// end 'while'

    return;
}//end 'getInputData'