mirror of
https://github.com/marian-nmt/marian.git
synced 2024-09-17 09:47:34 +03:00
move cuda device into graph. Add EOS, padding, UNK to vocab
This commit is contained in:
parent
728ca05152
commit
2965bde889
@ -37,5 +37,14 @@ std::string Expr::Debug() const
|
|||||||
strm << marian::Debug(shape);
|
strm << marian::Debug(shape);
|
||||||
return strm.str();
|
return strm.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////
|
||||||
|
ExpressionGraph::ExpressionGraph(int cudaDevice)
|
||||||
|
: stack_(new ChainableStack)
|
||||||
|
{
|
||||||
|
std::srand (time(NULL));
|
||||||
|
cudaSetDevice(0);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -38,9 +38,7 @@ class Expr {
|
|||||||
|
|
||||||
class ExpressionGraph {
|
class ExpressionGraph {
|
||||||
public:
|
public:
|
||||||
ExpressionGraph()
|
ExpressionGraph(int cudaDevice);
|
||||||
: stack_(new ChainableStack)
|
|
||||||
{}
|
|
||||||
|
|
||||||
void forward(size_t batchSize) {
|
void forward(size_t batchSize) {
|
||||||
for(auto&& v : *stack_) {
|
for(auto&& v : *stack_) {
|
||||||
|
@ -23,8 +23,6 @@ SGD::SGD(ExpressionGraph& g, float eta,
|
|||||||
|
|
||||||
void SGD::Run()
|
void SGD::Run()
|
||||||
{
|
{
|
||||||
std::srand ( unsigned ( std::time(0) ) );
|
|
||||||
|
|
||||||
size_t numExamples = xData_.size()/ numFeatures_;
|
size_t numExamples = xData_.size()/ numFeatures_;
|
||||||
Tensor xt({(int)maxBatchSize_, (int)numExamples}, 0.0f);
|
Tensor xt({(int)maxBatchSize_, (int)numExamples}, 0.0f);
|
||||||
Tensor yt({(int)maxBatchSize_, (int)numClasses_}, 0.0f);
|
Tensor yt({(int)maxBatchSize_, (int)numClasses_}, 0.0f);
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
#include "vocab.h"
|
#include "vocab.h"
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
cudaSetDevice(0);
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace marian;
|
using namespace marian;
|
||||||
@ -22,7 +21,7 @@ int main(int argc, char** argv) {
|
|||||||
std::vector<Expr> Y;
|
std::vector<Expr> Y;
|
||||||
std::vector<Expr> H;
|
std::vector<Expr> H;
|
||||||
|
|
||||||
ExpressionGraph g;
|
ExpressionGraph g(0);
|
||||||
|
|
||||||
for (int t = 0; t < num_inputs; ++t) {
|
for (int t = 0; t < num_inputs; ++t) {
|
||||||
X.emplace_back(g.input(shape={batch_size, input_size}));
|
X.emplace_back(g.input(shape={batch_size, input_size}));
|
||||||
|
@ -16,7 +16,7 @@ int main(int argc, char** argv) {
|
|||||||
using namespace marian;
|
using namespace marian;
|
||||||
using namespace keywords;
|
using namespace keywords;
|
||||||
|
|
||||||
ExpressionGraph g;
|
ExpressionGraph g(0);
|
||||||
|
|
||||||
Expr x = named(g.input(shape={whatevs, IMAGE_SIZE}), "x");
|
Expr x = named(g.input(shape={whatevs, IMAGE_SIZE}), "x");
|
||||||
Expr y = named(g.input(shape={whatevs, LABEL_SIZE}), "y");
|
Expr y = named(g.input(shape={whatevs, LABEL_SIZE}), "y");
|
||||||
|
@ -10,7 +10,7 @@ const size_t IMAGE_SIZE = 784;
|
|||||||
const size_t LABEL_SIZE = 10;
|
const size_t LABEL_SIZE = 10;
|
||||||
int BATCH_SIZE = 10000;
|
int BATCH_SIZE = 10000;
|
||||||
|
|
||||||
ExpressionGraph build_graph() {
|
ExpressionGraph build_graph(int cudaDevice) {
|
||||||
std::cerr << "Loading model params...";
|
std::cerr << "Loading model params...";
|
||||||
NpzConverter converter("../scripts/test_model_single/model.npz");
|
NpzConverter converter("../scripts/test_model_single/model.npz");
|
||||||
|
|
||||||
@ -22,7 +22,7 @@ ExpressionGraph build_graph() {
|
|||||||
|
|
||||||
std::cerr << "Building model...";
|
std::cerr << "Building model...";
|
||||||
|
|
||||||
ExpressionGraph g;
|
ExpressionGraph g(cudaDevice);
|
||||||
auto x = named(g.input(shape={whatevs, IMAGE_SIZE}), "x");
|
auto x = named(g.input(shape={whatevs, IMAGE_SIZE}), "x");
|
||||||
auto y = named(g.input(shape={whatevs, LABEL_SIZE}), "y");
|
auto y = named(g.input(shape={whatevs, LABEL_SIZE}), "y");
|
||||||
|
|
||||||
@ -46,15 +46,12 @@ ExpressionGraph build_graph() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
|
||||||
cudaSetDevice(1);
|
|
||||||
|
|
||||||
std::cerr << "Loading test set...";
|
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> 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::vector<float> testLabels = datasets::mnist::ReadLabels("../examples/mnist/t10k-labels-idx1-ubyte", BATCH_SIZE, LABEL_SIZE);
|
||||||
std::cerr << "Done." << std::endl;
|
std::cerr << "Done." << std::endl;
|
||||||
|
|
||||||
ExpressionGraph g = build_graph();
|
ExpressionGraph g = build_graph(1);
|
||||||
|
|
||||||
Tensor xt({BATCH_SIZE, IMAGE_SIZE});
|
Tensor xt({BATCH_SIZE, IMAGE_SIZE});
|
||||||
Tensor yt({BATCH_SIZE, LABEL_SIZE});
|
Tensor yt({BATCH_SIZE, LABEL_SIZE});
|
||||||
|
@ -7,9 +7,7 @@ using namespace marian;
|
|||||||
using namespace keywords;
|
using namespace keywords;
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
|
||||||
cudaSetDevice(0);
|
|
||||||
|
|
||||||
const size_t IMAGE_SIZE = 784;
|
const size_t IMAGE_SIZE = 784;
|
||||||
const size_t LABEL_SIZE = 10;
|
const size_t LABEL_SIZE = 10;
|
||||||
const size_t BATCH_SIZE = 24;
|
const size_t BATCH_SIZE = 24;
|
||||||
@ -59,7 +57,7 @@ int main(int argc, char** argv) {
|
|||||||
std::cerr << "\tDone." << std::endl;
|
std::cerr << "\tDone." << std::endl;
|
||||||
|
|
||||||
|
|
||||||
ExpressionGraph g;
|
ExpressionGraph g(0);
|
||||||
|
|
||||||
auto x = g.input(shape={whatevs, IMAGE_SIZE}, name="X");
|
auto x = g.input(shape={whatevs, IMAGE_SIZE}, name="X");
|
||||||
auto y = g.input(shape={whatevs, LABEL_SIZE}, name="Y");
|
auto y = g.input(shape={whatevs, LABEL_SIZE}, name="Y");
|
||||||
|
@ -24,6 +24,21 @@ inline std::vector<std::string> Tokenize(const std::string& str,
|
|||||||
return tokens;
|
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)
|
size_t Vocab::GetOrCreate(const std::string &word)
|
||||||
{
|
{
|
||||||
|
@ -10,6 +10,9 @@ public:
|
|||||||
size_t GetOrCreate(const std::string &word);
|
size_t GetOrCreate(const std::string &word);
|
||||||
std::vector<size_t> ProcessSentence(const std::string &sentence);
|
std::vector<size_t> ProcessSentence(const std::string &sentence);
|
||||||
|
|
||||||
|
size_t GetUNK() const;
|
||||||
|
size_t GetPad() const;
|
||||||
|
size_t GetEOS() const;
|
||||||
protected:
|
protected:
|
||||||
typedef std::unordered_map<std::string, size_t> Coll;
|
typedef std::unordered_map<std::string, size_t> Coll;
|
||||||
Coll coll_;
|
Coll coll_;
|
||||||
|
Loading…
Reference in New Issue
Block a user