Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce_sum如何只输出常量,不要带维度 #16507

Closed
yeyupiaoling opened this issue Mar 28, 2019 · 9 comments
Closed

reduce_sum如何只输出常量,不要带维度 #16507

yeyupiaoling opened this issue Mar 28, 2019 · 9 comments

Comments

@yeyupiaoling
Copy link
Contributor

yeyupiaoling commented Mar 28, 2019

环境

  • PaddlePaddle (develop)
  • Ubuntu 16.04
  • Python 3.5

问题

我希望得到的是通过reduce_sum接口后得到一个常量,但是这接口得到的是维度为1的。

num_valid = fluid.layers.reduce_sum(valid_inds)

因为我下面需要计算

    # 选取70%的数据
    num_keep_radio = 0.7
    keep_num = fluid.layers.squeeze(fluid.layers.cast(num_valid * num_keep_radio, dtype='int32'), axes=[])

num_valid的信息:

name: "reduce_sum_0.tmp_0"
type {
  type: LOD_TENSOR
  lod_tensor {
    tensor {
      data_type: FP32
      dims: 1
    }
  }
}
persistable: false

会报错:

C++ Callstacks: 
The shape information must be set by Attr(shape). at [/paddle/paddle/fluid/operators/reshape_op.cc:37]
PaddlePaddle Call Stacks: 

项目代码:https://github.com/yeyupiaoling/TestMTCNN/blob/a04126361fa0918f78420e3ce29c77bf8b97aede/train/model.py#L157-L159

@Yancey1989
Copy link
Contributor

scalar也是一个dim=[1]的tensor,并且squeeze layer是从input 中去掉axes指定纬度的数据。

另外请问下“# 选取70%的数据” 是指什么方式选取,希望的输出是什么样子的呢?

@yeyupiaoling
Copy link
Contributor Author

@Yancey1989

另外请问下“# 选取70%的数据” 是指什么方式选取,希望的输出是什么样子的呢?

这个是MTCNN模型的是否有人脸的分类交叉熵函数,在训练过程中,为了取得更好的效果,作者每次只后向传播前70%样本的梯度,这样来保证传递的都是有效的数字。

@yeyupiaoling
Copy link
Contributor Author

@Yancey1989 如果修改成以下这样,还是报错

    # 选取70%的数据
    num_keep_radio = 0.7
    num_valid = fluid.layers.squeeze(input=num_valid, axes=[])
    keep_num = fluid.layers.squeeze(fluid.layers.cast(num_valid * num_keep_radio, dtype='int32'), axes=[])

报错如下:

    keep_num = fluid.layers.squeeze(fluid.layers.cast(num_keep_radio * num_valid, dtype='int32'), axes=[])
  File "/home/test/PaddlePaddle_Python3.5/lib/python3.5/site-packages/paddle/fluid/layers/math_op_patch.py", line 137, in __impl__
    (len(self.shape), len(other_var.shape)))
AssertionError: The rank of the first argument of an binary operator cannot be smaller than the rank of its second argument: 0 vs 1

这个rank到底是指什么?还有项目中直接执行tain_PNet.py接口运行本项目

@Yancey1989
Copy link
Contributor

Yancey1989 commented Mar 28, 2019

这个rank到底是指什么?

原因在:num_valid = fluid.layers.squeeze(input=num_valid, axes=[]) 输出的num_valid的shape已经是0了。所以在num_keep_radio * num_valid 这个 elementwise_mul 会报错。

C++ Callstacks:
The shape information must be set by Attr(shape). at [/paddle/paddle/fluid/operators/reshape_op.cc:37]
PaddlePaddle Call Stacks:

这个报错的原因应该是reshape op没有指定shape吧?

@yeyupiaoling
Copy link
Contributor Author

yeyupiaoling commented Mar 28, 2019

@Yancey1989 如果不设置的话就会报一开始的错误,

如果我修改成如下的话,报的错跟一开始的错误。

    # 选取70%的数据
    num_keep_radio = fluid.layers.fill_constant(shape=[1], dtype='float32', value=0.7)
    print(num_keep_radio)
    print(num_valid)
    keep_num = fluid.layers.squeeze(fluid.layers.cast(num_valid * num_keep_radio, dtype='int32'), axes=[])

@Yancey1989
Copy link
Contributor

>>> num_keep_radio = fluid.layers.fill_constant(shape=[1], dtype='float32', value=0.7)
>>> valid_inds=fluid.layers.data(name="x", shape=[10])
>>> num_valid = fluid.layers.reduce_sum(valid_inds)
>>> keep_num = fluid.layers.squeeze(fluid.layers.cast(num_valid * num_keep_radio, dtype='int32'), axes=[])

这个是正常的,没有报错。

还没报更一开始的错误。

更一开始的错误是什么呢?

@yeyupiaoling
Copy link
Contributor Author

@Yancey1989

更一开始的错误是什么呢?

已经修改:报的错跟一开始的错误。

@yeyupiaoling
Copy link
Contributor Author

@Yancey1989 我再琢磨琢磨吧。谢谢

@zhwesky2010
Copy link
Contributor

@yeyupiaoling
你好,飞桨将于2.5版本全面支持0维Tensor,上述反馈的0D问题也将同时解决。

欢迎试用并安装develop 版本的paddle whl包,将体验最新的0维特性:https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/develop/install/pip/linux-pip.html
安装命令:
pip install paddlepaddle-gpu==0.0.0.post112 -f https://www.paddlepaddle.org.cn/whl/linux/gpu/develop.html


Hello, Paddle will fully support the 0-dimensional Tensor in version 2.5, and the 0D issue will also be resolved at the same time.
Welcome to try and install the develop version of the paddle whl package, and you will experience the latest 0-dimensional features: https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/develop/install/pip/linux -pip.html
Install Command:
pip install paddlepaddle-gpu==0.0.0.post112 -f https://www.paddlepaddle.org.cn/whl/linux/gpu/develop.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants