/**
                     * Allows to dump the search lattice in case it is needed.
                     * Note that, in this code we do not care if in case of an
                     * error we do not close the files. This is because lattice
                     * dumping is only needed while model training, plus it should
                     * work without throwing any errors. So if it happens then
                     * it is not a big problem.
                     * @param de_params the decoder parameters
                     */
                    inline void dump_search_lattice(const de_parameters & de_params) const {
                        LOG_DEBUG1 << "Dumping the search lattice for the translation task " << m_task_id << END_LOG;

                        //Create the file names, we do not add the session id and job id as
                        //the tasks in training mode are issued unique task ids.
                        const string file_name = de_params.m_lattices_folder + "/" + to_string(m_task_id) + ".";
                        const string scores_file_name = file_name + de_params.m_scores_file_ext;
                        const string lattice_file_name = file_name + de_params.m_lattice_file_ext;

                        //Open the output stream to the files
                        ofstream scores_file(scores_file_name), lattice_file(lattice_file_name);

                        try {
                            //Check that the files are open
                            ASSERT_CONDITION_THROW(!scores_file.is_open(), string("Could not open: ") +
                                    scores_file_name + string(" for writing"));
                            ASSERT_CONDITION_THROW(!lattice_file.is_open(), string("Could not open: ") +
                                    lattice_file_name + string(" for writing"));

                            //Call the sentence decoder to do dumping.
                            m_decoder.dump_search_lattice(lattice_file, scores_file);
                        } catch (std::exception & ex) {
                            //Close the lattice files
                            LOG_ERROR << ex.what() << END_LOG;
                            close_lattice_files(scores_file, lattice_file);
                            //Re-throw an exception, do not use the exception object as it would
                            //create a copy of it loosing all needed additional information.
                            throw;
                        }
                        //Close the lattice files
                        close_lattice_files(scores_file, lattice_file);
                    }
Ejemplo n.º 2
0
int main(int argc, char *argv[]) {
  std::string solver_name(rokko::serial_dense_solver::default_solver());
  std::string lattice_file("xyz.dat");
  if (argc >= 2) solver_name = argv[1];
  if (argc >= 3) lattice_file = argv[2];

  std::cout.precision(5);

  int num_sites;
  std::vector<std::pair<int, int> > lattice;
  std::vector<boost::tuple<double, double, double> > coupling;
  rokko::read_lattice_file(lattice_file, num_sites, lattice, coupling);
  int dim = 1 << num_sites;

  rokko::serial_dense_solver solver(solver_name);
  solver.initialize(argc, argv);
  std::cout << "Eigenvalue decomposition of XYZ model" << std::endl
            << "solver = " << solver_name << std::endl
            << "lattice file = " << lattice_file << std::endl
            << "number of sites = " << num_sites << std::endl
            << "number of bonds = " << lattice.size() << std::endl
            << "matrix dimension = " << dim << std::endl;

  rokko::localized_matrix<double, matrix_major> mat(dim, dim);
  rokko::xyz_hamiltonian::generate(num_sites, lattice, coupling, mat);

  rokko::localized_vector<double> eigval(dim);
  rokko::localized_matrix<double, matrix_major> eigvec(dim, dim);
  try {
    solver.diagonalize(mat, eigval, eigvec);
  }
  catch (const char *e) {
    std::cout << "Exception : " << e << std::endl;
    exit(22);
  }
  rokko::xyz_hamiltonian::generate(num_sites, lattice, coupling, mat);

  std::cout << "smallest eigenvalues:";
  for (int i = 0; i < std::min(dim, 10); ++i) std::cout << ' ' << eigval(i);
  std::cout << std::endl;
  std::cout << "residual of the smallest eigenvalue/vector: |x A x - lambda| = "
            << std::abs(eigvec.col(0).transpose() * mat * eigvec.col(0) - eigval(0))
            << std::endl;

  solver.finalize();
}