diff --git a/cpp-package/example/mlp_csv.cpp b/cpp-package/example/mlp_csv.cpp new file mode 100644 index 000000000000..8aec4b76d917 --- /dev/null +++ b/cpp-package/example/mlp_csv.cpp @@ -0,0 +1,272 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Example: mlp_csv + * Description: + * The following example demonstrates how to use CSVIter. This example creates + * mlp (multi-layer perceptron) model and trains the MNIST data which is in + * CSV format. + */ +#include +#include "utils.h" +#include "mxnet-cpp/MxNetCpp.h" + +using namespace mxnet::cpp; + +/* + * Implementing the mlp symbol with given hidden units configuration. + */ +Symbol mlp(const std::vector &hidden_units) { + auto data = Symbol::Variable("data"); + auto label = Symbol::Variable("label"); + + std::vector weights(hidden_units.size()); + std::vector biases(hidden_units.size()); + std::vector outputs(hidden_units.size()); + + for (size_t i = 0; i < hidden_units.size(); ++i) { + weights[i] = Symbol::Variable("w" + std::to_string(i)); + biases[i] = Symbol::Variable("b" + std::to_string(i)); + Symbol fc = FullyConnected( + i == 0? data : outputs[i-1], // data + weights[i], + biases[i], + hidden_units[i]); + outputs[i] = i == hidden_units.size()-1 ? fc : Activation(fc, ActivationActType::kRelu); + } + return SoftmaxOutput(outputs.back(), label); +} + +/* + * Convert the input string of number of hidden units into the vector of integers. + */ +std::vector getLayers(const std::string& hidden_units_string) { + std::vector hidden_units; + char *pNext; + int num_unit = strtol(hidden_units_string.c_str(), &pNext, 10); + hidden_units.push_back(num_unit); + while (*pNext) { + num_unit = strtol(pNext, &pNext, 10); + hidden_units.push_back(num_unit); + } + return hidden_units; +} + +void printUsage() { + std::cout << "Usage:" << std::endl; + std::cout << "mlp_csv --train mnist_training_set.csv --test mnist_test_set.csv --epochs 10 " + << "--batch_size 100 --hidden_units \"128 64 64\" [--gpu]" << std::endl; + std::cout << "The example uses mnist data in CSV format. The MNIST data in CSV format assumes " + << "the column 0 to be label and the rest 784 column to be data." << std::endl; + std::cout << "By default, the example uses 'cpu' context. If '--gpu' is specified, " + << "program uses 'gpu' context." <