mirror of
https://github.com/marian-nmt/marian.git
synced 2024-11-09 13:09:29 +03:00
batches
This commit is contained in:
parent
a573eecf5c
commit
f0f0dbe9ee
@ -80,12 +80,12 @@ void Tensor::Load(const std::string &path)
|
|||||||
}
|
}
|
||||||
strm.close();
|
strm.close();
|
||||||
|
|
||||||
Load(hostData);
|
Load(hostData.begin(), hostData.begin());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tensor::Load(const std::vector<float> &values)
|
void Tensor::Load(const std::vector<float>::const_iterator &begin, const std::vector<float>::const_iterator &end)
|
||||||
{
|
{
|
||||||
pimpl_->set(values);
|
pimpl_->set(begin, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
10
src/tensor.h
10
src/tensor.h
@ -158,11 +158,11 @@ class TensorImpl {
|
|||||||
thrust::fill(data_.begin(), data_.end(), value);
|
thrust::fill(data_.begin(), data_.end(), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set(const std::vector<Float> &values) {
|
void set(const std::vector<float>::const_iterator &begin, const std::vector<float>::const_iterator &end) {
|
||||||
size_t totSize = GetTotalSize(shape());
|
size_t totSize = GetTotalSize(shape());
|
||||||
std::cerr << "tensor size=" << totSize << " vector size=" << values.size() << std::endl;
|
//std::cerr << "tensor size=" << totSize << " vector size=" << values.size() << std::endl;
|
||||||
assert(totSize == values.size());
|
//assert(totSize == values.size());
|
||||||
thrust::copy(values.begin(), values.end(), data_.begin());
|
thrust::copy(begin, end, data_.begin());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Debug() const
|
std::string Debug() const
|
||||||
@ -275,7 +275,7 @@ class Tensor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Load(const std::string &path);
|
void Load(const std::string &path);
|
||||||
void Load(const std::vector<float> &values);
|
void Load(const std::vector<float>::const_iterator &begin, const std::vector<float>::const_iterator &end);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
55
src/test.cu
55
src/test.cu
@ -12,6 +12,7 @@ int main(int argc, char** argv) {
|
|||||||
using namespace marian;
|
using namespace marian;
|
||||||
using namespace keywords;
|
using namespace keywords;
|
||||||
|
|
||||||
|
const size_t BATCH_SIZE = 500;
|
||||||
const size_t IMAGE_SIZE = 784;
|
const size_t IMAGE_SIZE = 784;
|
||||||
const size_t LABEL_SIZE = 10;
|
const size_t LABEL_SIZE = 10;
|
||||||
|
|
||||||
@ -31,41 +32,51 @@ int main(int argc, char** argv) {
|
|||||||
//vector<float> labels = datasets::mnist::ReadLabels("../examples/mnist/t10k-labels-idx1-ubyte", numofdata, LABEL_SIZE);
|
//vector<float> labels = datasets::mnist::ReadLabels("../examples/mnist/t10k-labels-idx1-ubyte", numofdata, LABEL_SIZE);
|
||||||
vector<float> images = datasets::mnist::ReadImages("../examples/mnist/train-images-idx3-ubyte", numofdata, IMAGE_SIZE);
|
vector<float> images = datasets::mnist::ReadImages("../examples/mnist/train-images-idx3-ubyte", numofdata, IMAGE_SIZE);
|
||||||
vector<float> labels = datasets::mnist::ReadLabels("../examples/mnist/train-labels-idx1-ubyte", numofdata, LABEL_SIZE);
|
vector<float> labels = datasets::mnist::ReadLabels("../examples/mnist/train-labels-idx1-ubyte", numofdata, LABEL_SIZE);
|
||||||
|
|
||||||
|
|
||||||
cerr << "images=" << images.size() << " labels=" << labels.size() << endl;
|
cerr << "images=" << images.size() << " labels=" << labels.size() << endl;
|
||||||
cerr << "numofdata=" << numofdata << endl;
|
cerr << "numofdata=" << numofdata << endl;
|
||||||
|
|
||||||
Tensor tx({numofdata, IMAGE_SIZE}, 1);
|
size_t startInd = 0;
|
||||||
Tensor ty({numofdata, LABEL_SIZE}, 1);
|
size_t startIndData = 0;
|
||||||
|
while (startInd < numofdata) {
|
||||||
|
size_t batchSize = (startInd + BATCH_SIZE < numofdata) ? BATCH_SIZE : numofdata - startInd;
|
||||||
|
cerr << "startInd=" << startInd
|
||||||
|
<< " startIndData=" << startIndData
|
||||||
|
<< " batchSize=" << batchSize << endl;
|
||||||
|
|
||||||
tx.Load(images);
|
Tensor tx({numofdata, IMAGE_SIZE}, 1);
|
||||||
ty.Load(labels);
|
Tensor ty({numofdata, LABEL_SIZE}, 1);
|
||||||
|
|
||||||
//cerr << "tx=" << Debug(tx.shape()) << endl;
|
tx.Load(images.begin() + startIndData, images.begin() + startIndData + batchSize * IMAGE_SIZE);
|
||||||
//cerr << "ty=" << Debug(ty.shape()) << endl;
|
ty.Load(labels.begin() + startInd, labels.begin() + startInd + batchSize);
|
||||||
|
|
||||||
x = tx;
|
//cerr << "tx=" << Debug(tx.shape()) << endl;
|
||||||
y = ty;
|
//cerr << "ty=" << Debug(ty.shape()) << endl;
|
||||||
|
|
||||||
cerr << "x=" << Debug(x.val().shape()) << endl;
|
x = tx;
|
||||||
cerr << "y=" << Debug(y.val().shape()) << endl;
|
y = ty;
|
||||||
|
|
||||||
|
cerr << "x=" << Debug(x.val().shape()) << endl;
|
||||||
|
cerr << "y=" << Debug(y.val().shape()) << endl;
|
||||||
|
|
||||||
|
|
||||||
graph.forward(500);
|
graph.forward(batchSize);
|
||||||
|
|
||||||
cerr << "w=" << Debug(w.val().shape()) << endl;
|
cerr << "w=" << Debug(w.val().shape()) << endl;
|
||||||
cerr << "b=" << Debug(b.val().shape()) << endl;
|
cerr << "b=" << Debug(b.val().shape()) << endl;
|
||||||
std::cerr << "z: " << Debug(z.val().shape()) << endl;
|
std::cerr << "z: " << Debug(z.val().shape()) << endl;
|
||||||
std::cerr << "lr: " << Debug(lr.val().shape()) << endl;
|
std::cerr << "lr: " << Debug(lr.val().shape()) << endl;
|
||||||
std::cerr << "Log-likelihood: " << Debug(graph.val().shape()) << endl ;
|
std::cerr << "Log-likelihood: " << Debug(graph.val().shape()) << endl ;
|
||||||
|
|
||||||
//std::cerr << "scores=" << scores.val().Debug() << endl;
|
//std::cerr << "scores=" << scores.val().Debug() << endl;
|
||||||
std::cerr << "lr=" << lr.val().Debug() << endl;
|
std::cerr << "lr=" << lr.val().Debug() << endl;
|
||||||
|
|
||||||
graph.backward();
|
graph.backward();
|
||||||
|
|
||||||
//std::cerr << graph["pred"].val()[0] << std::endl;
|
//std::cerr << graph["pred"].val()[0] << std::endl;
|
||||||
|
|
||||||
|
startInd += batchSize;
|
||||||
|
startIndData += batchSize * IMAGE_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// XOR
|
// XOR
|
||||||
|
Loading…
Reference in New Issue
Block a user