BeamSearchDecoder not working
I intalled TensorFlow from 2017-05-10 nightly binaries. The TensorFlow version is v1.1.0-rc2-773-g7fa0cf3 1.1.0-rc2. I have CUDA 8.0 and a Tesla K20c with 4.5G memory. My OS is Linux Ubuntu 14.04.
I tried to run the following code to test the latest BeamSearchDecoder:
lstm = rnn.OutputProjectionWrapper(
rnn.LayerNormBasicLSTMCell(n_hidden, dropout_keep_prob=keep_prob), n_classes)
infer_decoder = BeamSearchDecoder(lstm,
embedding=lambda tokens:tf.nn.embedding_lookup(embedding_matrix, tokens),
start_tokens=start_tokens, end_token=EOS, initial_state=encoder_state, beam_width=5)
decoder_outputs_infer, decoder_state_infer, decoder_seq_infer = dynamic_decode(infer_decoder)
But I got :
Traceback (most recent call last):
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/python/framework/tensor_util.py", line 458, in make_tensor_proto
str_values = [compat.as_bytes(x) for x in proto_values]
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/python/framework/tensor_util.py", line 458, in <listcomp>
str_values = [compat.as_bytes(x) for x in proto_values]
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/python/util/compat.py", line 65, in as_bytes
(bytes_or_text,))
TypeError: Expected binary or unicode string, got None
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/anxf/test_beam.py", line 127, in <module>
decoder_outputs_infer, decoder_state_infer, decoder_seq_infer = dynamic_decode(infer_decoder)
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/contrib/seq2seq/python/ops/decoder.py", line 286, in dynamic_decode
swap_memory=swap_memory)
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2705, in while_loop
result = context.BuildLoop(cond, body, loop_vars, shape_invariants)
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2534, in BuildLoop
pred, body, original_loop_vars, loop_vars, shape_invariants)
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/python/ops/control_flow_ops.py", line 2484, in _BuildLoop
body_result = body(*packed_vars_for_body)
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/contrib/seq2seq/python/ops/decoder.py", line 234, in body
decoder_finished) = decoder.step(time, inputs, state)
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/contrib/seq2seq/python/ops/beam_search_decoder.py", line 437, in step
length_penalty_weight=length_penalty_weight)
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/contrib/seq2seq/python/ops/beam_search_decoder.py", line 516, in _beam_search_step
final_shape=[static_batch_size, beam_width])
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/contrib/seq2seq/python/ops/beam_search_decoder.py", line 638, in _tensor_gather_helper
output = array_ops.reshape(output, final_shape)
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2548, in reshape
name=name)
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/python/framework/op_def_library.py", line 493, in apply_op
raise err
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/python/framework/op_def_library.py", line 490, in apply_op
preferred_dtype=default_dtype)
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/python/framework/ops.py", line 714, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/python/framework/constant_op.py", line 113, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/python/framework/constant_op.py", line 102, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "/home/anxf/.local/lib/python3.4/site-packages/tensorflow/python/framework/tensor_util.py", line 462, in make_tensor_proto
"supported type." % (type(values), values))
TypeError: Failed to convert object of type <class 'list'> to Tensor. Contents: [None, 5]. Consider casting elements to a supported type.
Process finished with exit code 1
I wonder how I can fix this problem? Or does this mean BeamSearchDecoder is still testing and does not work right now?
I encountered exactly the same problem. Help would be much appreciated
I encountered the same issue, and I found out this error is gone when the batch size of the input of BeamSearchDecoder is specified. This error is due to the line 638 of beam_search_decoder.py,
output = array_ops.reshape(output, final_shape).
Since this final_shape parameter is final_shape=[static_batch_size, beam_width] and static_batch_size = tensor_util.constant_value(batch_size), static_batch_size is remained as None if the shape of the input batch size is not specified. Therefore, reshaping tensor as [None, beam_width] fails.
as @shuuki4 mentioned, one way to solve this is to reshape as [-1, beam_width] when batch size is dynamic(None).
Thanks for reporting this. I'll try to get a fix out on Monday.
On May 12, 2017 7:25 PM, "allen" [email protected] wrote:
as @shuuki4 https://github.com/shuuki4 mentioned, one way to solve this is to reshape as [-1, beam_width] when batch size is dyanmic(None).
— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/tensorflow/tensorflow/issues/9832#issuecomment-301219688, or mute the thread https://github.com/notifications/unsubscribe-auth/ABtim8jZICuw9wHLvY-zgpH3C-CpS9jJks5r5RScgaJpZM4NXsUR .
Hello, was wondering if there were there any updates on this? Also running into the same issue. Thanks!
Fix is in the queue, but I don't think we're pushing any code over the long weekend. Will likely show up by end of day Tuesday.
On May 27, 2017 3:10 AM, "classicCoder16" [email protected] wrote:
Hello, was wondering if there were there any updates on this? Also running into the same issue. Thanks!
— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/tensorflow/tensorflow/issues/9832#issuecomment-304442694, or mute the thread https://github.com/notifications/unsubscribe-auth/ABtim5on9_9pW8I2fcPQ4E2NSMC9g_e1ks5r9_aHgaJpZM4NXsUR .
Great, thanks! A couple of quick follow-up questions:
-
When using BeamSearchDecoder with
dynamic_decode, it seems that we can't pass inimpute_finished=Truewithout obtaining an error -- is this expected? -
Also, BeamSearchDecoder appears to expect
initial_stateto have a shape whose first dimension isbatch_size*beam_size. However, it is not entirely clear to me how you would form this initial state, say, from the last hidden state of an encoder, which would have a first dimension ofbatch_size.Would you simply tile the encoded hidden statebeam_sizenumber of times? Or perhaps perform some type of interleaving?
Some clarification on this would be much appreciated!
I meet the same problem with @classicCoder16. I used a bi-direction rnn as encode, right now I use following code to get the initial state.
initial_state = self.dec_cell.zero_state(dtype=tf.float32, batch_size = self.batch_size * self.beam_size)
fw,bw = self.encode_state
fw = tf.contrib.seq2seq.tile_batch(fw,multiplier=self.beam_size)
bw = tf.contrib.seq2seq.tile_batch(bw,multiplier=self.beam_size)
self.initial_state = initial_state.clone(cell_state=(fw,bw))
But when I ran the dynamic decode, I can't get correct beam search result. Maybe my code is not right. Has anyone tried the BeamSearchDecoder and got the correct result? Would you please share the code with me? My tensorFlow version is 1.2.0-rc0. Thanks!
Agreed, would love to see a piece of example code that illustrates the proper way to use the BeamSearchDecoder.
Struggling with the same issues as @xueyouluo and @classicCoder16. I have been working on resolving this problem for some time with no success and some sample code would be incredibly helpful! Thanks in advance
@ebrevdo Thanks for your help thus far! I've been looking over the implementation of the tests in beam_search_decoder_test.py and they are quite helpful. However, I have a bit of a fundamental question: when using dynamic_decode with the BeamSearchDecoder, it seems that the final_outputs that is returned has an attribute predicted_ids whose shape is (batch_size, T, beam_width), which are the final sequences found by beam search.
How then do we go from final_outputs.predicted_ids to the actual most probable sequence for each example in the batch, to therefore obtain something of size (batch_size, T)?
Thanks in advance!
classicCoder: the BeamSearchDecoder uses tf.nn.top_k to update the beams at each iteration (top_k by default returns sorted values). The final computation uses GatherTree to identify the true indices at each time step. This means that the 0th beam is the one with the highest score; and you should be able to use predicted_ids[:, :, 0] to access it.
On Sun, Jun 4, 2017 at 2:49 PM, classicCoder16 [email protected] wrote:
@ebrevdo https://github.com/ebrevdo Thanks for your help thus far! I've been looking over the implementation of the tests in beam_search_decoder_test.py and they are quite helpful. However, I have a bit of a fundamental question: when using dynamic_decode with the BeamSearchDecoder, it seems that the final_outputs that is returned has an attribute predicted_ids whose shape is (batch_size, T, beam_width), which are the final sequences found by beam search.
How then do we go from final_outputs.predicted_ids to the actual most probable sequence for each example in the batch, to therefore obtain something of size (batch_size, T)?
Thanks in advance!
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tensorflow/tensorflow/issues/9832#issuecomment-306069454, or mute the thread https://github.com/notifications/unsubscribe-auth/ABtim8viN2KW4FrFKMd1WHS9PJPfU9Gcks5sAyZagaJpZM4NXsUR .
@ebrevdo I'm looking to incorporate BeamSearchDecoder into a project I'm working on as soon as possible. Have all the fixes been pushed/is there anything still in the pipeline to be done that I should be aware of when using the current state of the code? Thanks!
@lmkostas I think you can use the code of master branch to implement BeamSearchDecoder, and it works.
Here's an example including beam search. The trick is to use the tile_batch method to replicate the input to the starting state.
https://github.com/pplantinga/tensorflow-examples/blob/master/TensorFlow%201.2%20seq2seq%20example.ipynb
I have run into the same problem with the dynamic input size, has this issue been solved? Is there any official workaround for this problem?
This thread has many issues. Can you open a new one with code that replicates your error? Using the latest tf nightly?
I have the same problem with @classicCoder16:
When using BeamSearchDecoder with dynamic_decode, it seems that we can't pass in impute_finished=True without obtaining an error -- is this expected?
Of course, with impute_finished=False, the error is gone and the result seems to be reasonable.
I'm using tf 1.2.1 on Mac OS X.
Also the BeamSearchDecoder does not work with the multilayer cell wrapper, as the first dimension of the cell wrapper is number of layers. But BeamSearchDecoder expect initial_state to have batch_size*beam_size as the first dim.
Steven, please open a new issue for this. We'll keep track of it separately.
On Fri, Sep 15, 2017 at 6:55 AM, Steven Ding [email protected] wrote:
Also the BeamSearchDecoder does not work with the multilayer cell wrapper, as the first dimension of the cell wrapper is number of layers. But BeamSearchDecoder expect initial_state to have batch_size*beam_size as the first dim.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tensorflow/tensorflow/issues/9832#issuecomment-329789609, or mute the thread https://github.com/notifications/unsubscribe-auth/ABtim39OugI3d4QoQa29xV6UiubDbm-pks5sioHVgaJpZM4NXsUR .