Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add function to load vocabularies from the file #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sample_data/sample_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ target_vocabulary_type=word
source_vocabulary_size=4100
target_vocabulary_size=4900

; If you specified the model directory here, load vocabulary from the specified model.
; If you use this option, the vocabulary type and the vocabulary size specified above will be ignored.
; By writing vocabulary_model=none, this option will be ignored.
vocabulary_model=none
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows us not to load vocabulary from none directory. It might be better to separate switch and path options.


; Name of the encoder strategy. Available options:
; * bidirectional ... Bidirectional RNN.
; * forward ......... Forward RNN.
Expand Down
51 changes: 41 additions & 10 deletions src/bin/train.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <nmtkit/init.h>
#include <nmtkit/monotone_sampler.h>
#include <nmtkit/sorted_random_sampler.h>
#include <nmtkit/vocabulary.h>
#include <nmtkit/word_vocabulary.h>
#include <spdlog/spdlog.h>

Expand Down Expand Up @@ -333,6 +334,23 @@ float evaluateBLEU(
return evaluator->integrate(stats);
}

template <class T>
void loadArchive(
const FS::path & filepath,
const string & archive_format,
T * obj) {
std::ifstream ifs(filepath.string());
NMTKIT_CHECK(
ifs.is_open(), "Could not open file to read: " + filepath.string());
if (archive_format == "binary") {
boost::archive::binary_iarchive iar(ifs);
iar >> *obj;
} else if (archive_format == "text") {
boost::archive::text_iarchive iar(ifs);
iar >> *obj;
}
}

} // namespace

int main(int argc, char * argv[]) {
Expand Down Expand Up @@ -374,16 +392,29 @@ int main(int argc, char * argv[]) {
nmtkit::initialize(global_config);

// Creates vocabularies.
boost::scoped_ptr<nmtkit::Vocabulary> src_vocab(
::createVocabulary(
config.get<string>("Corpus.train_source"),
config.get<string>("Model.source_vocabulary_type"),
config.get<unsigned>("Model.source_vocabulary_size")));
boost::scoped_ptr<nmtkit::Vocabulary> trg_vocab(
::createVocabulary(
config.get<string>("Corpus.train_target"),
config.get<string>("Model.target_vocabulary_type"),
config.get<unsigned>("Model.target_vocabulary_size")));
boost::scoped_ptr<nmtkit::Vocabulary> src_vocab, trg_vocab;
if (config.get<string>("Model.vocabulary_model") == "none") {
logger->info("Making source vocabulary.");
src_vocab.reset(::createVocabulary(
config.get<string>("Corpus.train_source"),
config.get<string>("Model.source_vocabulary_type"),
config.get<unsigned>("Model.source_vocabulary_size")));
logger->info("Making target vocabulary.");
trg_vocab.reset(::createVocabulary(
config.get<string>("Corpus.train_target"),
config.get<string>("Model.target_vocabulary_type"),
config.get<unsigned>("Model.target_vocabulary_size")));
} else {
FS::path vocab_model_dir(config.get<string>("Model.vocabulary_model"));
// Parses config file.
PT::ptree vocab_config;
PT::read_ini((vocab_model_dir / "config.ini").string(), vocab_config);
// Archive format to load models.
const string vocab_archive_format = vocab_config.get<string>("Global.archive_format");
::loadArchive(vocab_model_dir / "source.vocab", vocab_archive_format, &src_vocab);
::loadArchive(vocab_model_dir / "target.vocab", vocab_archive_format, &trg_vocab);
logger->info("Loaded vocabularies.");
}
::saveArchive(model_dir / "source.vocab", archive_format, src_vocab);
::saveArchive(model_dir / "target.vocab", archive_format, trg_vocab);

Expand Down