Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

fluid layers.reshape如何输入shape

Open yangjing14 opened this issue 7 years ago • 10 comments

构建的部分网络结构如下,其中input是一个3d的tensor,shape为(batch_size, num_vector, vector_dim),想将input reshape到二维,不知道shape怎么给比较合适,按现在的代码会报错。

def nw_2dvector_to_1dvector(input, vector_dim): shape = fluid.layers.shape(input) batch_size, num_vectors = shape[0], shape[1] reshaped = fluid.layers.reshape(input, shape=[batch_size * num_vectors, vector_dim]) fc = fluid.layers.fc(input=reshaped, size=HIDDEN1, act='relu') reshaped2 = fluid.layers.reshape(fc, shape=[batch_size, num_vectors, HIDDEN1]) reduced = fluid.layers.reduce_mean(reshaped2, dim=0) fc2 = fluid.layers.fc(input=reduced, size=HIDDEN1, act='relu') return fc2

错误信息: Traceback (most recent call last): File "onehot_train_paddle_lod.py", line 342, in train(use_cuda, USE_PRE_TRAIN_EMBEDDING) File "onehot_train_paddle_lod.py", line 239, in train predict, label, avg_cost, acc = network_onehot_plus_2dvector() File "onehot_train_paddle_lod.py", line 172, in network_onehot_plus_2dvector fc1_vec = nw_2dvector_to_1dvector(vector, 100) File "onehot_train_paddle_lod.py", line 152, in nw_2dvector_to_1dvector batch_size, num_vectors = shape[0], shape[1] TypeError: 'Variable' object does not support indexing

  • 版本、环境信息:    1)PaddlePaddle版本:1.3.2-gpu    3)GPU:Tesla P40, CUDA9.0    4)系统环境:CentOS,Python版本3.5.2

yangjing14 avatar Apr 22 '19 05:04 yangjing14

shape = fluid.layers.shape(input) 改成 shape = input.shape 试下

heavengate avatar Apr 22 '19 05:04 heavengate

shape = fluid.layers.shape(input) 改成 shape = input.shape 试下

试了一下,前面这个问题确实没有了,但是后面第二个reshape的地方还是报错 Traceback (most recent call last): File "onehot_train_paddle_lod.py", line 343, in train(use_cuda, USE_PRE_TRAIN_EMBEDDING) File "onehot_train_paddle_lod.py", line 240, in train predict, label, avg_cost, acc = network_onehot_plus_2dvector() File "onehot_train_paddle_lod.py", line 173, in network_onehot_plus_2dvector fc1_vec = nw_2dvector_to_1dvector(vector, 100) File "onehot_train_paddle_lod.py", line 156, in nw_2dvector_to_1dvector reshaped2 = fluid.layers.reshape(fc, shape=[batch_size, num_vectors, HIDDEN1]) File "/usr/local/lib/python3.5/dist-packages/paddle/fluid/layers/nn.py", line 5979, in reshape "Only one dimension in shape can be unknown.") AssertionError: Only one dimension in shape can be unknown.

yangjing14 avatar Apr 22 '19 05:04 yangjing14

这个报错说明input.shape里读取的batch_size和num_vectors都是-1, 应该是传入的input之前做过第二的reshape操作之类的,把batch_size和num_vectors其中有一个指定为具体值就可以了

heavengate avatar Apr 22 '19 05:04 heavengate

这个报错说明input.shape里读取的batch_size和num_vectors都是-1, 应该是传入的input之前做过第二的reshape操作之类的,把batch_size和num_vectors其中有一个指定为具体值就可以了

Batch size理论上是可以指定的,但是不太清楚怎么处理最后一个batch大小不等于batch size的情况

yangjing14 avatar Apr 22 '19 06:04 yangjing14

这个报错说明input.shape里读取的batch_size和num_vectors都是-1, 应该是传入的input之前做过第二的reshape操作之类的,把batch_size和num_vectors其中有一个指定为具体值就可以了

Batch size理论上是可以指定的,但是不太清楚怎么处理最后一个batch大小不等于batch size的情况

一般选择drop_last或者padding的方式

heavengate avatar Apr 22 '19 06:04 heavengate

这个报错说明input.shape里读取的batch_size和num_vectors都是-1, 应该是传入的input之前做过第二的reshape操作之类的,把batch_size和num_vectors其中有一个指定为具体值就可以了

Batch size理论上是可以指定的,但是不太清楚怎么处理最后一个batch大小不等于batch size的情况

一般选择drop_last或者padding的方式

请问两个选项在哪里指定?

yangjing14 avatar Apr 22 '19 06:04 yangjing14

https://github.com/PaddlePaddle/Paddle/blob/24923f760497e6b0ae9abbc96382e55fa218ffa9/python/paddle/batch.py#L18

这个报错说明input.shape里读取的batch_size和num_vectors都是-1, 应该是传入的input之前做过第二的reshape操作之类的,把batch_size和num_vectors其中有一个指定为具体值就可以了

Batch size理论上是可以指定的,但是不太清楚怎么处理最后一个batch大小不等于batch size的情况

一般选择drop_last或者padding的方式

请问两个选项在哪里指定?

https://github.com/PaddlePaddle/Paddle/blob/24923f760497e6b0ae9abbc96382e55fa218ffa9/python/paddle/batch.py#L18

heavengate avatar Apr 22 '19 06:04 heavengate

reshaped2 = fluid.layers.reshape(fc, shape=[BATCH_SIZE, -1, HIDDEN1]) 固定batch_size后reshape报一下错误

Traceback (most recent call last): File "onehot_train_paddle_lod.py", line 345, in train(use_cuda, USE_PRE_TRAIN_EMBEDDING) File "onehot_train_paddle_lod.py", line 242, in train predict, label, avg_cost, acc = network_onehot_plus_2dvector() File "onehot_train_paddle_lod.py", line 175, in network_onehot_plus_2dvector fc1_vec = nw_2dvector_to_1dvector(vector, 100) File "onehot_train_paddle_lod.py", line 158, in nw_2dvector_to_1dvector reshaped2 = fluid.layers.reshape(fc, shape=[BATCH_SIZE, -1, HIDDEN1]) File "/usr/local/lib/python3.5/dist-packages/paddle/fluid/layers/nn.py", line 5998, in reshape "XShape": x_shape}) File "/usr/local/lib/python3.5/dist-packages/paddle/fluid/layer_helper.py", line 56, in append_op return self.main_program.current_block().append_op(*args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/paddle/fluid/framework.py", line 1317, in append_op attrs=kwargs.get("attrs", None)) File "/usr/local/lib/python3.5/dist-packages/paddle/fluid/framework.py", line 727, in init self.desc.infer_shape(self.block.desc) paddle.fluid.core.EnforceNotMet: Enforce failed. Expected output_shape[unk_dim_idx] * capacity == -in_size, but received output_shape[unk_dim_idx] * capacity:0 != -in_size:-512.

yangjing14 avatar Apr 22 '19 08:04 yangjing14

另外想请教一下如何在运行时动态的获取Tensor实际的shape?(不是定义网络结构的时候给的shape)

yangjing14 avatar Apr 22 '19 08:04 yangjing14

另外想请教一下如何在运行时动态的获取Tensor实际的shape?(不是定义网络结构的时候给的shape)

fluid.layers.shape

heavengate avatar Apr 22 '19 11:04 heavengate