move cuda device into graph. Add EOS, padding, UNK to vocab

This commit is contained in:
Hieu Hoang 2016-09-16 14:04:15 +02:00
parent 728ca05152
commit 2965bde889
9 changed files with 36 additions and 19 deletions

View File

@ -38,4 +38,13 @@ std::string Expr::Debug() const
return strm.str();
}
///////////////////////////////////////////////////////
ExpressionGraph::ExpressionGraph(int cudaDevice)
: stack_(new ChainableStack)
{
std::srand (time(NULL));
cudaSetDevice(0);
}
}

View File

@ -38,9 +38,7 @@ class Expr {
class ExpressionGraph {
public:
ExpressionGraph()
: stack_(new ChainableStack)
{}
ExpressionGraph(int cudaDevice);
void forward(size_t batchSize) {
for(auto&& v : *stack_) {

View File

@ -23,8 +23,6 @@ SGD::SGD(ExpressionGraph& g, float eta,
void SGD::Run()
{
std::srand ( unsigned ( std::time(0) ) );
size_t numExamples = xData_.size()/ numFeatures_;
Tensor xt({(int)maxBatchSize_, (int)numExamples}, 0.0f);
Tensor yt({(int)maxBatchSize_, (int)numClasses_}, 0.0f);

View File

@ -4,7 +4,6 @@
#include "vocab.h"
int main(int argc, char** argv) {
cudaSetDevice(0);
using namespace std;
using namespace marian;
@ -22,7 +21,7 @@ int main(int argc, char** argv) {
std::vector<Expr> Y;
std::vector<Expr> H;
ExpressionGraph g;
ExpressionGraph g(0);
for (int t = 0; t < num_inputs; ++t) {
X.emplace_back(g.input(shape={batch_size, input_size}));

View File

@ -16,7 +16,7 @@ int main(int argc, char** argv) {
using namespace marian;
using namespace keywords;
ExpressionGraph g;
ExpressionGraph g(0);
Expr x = named(g.input(shape={whatevs, IMAGE_SIZE}), "x");
Expr y = named(g.input(shape={whatevs, LABEL_SIZE}), "y");

View File

@ -10,7 +10,7 @@ const size_t IMAGE_SIZE = 784;
const size_t LABEL_SIZE = 10;
int BATCH_SIZE = 10000;
ExpressionGraph build_graph() {
ExpressionGraph build_graph(int cudaDevice) {
std::cerr << "Loading model params...";
NpzConverter converter("../scripts/test_model_single/model.npz");
@ -22,7 +22,7 @@ ExpressionGraph build_graph() {
std::cerr << "Building model...";
ExpressionGraph g;
ExpressionGraph g(cudaDevice);
auto x = named(g.input(shape={whatevs, IMAGE_SIZE}), "x");
auto y = named(g.input(shape={whatevs, LABEL_SIZE}), "y");
@ -46,15 +46,12 @@ ExpressionGraph build_graph() {
}
int main(int argc, char** argv) {
cudaSetDevice(1);
std::cerr << "Loading test set...";
std::vector<float> testImages = datasets::mnist::ReadImages("../examples/mnist/t10k-images-idx3-ubyte", BATCH_SIZE, IMAGE_SIZE);
std::vector<float> testLabels = datasets::mnist::ReadLabels("../examples/mnist/t10k-labels-idx1-ubyte", BATCH_SIZE, LABEL_SIZE);
std::cerr << "Done." << std::endl;
ExpressionGraph g = build_graph();
ExpressionGraph g = build_graph(1);
Tensor xt({BATCH_SIZE, IMAGE_SIZE});
Tensor yt({BATCH_SIZE, LABEL_SIZE});

View File

@ -8,8 +8,6 @@ using namespace keywords;
int main(int argc, char** argv) {
cudaSetDevice(0);
const size_t IMAGE_SIZE = 784;
const size_t LABEL_SIZE = 10;
const size_t BATCH_SIZE = 24;
@ -59,7 +57,7 @@ int main(int argc, char** argv) {
std::cerr << "\tDone." << std::endl;
ExpressionGraph g;
ExpressionGraph g(0);
auto x = g.input(shape={whatevs, IMAGE_SIZE}, name="X");
auto y = g.input(shape={whatevs, LABEL_SIZE}, name="Y");

View File

@ -24,6 +24,21 @@ inline std::vector<std::string> Tokenize(const std::string& str,
return tokens;
}
////////////////////////////////////////////////////////
size_t Vocab::GetUNK() const
{
return std::numeric_limits<size_t>::max();
}
size_t Vocab::GetPad() const
{
return std::numeric_limits<size_t>::max() - 1;
}
size_t Vocab::GetEOS() const
{
return std::numeric_limits<size_t>::max() - 2;
}
size_t Vocab::GetOrCreate(const std::string &word)
{

View File

@ -10,6 +10,9 @@ public:
size_t GetOrCreate(const std::string &word);
std::vector<size_t> ProcessSentence(const std::string &sentence);
size_t GetUNK() const;
size_t GetPad() const;
size_t GetEOS() const;
protected:
typedef std::unordered_map<std::string, size_t> Coll;
Coll coll_;