From 8dcdf8f28a9bd619a6d67929d0947517945801a1 Mon Sep 17 00:00:00 2001 From: Hieu Hoang Date: Tue, 13 Sep 2016 18:55:48 +0200 Subject: [PATCH 1/3] set value in tensor --- src/tensor.cu | 12 +++++++++--- src/tensor.h | 6 ++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/tensor.cu b/src/tensor.cu index 4711e62b..95c684b3 100644 --- a/src/tensor.cu +++ b/src/tensor.cu @@ -59,23 +59,29 @@ inline std::vector Tokenize( const std::string &input void Tensor::Load(const std::string &path) { + size_t totSize = std::accumulate(pimpl_->shape().begin(), pimpl_->shape().end(), + 1, std::multiplies()); + cerr << "totSize=" << totSize << endl; + std::vector hostData(totSize); + fstream strm; strm.open(path.c_str()); - size_t lineNum = 0; string line; + size_t ind = 0; while ( getline (strm, line) ) { cerr << line << '\n'; vector toks = Tokenize(line); for (size_t i = 0; i < toks.size(); ++i) { - pimpl_->set(toks[i], lineNum, i); + hostData[ind] = toks[i]; } - ++lineNum; + ++ind; } strm.close(); + } } diff --git a/src/tensor.h b/src/tensor.h index fea9398c..e8ff92bf 100644 --- a/src/tensor.h +++ b/src/tensor.h @@ -152,10 +152,8 @@ class TensorImpl { thrust::fill(data_.begin(), data_.end(), value); } - void set(value_type value, size_t x, size_t y) { - assert(shape().size() == 2); - size_t sizeRow = sizeof(Float) * shape()[1]; - data_[x + sizeRow * y] = value; + void set(const std::vector &values) { + thrust::copy(values.begin(), values.end(), data_.begin()); } std::string Debug() const From 482c0df90a14e83fddca26202d83c8eabbc60250 Mon Sep 17 00:00:00 2001 From: Hieu Hoang Date: Tue, 13 Sep 2016 19:07:12 +0200 Subject: [PATCH 2/3] load --- marian/.project | 5 +++++ src/tensor.cu | 5 +++++ src/tensor.h | 1 + src/test.cu | 7 +++++++ 4 files changed, 18 insertions(+) diff --git a/marian/.project b/marian/.project index 215485f6..e5b195c5 100644 --- a/marian/.project +++ b/marian/.project @@ -85,6 +85,11 @@ 1 PARENT-1-PROJECT_LOC/src/marian.h + + mnist.h + 1 + PARENT-1-PROJECT_LOC/src/mnist.h + tensor.cu 1 diff --git a/src/tensor.cu b/src/tensor.cu index 95c684b3..6048bb43 100644 --- a/src/tensor.cu +++ b/src/tensor.cu @@ -81,7 +81,12 @@ void Tensor::Load(const std::string &path) } strm.close(); + Load(hostData); +} +void Tensor::Load(const std::vector &values) +{ + pimpl_->set(values); } } diff --git a/src/tensor.h b/src/tensor.h index e8ff92bf..a801cd2a 100644 --- a/src/tensor.h +++ b/src/tensor.h @@ -245,6 +245,7 @@ class Tensor { } void Load(const std::string &path); + void Load(const std::vector &values); }; diff --git a/src/test.cu b/src/test.cu index ed43f052..1e07bdb3 100644 --- a/src/test.cu +++ b/src/test.cu @@ -27,6 +27,11 @@ int main(int argc, char** argv) { Tensor tx({500, 784}, 1); Tensor ty({500, 10}, 1); + + int numImg, imgSize; + vector images = datasets::mnist::ReadImages("../examples/mnist/t10k-images-idx3-ubyte", numImg, imgSize); + vector labels = datasets::mnist::ReadLabels("../examples/mnist/t10k-labels-idx1-ubyte"); + cerr << "tx=" << tx.Debug() << endl; cerr << "ty=" << ty.Debug() << endl; @@ -56,6 +61,7 @@ int main(int argc, char** argv) { //std::cerr << graph["pred"].val()[0] << std::endl; */ + // XOR Expr x = input(shape={whatevs, 2}, name="X"); Expr y = input(shape={whatevs, 2}, name="Y"); @@ -77,6 +83,7 @@ int main(int argc, char** argv) { tx.Load("../examples/xor/train.txt"); ty.Load("../examples/xor/label.txt"); + #if 0 hook0(graph); graph.autodiff(); From 93eb3ca7abd7b11bd5043b955ef384ed52ac742d Mon Sep 17 00:00:00 2001 From: Hieu Hoang Date: Tue, 13 Sep 2016 19:13:49 +0200 Subject: [PATCH 3/3] debug --- src/tensor.h | 6 +++++- src/test.cu | 9 ++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/tensor.h b/src/tensor.h index a801cd2a..2cf4c267 100644 --- a/src/tensor.h +++ b/src/tensor.h @@ -153,7 +153,11 @@ class TensorImpl { } void set(const std::vector &values) { - thrust::copy(values.begin(), values.end(), data_.begin()); + size_t totSize = std::accumulate(shape().begin(), shape().end(), + 1, std::multiplies()); + std::cerr << "totSize=" << totSize << " " << values.size() << std::endl; + assert(totSize == values.size()); + thrust::copy(values.begin(), values.end(), data_.begin()); } std::string Debug() const diff --git a/src/test.cu b/src/test.cu index 1e07bdb3..d87c9d1a 100644 --- a/src/test.cu +++ b/src/test.cu @@ -12,7 +12,7 @@ int main(int argc, char** argv) { using namespace marian; using namespace keywords; - /* + Expr x = input(shape={whatevs, 784}, name="X"); Expr y = input(shape={whatevs, 10}, name="Y"); @@ -31,6 +31,8 @@ int main(int argc, char** argv) { int numImg, imgSize; vector images = datasets::mnist::ReadImages("../examples/mnist/t10k-images-idx3-ubyte", numImg, imgSize); vector labels = datasets::mnist::ReadLabels("../examples/mnist/t10k-labels-idx1-ubyte"); + tx.Load(images); + //ty.Load(labels); cerr << "tx=" << tx.Debug() << endl; cerr << "ty=" << ty.Debug() << endl; @@ -59,9 +61,10 @@ int main(int argc, char** argv) { graph.backward(); //std::cerr << graph["pred"].val()[0] << std::endl; - */ + // XOR + /* Expr x = input(shape={whatevs, 2}, name="X"); Expr y = input(shape={whatevs, 2}, name="Y"); @@ -82,7 +85,7 @@ int main(int argc, char** argv) { tx.Load("../examples/xor/train.txt"); ty.Load("../examples/xor/label.txt"); - + */ #if 0 hook0(graph);