Skip to content

Commit

Permalink
added an option to save the output vectors
Browse files Browse the repository at this point in the history
Summary:
added an option to save the output vectors too. -saveOutput 1 will
save the output vectors along with the input ones. Saves to a file
called [something].output

Reviewed By: ajoulin

Differential Revision: D3923702

fbshipit-source-id: c2fb1cd0f6d51fcd9ed95fd54ee7e44c5e6fe861
  • Loading branch information
piotr-bojanowski authored and facebook-github-bot committed Mar 13, 2017
1 parent 3e4a901 commit 955fbe7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Args::Args() {
label = "__label__";
verbose = 2;
pretrainedVectors = "";
saveOutput = 0;
}

void Args::parseArgs(int argc, char** argv) {
Expand Down Expand Up @@ -113,6 +114,8 @@ void Args::parseArgs(int argc, char** argv) {
verbose = atoi(argv[ai + 1]);
} else if (strcmp(argv[ai], "-pretrainedVectors") == 0) {
pretrainedVectors = std::string(argv[ai + 1]);
} else if (strcmp(argv[ai], "-saveOutput") == 0) {
saveOutput = atoi(argv[ai + 1]);
} else {
std::cout << "Unknown argument: " << argv[ai] << std::endl;
printHelp();
Expand Down Expand Up @@ -158,6 +161,7 @@ void Args::printHelp() {
<< " -label labels prefix [" << label << "]\n"
<< " -verbose verbosity level [" << verbose << "]\n"
<< " -pretrainedVectors pretrained word vectors for supervised learning []"
<< " -saveOutput whether output params should be saved [" << saveOutput << "]\n"
<< std::endl;
}

Expand Down
1 change: 1 addition & 0 deletions src/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Args {
std::string label;
int verbose;
std::string pretrainedVectors;
int saveOutput;

void parseArgs(int, char**);
void printHelp();
Expand Down
20 changes: 20 additions & 0 deletions src/fasttext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ void FastText::saveVectors() {
ofs.close();
}

void FastText::saveOutput() {
std::ofstream ofs(args_->output + ".output");
if (!ofs.is_open()) {
std::cout << "Error opening file for saving vectors." << std::endl;
exit(EXIT_FAILURE);
}
ofs << dict_->nwords() << " " << args_->dim << std::endl;
Vector vec(args_->dim);
for (int32_t i = 0; i < dict_->nwords(); i++) {
std::string word = dict_->getWord(i);
vec.zero();
vec.addRow(*output_, i);
ofs << word << " " << vec << std::endl;
}
ofs.close();
}

void FastText::saveModel() {
std::ofstream ofs(args_->output + ".bin", std::ofstream::binary);
if (!ofs.is_open()) {
Expand Down Expand Up @@ -365,6 +382,9 @@ void FastText::train(std::shared_ptr<Args> args) {
saveModel();
if (args_->model != model_name::sup) {
saveVectors();
if (args_->saveOutput > 0) {
saveOutput();
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/fasttext.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class FastText {
public:
void getVector(Vector&, const std::string&);
void saveVectors();
void saveOutput();
void saveModel();
void loadModel(const std::string&);
void loadModel(std::istream&);
Expand Down

0 comments on commit 955fbe7

Please sign in to comment.