Infer interface's unexpected behavior
I'm implementing a pointer network with PaddlePaddle, which does a softmax over a sequence, and the picks the index with the maximum probability.
Now I want to output the sequence after the sequence_softmax is done. Below is my code:
paddle.init(use_gpu=True, trainer_count=1)
out_layer = my_network(args)
reader = my_dataset.create_reader()
infer_batch_reader = paddle.batch(reader=reader, batch_size=2)
parameters = paddle.parameters.Parameters.from_tar(gzip.open(model_file, 'r'))
inferer = paddle.inference.Inference(output_layer=out_layer, parameters=parameters)
for i, batch in enumerate(infer_batch_reader()):
result = inferer.infer(input=batch, flatten_result=False, field=['value'])
logger.info('result detail: {}\n{}'.format(type(res), res))
...
and the code in my_network is simplified below:
p1 = layer.fc(input=some_feature_sequence, size=1, act=act.SequenceSoftmax())
if is_infer:
# here is original logic
# pos = layer.pooling(input=p1, pooling_type=paddle.pooling.Max(output_max_index=True))
# return pos
return p1
My problem is:
- For example, my batch size of inference is
2, and I expect 2 sequences with length of10and11, with theinferinterface, I get a output of length21, the 2 output sequences are concatenated to 1. - If I use the commented out codes in
my_network, i.e, outputpos, which is not a sequence, and is holding the position index with max prob, when the batch size is 2, I got two outputs: 5 and 15, while the expected indices should be 5 and 6, the 2nd even exceeds the second sequence length, it seems the 2 outputs are all computed over the concatenated sequence, although I need more conformation on this.
The way you call the infer interface is right. There is other bug in the script.
I am sure the way you call infer is correct, and the infer interface returns correct results.
I use GDB to print the calculation results in the C++ end. The layer you specified as the output layer DOES NOT return a sequence as you wanted. This means the bug is caused by your network topology definition. Please check your network define.
For this:
For example, my batch size of inference is 2, and I expect 2 sequences with a length of 10 and 11, with the infer interface, I get an output of length 21, the 2 output sequences are concatenated to 1.
This is not a bug or a problem, this is exactly WHAT PaddlePaddle returns. We HAVE already explained the this in our document http://doc.paddlepaddle.org/develop/doc_cn/faq/index_cn.html#infer-layer.
You can also check every demo in Paddle models repo.
If I use the commented out codes in my_network, i.e, output pos, which is not a sequence, and is holding the position index with max prob, when the batch size is 2, I got two outputs: 5 and 15, while the expected indices should be 5 and 6, the 2nd even exceeds the second sequence length, it seems the 2 outputs are all computed over the concatenated sequence, although I need more conformation on this.
This is also not a problem or a bug. Please read our document about the height of the output matrix: @http://doc.paddlepaddle.org/develop/doc_cn/faq/index_cn.html#infer-layer
If I use the commented out codes in my_network, i.e, output pos, which is not a sequence, and is holding the position index with max prob, when the batch size is 2, I got two outputs: 5 and 15, while the expected indices should be 5 and 6, the 2nd even exceeds the second sequence length, it seems the 2 outputs are all computed over the concatenated sequence, although I need more conformation on this.
This is also not a problem or a bug. Please read our document about the height of the output matrix: @http://doc.paddlepaddle.org/develop/doc_cn/faq/index_cn.html#infer-layer
Thanks @lcy-seso , I've checked the referenced docs and aware that my 1st problem is just PaddlePaddle's designed behavior, while the 2nd problem still exists, and let me rephrase the problem: When I do a max pooling on a sequence, and output the max index instead of max value of the sequence, I get a index larger than the sequence length.
To target the problem, I've built a very simple network:
a = paddle.layer.embedding(input=a_ids, size=emb_dim)
a_prob = paddle.layer.fc(size=1, act=act.SequenceSoftmax(), input=a)
if is_infer:
a_max = paddle.layer.pooling(input=a_prob, pooling_type=pooling.Max(output_max_index=True))
return a_max
when doing infer, I can still get indices larger than sequence length, when batch size larger than 1.
when doing infer, I can still get indices larger than sequence length, when batch size larger than 1
I noticed that the index I got is actually the real index with an offset, which is the sum of the former sequences' lengths in the batch, just want to conform if this is also a designed behavior of PaddlePaddle.
when doing infer, I can still get indices larger than sequence length, when batch size larger than 1.
The index returned in paddle is the index in batch.
Because samples in a batch are contiguous in memory. Paddle just return you the entire batch. It doesn't care to how split the batch into a single sample.
The problem has been solved.