tensorflow: 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?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 2
- Comments: 25 (7 by maintainers)
Commits related to this issue
- [tf.contrib.seq2seq] Better docstrings for AttentionWrapper and BeamSearchDecoder. Fixes #9832, #12569. PiperOrigin-RevId: 170553460 — committed to sb2nov/tensorflow by ebrevdo 7 years ago
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!
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 notifications@github.com wrote:
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 thisfinal_shapeparameter isfinal_shape=[static_batch_size, beam_width]andstatic_batch_size = tensor_util.constant_value(batch_size),static_batch_sizeis remained asNoneif the shape of the input batch size is not specified. Therefore, reshaping tensor as[None, beam_width]fails.Added documentation and an example to
BeamSearchDecoder,AttentionWrapper.__init__, andAttentionWrapper.zero_state. Should show up in a day or two.On Fri, Sep 29, 2017 at 1:35 PM, Sumeet Singh notifications@github.com wrote:
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.