From d37ed6cb13fc63aa2c81ece8de005bfbea4916be Mon Sep 17 00:00:00 2001 From: Yibing Liu Date: Fri, 15 Dec 2017 07:41:54 +0000 Subject: [PATCH] polish code in reshape_op --- paddle/operators/reshape_op.cc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/paddle/operators/reshape_op.cc b/paddle/operators/reshape_op.cc index 5baf45953..d82d82874 100644 --- a/paddle/operators/reshape_op.cc +++ b/paddle/operators/reshape_op.cc @@ -36,10 +36,13 @@ class ReshapeOp : public framework::OperatorWithKernel { auto x_dims = ctx->GetInputDim("X"); std::vector neg_dims_idx; + // set some dimension to -1 if it is unknown + const int unknown_size = -1; for (size_t i = 0; i < shape.size(); ++i) { - PADDLE_ENFORCE(shape[i] > 0 || shape[i] == -1, - "Each dimension of Attr(shape) must be positive or -1."); - if (shape[i] == -1) { + PADDLE_ENFORCE(shape[i] > 0 || shape[i] == unknown_size, + "Each dimension of Attr(shape) must be positive or %d.", + unknown_size); + if (shape[i] == unknown_size) { neg_dims_idx.push_back(i); PADDLE_ENFORCE(neg_dims_idx.size() <= 1, "Only one dimension of Attr(shape) can be unknown."); @@ -53,8 +56,7 @@ class ReshapeOp : public framework::OperatorWithKernel { // dim infer shape[neg_dims_idx[0]] = in_size / (-capacity); // recalculate capacity - capacity = std::accumulate(shape.begin(), shape.end(), 1, - std::multiplies()); + capacity = shape[neg_dims_idx[0]] * (-capacity); } // capacity check PADDLE_ENFORCE(capacity == in_size, @@ -98,9 +100,9 @@ the tensor X into a 2-D tensor: [[1, 2, 3, 4]] -One dimension in the target shape can be set -1, and the real dimension -will be infered from the original shape of Input(X) and other -dimensions in the target shape. +One dimension in the target shape can be set -1, representing that its +size is unknown. In this case, the real dimension will be infered from +the original shape of Input(X) and other dimensions in the target shape. )DOC"); } }; -- GitLab