Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

Infer interface's unexpected behavior

Open liuyuuan opened this issue 9 years ago • 9 comments

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:

  1. For example, my batch size of inference is 2, and I expect 2 sequences with length of 10 and 11, with the infer interface, I get a output of length 21, the 2 output sequences are concatenated to 1.
  2. 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.

liuyuuan avatar Sep 27 '17 03:09 liuyuuan

The way you call the infer interface is right. There is other bug in the script.

lcy-seso avatar Sep 27 '17 10:09 lcy-seso

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.

lcy-seso avatar Sep 27 '17 12:09 lcy-seso

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.

lcy-seso avatar Sep 27 '17 12:09 lcy-seso

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

lcy-seso avatar Sep 27 '17 12:09 lcy-seso

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.

liuyuuan avatar Sep 27 '17 16:09 liuyuuan

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.

liuyuuan avatar Sep 28 '17 05:09 liuyuuan

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.

chengduoZH avatar Sep 28 '17 05:09 chengduoZH

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.

lcy-seso avatar Sep 28 '17 05:09 lcy-seso

The problem has been solved.

chengduoZH avatar Oct 22 '17 14:10 chengduoZH