SINGA-204 Support the training of feed-forward neural nets Merge commits from #xiezl (zhongle) for adding CMake for examples. To test the cifar10 exmaple, 1. mkdir build && cd build 2. cmake .. && make 3. cd ../examples/cifar10 4. export LD_LIBRARY_PATH=../../build/lib:$LD_LIBRARY_PATH 5. ./run.sh
Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/4db968c2 Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/4db968c2 Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/4db968c2 Branch: refs/heads/dev Commit: 4db968c2ec003a051118cf4b49f6ae10cfbc054f Parents: 97648a6 fce5d81 Author: Wei Wang <[email protected]> Authored: Mon Jun 27 19:55:46 2016 +0800 Committer: Wei Wang <[email protected]> Committed: Mon Jun 27 19:55:46 2016 +0800 ---------------------------------------------------------------------- CMakeLists.txt | 3 ++- examples/CMakeLists.txt | 8 ++++++++ examples/cifar10/alexnet.cc | 6 ++---- examples/cifar10/cifar10.h | 6 +++--- examples/cifar10/make.sh | 1 - examples/cifar10/run.sh | 2 ++ src/model/feed_forward_net.cc | 4 +++- src/model/layer/cudnn_convolution.cc | 4 ++-- 8 files changed, 22 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/4db968c2/CMakeLists.txt ---------------------------------------------------------------------- diff --cc CMakeLists.txt index 7a5caf3,21d25cf..ff94d4e --- a/CMakeLists.txt +++ b/CMakeLists.txt @@@ -20,7 -20,7 +20,7 @@@ INCLUDE_DIRECTORIES(${SINGA_INCLUDE_DIR OPTION(USE_CBLAS "Use CBlas libs" ON) OPTION(USE_CUDA "Use Cuda libs" ON) --OPTION(USE_CUDNN "Use Cudnn libs" OFF) ++OPTION(USE_CUDNN "Use Cudnn libs" ON) OPTION(USE_OPENCV "Use opencv" OFF) OPTION(USE_LMDB "Use LMDB libs" OFF) OPTION(USE_PYTHON "Generate py wrappers" OFF) http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/4db968c2/examples/cifar10/alexnet.cc ---------------------------------------------------------------------- diff --cc examples/cifar10/alexnet.cc index 81713f1,d6541a3..5175f49 --- a/examples/cifar10/alexnet.cc +++ b/examples/cifar10/alexnet.cc @@@ -84,12 -84,12 +84,10 @@@ LayerConf GenDenseConf(string name, in conf.set_type("Dense"); DenseConf *dense = conf.mutable_dense_conf(); dense->set_num_output(num_output); -- FillerConf *bias = dense->mutable_bias_filler(); ParamSpec *wspec = conf.add_param(); wspec->set_name(name + "_weight"); wspec->set_decay_mult(wd); -- auto wfill = wspec->mutable_filler(); wfill->set_type("Gaussian"); wfill->set_std(std); @@@ -155,16 -155,14 +153,16 @@@ void Train(float lr, int num_epoch, str train_y = train.second; auto test = data.ReadTestData(); nsamples = test.first.shape(0); - auto maty = + auto mtest = Reshape(test.first, Shape{nsamples, test.first.Size() / nsamples}); - SubRow(mean, &maty); - test_x = Reshape(maty, test.first.shape()); + SubRow(mean, &mtest); + test_x = Reshape(mtest, test.first.shape()); test_y = test.second; } + CHECK_EQ(train_x.shape(0), train_y.shape(0)); + CHECK_EQ(test_x.shape(0), test_y.shape(0)); LOG(INFO) << "Training samples = " << train_y.shape(0) - << " Test samples = " << test_y.shape(0); - << " Test samples =" << test_y.shape(0); ++ << ", Test samples = " << test_y.shape(0); auto net = CreateNet(); SGD sgd; OptimizerConf opt_conf; @@@ -177,10 -175,9 +175,10 @@@ return 0.001; else if (step <= 130) return 0.0001; -- else if (step <= 140) ++ else return 0.00001; }); + SoftmaxCrossEntropy loss; Accuracy acc; net.Compile(true, &sgd, &loss, &acc); http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/4db968c2/examples/cifar10/cifar10.h ---------------------------------------------------------------------- diff --cc examples/cifar10/cifar10.h index 5af54e2,7f10153..d2b9225 --- a/examples/cifar10/cifar10.h +++ b/examples/cifar10/cifar10.h @@@ -68,11 -70,11 +68,11 @@@ const std::pair<Tensor, Tensor> Cifar10 char image[kImageVol]; float float_image[kImageVol]; int tmplabels[kBatchSize]; -- for (int itemid = 0; itemid < kBatchSize; ++itemid) { ++ for (size_t itemid = 0; itemid < kBatchSize; ++itemid) { // LOG(INFO) << "reading " << itemid << "-th image"; ReadImage(&data_file, &label, image); -- for (int i = 0; i < kImageVol; i++) - float_image[i] = static_cast<float>(static_cast<int>(image[i])); ++ for (size_t i = 0; i < kImageVol; i++) + float_image[i] = static_cast<float>(static_cast<uint8_t>(image[i])); images.CopyDataFromHostPtr(float_image, kImageVol, itemid * kImageVol); tmplabels[itemid] = label; } @@@ -80,10 -82,10 +80,10 @@@ return std::make_pair(images, labels); } -const std::pair<Tensor, Tensor> Cifar10::ReadTrainData(bool shuffle) { +const std::pair<Tensor, Tensor> Cifar10::ReadTrainData() { Tensor images(Shape{kBatchSize * kTrainFiles, 3, kImageSize, kImageSize}); Tensor labels(Shape{kBatchSize * kTrainFiles}, kInt); -- for (int fileid = 0; fileid < kTrainFiles; ++fileid) { ++ for (size_t fileid = 0; fileid < kTrainFiles; ++fileid) { string file = "data_batch_" + std::to_string(fileid + 1) + ".bin"; const auto ret = ReadFile(file); CopyDataToFrom(&images, ret.first, ret.first.Size(), http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/4db968c2/examples/cifar10/make.sh ---------------------------------------------------------------------- diff --cc examples/cifar10/make.sh index 5a41612,5a41612..0000000 deleted file mode 100755,100755 --- a/examples/cifar10/make.sh +++ /dev/null @@@ -1,1 -1,1 +1,0 @@@ --g++ -g --std=c++11 alexnet.cc -o alexnet -I../../include -I../../build/include -I/home/wangwei/local/cudnn5/include -I/home/wangwei/local/include -I/usr/local/cuda/include/ -I../../lib/cnmem/include -L../../build/lib/ -lsinga_core -lsinga_model -lsinga_utils -lcudart -lcublas -lcurand -lcudnn -L/home/wangwei/local/cudnn5/lib64 -L/usr/local/cuda/lib64 ../../build/lib/libproto.a -lprotobuf http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/4db968c2/examples/cifar10/run.sh ---------------------------------------------------------------------- diff --cc examples/cifar10/run.sh index 0000000,0000000..c01ec18 new file mode 100755 --- /dev/null +++ b/examples/cifar10/run.sh @@@ -1,0 -1,0 +1,2 @@@ ++#!/usr/bin/env sh ++../../build/bin/alexnet -epoch 140 http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/4db968c2/src/model/feed_forward_net.cc ---------------------------------------------------------------------- diff --cc src/model/feed_forward_net.cc index 30d030a,e682918..ebbe00c --- a/src/model/feed_forward_net.cc +++ b/src/model/feed_forward_net.cc @@@ -202,8 -201,8 +203,9 @@@ const std::pair<float, float> FeedForwa const Tensor FeedForwardNet::Forward(int flag, const Tensor& data) { Tensor input = data, output; for (auto layer : layers_) { +// LOG(INFO) << layer->name() << ": " << input.L1(); output = layer->Forward(flag, input); + // LOG(INFO) << layer->name() << ": " << output.L2(); input = output; } return output; http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/4db968c2/src/model/layer/cudnn_convolution.cc ---------------------------------------------------------------------- diff --cc src/model/layer/cudnn_convolution.cc index 33f2cf8,3dca28a..82cf4e5 --- a/src/model/layer/cudnn_convolution.cc +++ b/src/model/layer/cudnn_convolution.cc @@@ -72,8 -72,8 +72,8 @@@ void CudnnConvolution::InitCudnn(const num_filters_, conv_height_, conv_width_)); if (bias_term_) CUDNN_CHECK(cudnnSetTensor4dDescriptor(bias_desc_, CUDNN_TENSOR_NCHW, - GetCudnnDataType(dtype), 1, num_filters_, 1, - 1)); - GetCudnnDataType(dtype), 1, 1, 1, - num_filters_)); ++ GetCudnnDataType(dtype), 1, ++ num_filters_, 1, 1)); CUDNN_CHECK(cudnnSetConvolution2dDescriptor(conv_desc_, pad_h_, pad_w_, stride_h_, stride_w_, 1, 1, CUDNN_CROSS_CORRELATION));
