mirror of
https://github.com/marian-nmt/marian.git
synced 2024-09-17 09:47:34 +03:00
std::accumuate -> GetTotalSize()
This commit is contained in:
commit
81f6f51f6f
@ -59,8 +59,7 @@ inline std::vector<T> Tokenize( const std::string &input
|
|||||||
|
|
||||||
void Tensor::Load(const std::string &path)
|
void Tensor::Load(const std::string &path)
|
||||||
{
|
{
|
||||||
size_t totSize = std::accumulate(pimpl_->shape().begin(), pimpl_->shape().end(),
|
size_t totSize = GetTotalSize(pimpl_->shape());
|
||||||
1, std::multiplies<int>());
|
|
||||||
cerr << "totSize=" << totSize << endl;
|
cerr << "totSize=" << totSize << endl;
|
||||||
std::vector<float> hostData(totSize);
|
std::vector<float> hostData(totSize);
|
||||||
|
|
||||||
|
22
src/tensor.h
22
src/tensor.h
@ -48,6 +48,13 @@ inline std::string Debug(const Shape &shape)
|
|||||||
return strm.str();
|
return strm.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline size_t GetTotalSize(const Shape &shape)
|
||||||
|
{
|
||||||
|
size_t ret = std::accumulate(shape.begin(), shape.end(),
|
||||||
|
1, std::multiplies<int>());
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
template<class Float>
|
template<class Float>
|
||||||
class TensorImpl {
|
class TensorImpl {
|
||||||
private:
|
private:
|
||||||
@ -81,8 +88,7 @@ class TensorImpl {
|
|||||||
|
|
||||||
std::cerr << "Allocating : " << shape[0] << " " << shape[1] << std::endl;
|
std::cerr << "Allocating : " << shape[0] << " " << shape[1] << std::endl;
|
||||||
|
|
||||||
int size = std::accumulate(shape_.begin(), shape_.end(),
|
int size = GetTotalSize(shape_);
|
||||||
1, std::multiplies<int>());
|
|
||||||
data_.resize(size, value);
|
data_.resize(size, value);
|
||||||
cudnnCreateTensorDescriptor(&desc_);
|
cudnnCreateTensorDescriptor(&desc_);
|
||||||
switch (shape_.size()) {
|
switch (shape_.size()) {
|
||||||
@ -153,8 +159,7 @@ class TensorImpl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void set(const std::vector<Float> &values) {
|
void set(const std::vector<Float> &values) {
|
||||||
size_t totSize = std::accumulate(shape().begin(), shape().end(),
|
size_t totSize = GetTotalSize(shape());
|
||||||
1, std::multiplies<int>());
|
|
||||||
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(values.begin(), values.end(), data_.begin());
|
||||||
@ -164,7 +169,14 @@ class TensorImpl {
|
|||||||
{
|
{
|
||||||
std::stringstream strm;
|
std::stringstream strm;
|
||||||
assert(shape_.size());
|
assert(shape_.size());
|
||||||
strm << "shape=" << marian::Debug(shape_);
|
strm << "shape=" << marian::Debug(shape_) << std::endl;
|
||||||
|
|
||||||
|
// values
|
||||||
|
/*
|
||||||
|
size_t totSize = GetTotalSize(shape());
|
||||||
|
std::vector<Float> values(totSize);
|
||||||
|
thrust::copy(data_.begin(), data_.end(), values.begin());
|
||||||
|
*/
|
||||||
return strm.str();
|
return strm.str();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
29
src/test.cu
29
src/test.cu
@ -22,11 +22,10 @@ int main(int argc, char** argv) {
|
|||||||
Expr b = param(shape={1, LABEL_SIZE}, name="b0");
|
Expr b = param(shape={1, LABEL_SIZE}, name="b0");
|
||||||
|
|
||||||
auto scores = dot(x, w) + b;
|
auto scores = dot(x, w) + b;
|
||||||
auto lr = softmax_fast(scores, axis=1, name="pred");
|
auto lr = softmax(scores, axis=1, name="pred");
|
||||||
auto graph = -mean(sum(y * log(lr), axis=1), axis=0, name="cost");
|
auto graph = -mean(sum(y * log(lr), axis=1), axis=0, name="cost");
|
||||||
cerr << "lr=" << lr.Debug() << endl;
|
cerr << "lr=" << lr.Debug() << endl;
|
||||||
|
|
||||||
#if 0
|
|
||||||
int numofdata;
|
int numofdata;
|
||||||
vector<float> images = datasets::mnist::ReadImages("../examples/mnist/t10k-images-idx3-ubyte", numofdata, IMAGE_SIZE);
|
vector<float> images = datasets::mnist::ReadImages("../examples/mnist/t10k-images-idx3-ubyte", numofdata, IMAGE_SIZE);
|
||||||
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);
|
||||||
@ -41,34 +40,16 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
cerr << "tx=" << tx.Debug() << endl;
|
cerr << "tx=" << tx.Debug() << endl;
|
||||||
cerr << "ty=" << ty.Debug() << endl;
|
cerr << "ty=" << ty.Debug() << endl;
|
||||||
#else
|
|
||||||
Tensor tx({500, 784}, 1);
|
|
||||||
Tensor ty({500, 10}, 1);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
x = tx;
|
x = tx;
|
||||||
y = ty;
|
y = ty;
|
||||||
|
|
||||||
graph.forward(500);
|
graph.forward(500);
|
||||||
|
|
||||||
std::cerr << "Result: ";
|
std::cerr << "scores: " << Debug(scores.val().shape()) << endl;
|
||||||
for (auto val : scores.val().shape()) {
|
std::cerr << "lr: " << Debug(lr.val().shape()) << endl;
|
||||||
std::cerr << val << " ";
|
std::cerr << "Log-likelihood: " << Debug(graph.val().shape()) << endl ;
|
||||||
}
|
|
||||||
std::cerr << std::endl;
|
|
||||||
std::cerr << "Result: ";
|
|
||||||
for (auto val : lr.val().shape()) {
|
|
||||||
std::cerr << val << " ";
|
|
||||||
}
|
|
||||||
std::cerr << std::endl;
|
|
||||||
lr.val().Print();
|
|
||||||
std::cerr << "Log-likelihood: ";
|
|
||||||
for (auto val : graph.val().shape()) {
|
|
||||||
std::cerr << val << " ";
|
|
||||||
}
|
|
||||||
std::cerr << std::endl;
|
|
||||||
graph.val().Print();
|
|
||||||
|
|
||||||
graph.backward();
|
graph.backward();
|
||||||
|
|
||||||
//std::cerr << graph["pred"].val()[0] << std::endl;
|
//std::cerr << graph["pred"].val()[0] << std::endl;
|
||||||
|
Loading…
Reference in New Issue
Block a user