array_read编译和运行时返回结果的shape不一致
Created by: Ashleychen
def train_program(self):
loss = fluid.layers.zeros(shape = [1], dtype = 'float')
image_lod = fluid.layers.data(name = 'image_lod', dtype = 'float32',
shape = [784], lod_level = 1)
print('%s' % image_lod)
label_lod = fluid.layers.data(name = 'label_lod', dtype = 'float32',
shape = [1], lod_level = 1)
print('%s' % label_lod)
last_label_lod = fluid.layers.data(name = 'last_label_lod', dtype = 'float32',
shape = [1], lod_level = 1)
image_lod_rank_table = fluid.layers.control_flow.lod_rank_table(image_lod)
label_lod_rank_table = fluid.layers.control_flow.lod_rank_table(label_lod)
last_label_lod_rank_table = fluid.layers.control_flow.lod_rank_table(last_label_lod)
image_array = lod_tensor_to_array(x = image_lod, table = image_lod_rank_table)
label_array = lod_tensor_to_array(x = label_lod, table = label_lod_rank_table)
last_label_array = lod_tensor_to_array(x = last_label_lod, table = last_label_lod_rank_table)
array_len = fluid.layers.fill_constant(
shape = [1], dtype = 'int64', value = self.list_size)
counter = fluid.layers.zeros(shape = [1], dtype = 'int64')
cond = fluid.layers.less_than(x = counter, y = array_len)
while_op = fluid.layers.While(cond = cond)
with while_op.block():
current_image = fluid.layers.array_read(array = image_array, i = counter)
current_label = fluid.layers.array_read(array = label_array, i = counter)
print('%s' % current_label)
# current_label_input = fluid.layers.unsqueeze(
# input = current_label,
# axes = [-1])
# print('%s' % current_label_input)
current_last_label = fluid.layers.array_read(array = last_label_array, i = counter)
image_fc = fluid.layers.fc(input = current_image, size = 20)
last_label_fc = fluid.layers.fc(input = current_label, size = 20)
loss_input = fluid.layers.elementwise_add(
x = image_fc, y = last_label_fc)
current_loss = fluid.layers.softmax_with_cross_entropy(
logits = loss_input, label = current_label)
loss = fluid.layers.elementwise_add(
loss, fluid.layers.reduce_sum(current_loss, dim = 0))
#loss += current_loss
fluid.layers.increment(x = counter, value = 1, in_place = True)
fluid.layers.less_than(x = counter, y = array_len, cond = cond)
return loss
我在跑一个简单的demo,用来测试序列数据的分布处理,发现当序列数据最后一维的大小为1的时候(如我的label数据)array_read运行时返回的结果会将shape的最后一维去掉,如我想要current_label返回的shape为(128,1),实际返回的却是(128),导致我fc的时候报错了:
I0128 13:51:52.005067 11374 mul_op.cc:41] mul operator x.shape=128 y.shape=1, 20 x_num_col_dims=1 y_num_col_dims=1
。
而且这样的话我在调用 current_loss = fluid.layers.softmax_with_cross_entropy(logits = loss_input, label = current_label)
的时候编译的时候没有问题,运行的也是会报错:
Traceback (most recent call last):
File "train_omniglot.py", line 202, in <module>
ntm_main()
File "train_omniglot.py", line 82, in ntm_main
'last_label_lod': last_label_lod})
File "/home/ol/anaconda2/lib/python2.7/site-packages/paddle/fluid/executor.py", line 470, in run
self.executor.run(program.desc, scope, 0, True, True)
paddle.fluid.core.EnforceNotMet: Enforce failed. Expected labels_dims.size() == 2UL, but received labels_dims.size():1 != 2UL:2.
The labels should be a 2-D tensor. at [/paddle/paddle/fluid/operators/softmax_with_cross_entropy_op.cc:106]```
感觉这两个报错都是因为array_read返回结果的shape在编译和运行时不一致导致的。能帮忙解决一下吗?