Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Add error checking for cpp examples. #13828

Merged
merged 1 commit into from
Jan 17, 2019
Merged
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
8 changes: 6 additions & 2 deletions cpp-package/example/alexnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,14 @@ int main(int argc, char const *argv[]) {
};

auto train_iter = MXDataIter("MNISTIter");
setDataIter(&train_iter, "Train", data_files, batch_size);
if (!setDataIter(&train_iter, "Train", data_files, batch_size)) {
return 1;
Copy link
Member

Choose a reason for hiding this comment

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

Can we have some descriptive error messages?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The error message already printed in check_datafiles()

}

auto val_iter = MXDataIter("MNISTIter");
setDataIter(&val_iter, "Label", data_files, batch_size);
if (!setDataIter(&val_iter, "Label", data_files, batch_size)) {
return 1;
}

Optimizer* opt = OptimizerRegistry::Find("sgd");
opt->SetParam("momentum", 0.9)
Expand Down
8 changes: 6 additions & 2 deletions cpp-package/example/googlenet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,14 @@ int main(int argc, char const *argv[]) {
};

auto train_iter = MXDataIter("MNISTIter");
setDataIter(&train_iter, "Train", data_files, batch_size);
if (!setDataIter(&train_iter, "Train", data_files, batch_size)) {
return 1;
}

auto val_iter = MXDataIter("MNISTIter");
setDataIter(&val_iter, "Label", data_files, batch_size);
if (!setDataIter(&val_iter, "Label", data_files, batch_size)) {
return 1;
}

Optimizer* opt = OptimizerRegistry::Find("sgd");
opt->SetParam("momentum", 0.9)
Expand Down
8 changes: 6 additions & 2 deletions cpp-package/example/inception_bn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,14 @@ int main(int argc, char const *argv[]) {
};

auto train_iter = MXDataIter("MNISTIter");
setDataIter(&train_iter, "Train", data_files, batch_size);
if (!setDataIter(&train_iter, "Train", data_files, batch_size)) {
return 1;
}

auto val_iter = MXDataIter("MNISTIter");
setDataIter(&val_iter, "Label", data_files, batch_size);
if (!setDataIter(&val_iter, "Label", data_files, batch_size)) {
return 1;
}

// initialize parameters
Xavier xavier = Xavier(Xavier::gaussian, Xavier::in, 2);
Expand Down
8 changes: 6 additions & 2 deletions cpp-package/example/lenet_with_mxdataiter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,14 @@ int main(int argc, char const *argv[]) {
};

auto train_iter = MXDataIter("MNISTIter");
setDataIter(&train_iter, "Train", data_files, batch_size);
if (!setDataIter(&train_iter, "Train", data_files, batch_size)) {
return 1;
}

auto val_iter = MXDataIter("MNISTIter");
setDataIter(&val_iter, "Label", data_files, batch_size);
if (!setDataIter(&val_iter, "Label", data_files, batch_size)) {
return 1;
}

Optimizer* opt = OptimizerRegistry::Find("sgd");
opt->SetParam("momentum", 0.9)
Expand Down
8 changes: 6 additions & 2 deletions cpp-package/example/mlp_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ int main(int argc, char** argv) {
};

auto train_iter = MXDataIter("MNISTIter");
setDataIter(&train_iter, "Train", data_files, batch_size);
if (!setDataIter(&train_iter, "Train", data_files, batch_size)) {
return 1;
}

auto val_iter = MXDataIter("MNISTIter");
setDataIter(&val_iter, "Label", data_files, batch_size);
if (!setDataIter(&val_iter, "Label", data_files, batch_size)) {
return 1;
}

auto net = mlp(layers);

Expand Down
8 changes: 6 additions & 2 deletions cpp-package/example/mlp_gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ int main(int argc, char** argv) {
};

auto train_iter = MXDataIter("MNISTIter");
setDataIter(&train_iter, "Train", data_files, batch_size);
if (!setDataIter(&train_iter, "Train", data_files, batch_size)) {
return 1;
}

auto val_iter = MXDataIter("MNISTIter");
setDataIter(&val_iter, "Label", data_files, batch_size);
if (!setDataIter(&val_iter, "Label", data_files, batch_size)) {
return 1;
}

auto net = mlp(layers);

Expand Down
8 changes: 6 additions & 2 deletions cpp-package/example/resnet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,14 @@ int main(int argc, char const *argv[]) {
};

auto train_iter = MXDataIter("MNISTIter");
setDataIter(&train_iter, "Train", data_files, batch_size);
if (!setDataIter(&train_iter, "Train", data_files, batch_size)) {
return 1;
}

auto val_iter = MXDataIter("MNISTIter");
setDataIter(&val_iter, "Label", data_files, batch_size);
if (!setDataIter(&val_iter, "Label", data_files, batch_size)) {
return 1;
}

// initialize parameters
Xavier xavier = Xavier(Xavier::gaussian, Xavier::in, 2);
Expand Down
8 changes: 6 additions & 2 deletions cpp-package/example/test_score.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,14 @@ int main(int argc, char** argv) {
};

auto train_iter = MXDataIter("MNISTIter");
setDataIter(&train_iter, "Train", data_files, batch_size);
if (!setDataIter(&train_iter, "Train", data_files, batch_size)) {
return 1;
}

auto val_iter = MXDataIter("MNISTIter");
setDataIter(&val_iter, "Label", data_files, batch_size);
if (!setDataIter(&val_iter, "Label", data_files, batch_size)) {
return 1;
}

auto net = mlp(layers);

Expand Down
5 changes: 3 additions & 2 deletions cpp-package/example/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ bool check_datafiles(const std::vector<std::string> &data_files) {
}
}
return true;
}
}

bool setDataIter(MXDataIter *iter , const std::string &useType,
const std::vector<std::string> &data_files, int batch_size) {
if (!check_datafiles(data_files))
if (!check_datafiles(data_files)) {
return false;
}

iter->SetParam("batch_size", batch_size);
iter->SetParam("shuffle", 1);
Expand Down