nmt icon indicating copy to clipboard operation
nmt copied to clipboard

shape issue in the dynamic_decode with GreedyEmbeddingHelper

Open Ushiao opened this issue 8 years ago • 11 comments

Hello,

i'm trying dynamic_decode with GreedyEmbeddingHelper, but i got a error of shape:

ValueError: The shape for decoder_1/decoder/while/Merge_7:0 is not an invariant for the loop. It enters the loop with shape (1, 128), but has shape (?, 128) after one iteration. Provide shape invariants using either the shape_invariants` argument of tf.while_loop or set_shape() on the loop variables. PS. 128 is embedding size

i tried batch_size with 2, work fine, but not with size 1.

Ushiao avatar Sep 19 '17 17:09 Ushiao

hi, i have the same problem. did you get the solutions?

bob831009 avatar Nov 13 '17 17:11 bob831009

I got this exact same problem.

tohnperfect avatar Nov 15 '17 23:11 tohnperfect

Can you provide a command that can regenerate the issue with this codebase? Also python version and tensorflow version may also help.

oahziur avatar Nov 16 '17 03:11 oahziur

I use python3 + tensorflow1.4. And i got the same problem. Did you get solutions?

puzzledTao avatar Feb 28 '18 07:02 puzzledTao

python 3.6 + tf 1.5 same here, batch_size = 2 works fine and batch_size = 1 raises the error

ziruizhuang avatar Mar 07 '18 06:03 ziruizhuang

Same issue. Also, how does one perform inference for a single sequence if the greedy embedding helper does not accept batch size 1?

him593 avatar May 14 '18 10:05 him593

This workaround fixes batch size 1 (batch size 0 is still broken):

class FixedHelper(seq2seq.GreedyEmbeddingHelper):
    def sample(self, *args, **kwargs):
        result = super().sample(*args, **kwargs)
        result.set_shape([batch_size])
        return result

tavianator avatar May 25 '18 04:05 tavianator

@tavianator Is batch_size = 1 for this case? I tried the value but it was already 1 and got same error

result = Tensor("decoder/decoder/while/BasicDecoderStep/ArgMax:0", shape=(1,), dtype=int32) befor set_shape

trungd avatar Jun 27 '18 12:06 trungd

Same issue. Also, how does one perform inference for a single sequence if the greedy embedding helper does not accept batch size 1?

I think the best way to do that is to create a dummy sequence, like an empty one and then pass both of the single sentence appended by the empty one to the greedy embedding

roholazandie avatar Jan 17 '19 20:01 roholazandie

I'm also having exactly this problem, with python 2.7.12 and tf 1.13.1. I don't seem to get the FixedHelper workaround mentioned by @tavianator to work.. When replacing result = super().sample(*args, **kwargs) with result = super(tf.contrib.seq2seq.GreedyEmbeddingHelper, self).sample(*args, **kwargs), i get "AttributeError: 'NoneType' object has no attribute 'set_shape'" I used a batch size of 1.

@roholazandie, how could I best proceed adding this empty embedding? Should it be added to the initial_state of the following BasicDecoder, which is followed by dynamic_decode?

MarcGroef avatar Jul 24 '19 19:07 MarcGroef

I found a solution to this problem, at this link.

The way to go is to set the start tokens in the following manner: batch_size = tf.shape(self.inputs)[0:1] start_tokens = tf.ones(batch_size, dtype = tf.int32) * <your start token idx>

Here self.inputs refers to the input tensor [edits: markup]

MarcGroef avatar Aug 12 '19 22:08 MarcGroef