Tensorflow basic_rnn_seq2seq TypeError: Expected int32, got -0.1 of type 'float' instead

邮差的信 提交于 2019-12-10 18:48:38

问题


I am trying to run the encoder-decoder model on the dataset. Below is the sample code:

self._input_data = tf.placeholder(tf.int32, [batch_size, num_steps])
self._targets = tf.placeholder(tf.int32, [batch_size, num_steps])
enc_inputs.append(self._input_data) #one batch at once
dec_inputs.append(self._targets)
model = seq2seq.basic_rnn_seq2seq(enc_inputs, dec_inputs, tf.nn.rnn_cell.BasicLSTMCell(size, state_is_tuple=True))

I get an error of type mismatch (mentioned below). Does anyone know to solve the issue?

tensor_util.py, line 290, in _AssertCompatible
    (dtype.name, repr(mismatch), type(mismatch).__name__))
    TypeError: Expected int32, got -0.1 of type 'float' instead.

回答1:


This is an issue of confusing error message. The actual cause is, when you call tf.get_variable() but do not set the default initializer, the error message will be confusing. You can use a tf.zero_initializer() or something like that to suppress this error.




回答2:


The root case of the issue is in Tensorflow rnn_cell.py class:

with vs.variable_scope(scope or "Linear"):
    matrix = vs.get_variable("Matrix", [total_arg_size, output_size], dtype=dtype)

where you have two variable with different types (tf.int32 and tf.float32)

To solve the problem, I used tf.float32 for encoder and decoder inputs, while keeping targets as tf.int32 (required by Seq2Seq model).

Something like this may work:

self._input_data = tf.placeholder(tf.float32, [batch_size, num_steps])
self._targets = tf.placeholder(tf.int32, [batch_size, num_steps])
enc_inputs.append(self._input_data) #one batch at once
dec_inputs.append(self._targets)

Note that the issue was reproduced on TF v0.12.1. I checked the current master for rnn_cell.py and it's quite different. So I assume that issue may go away in later releases.



来源:https://stackoverflow.com/questions/38695086/tensorflow-basic-rnn-seq2seq-typeerror-expected-int32-got-0-1-of-type-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!