在LSTMCell上调用dynamic_rnn时出现TypeError

时间:2016-09-12 16:19:28

标签: python tensorflow lstm

我对使用TensorFlow感到很陌生,我的问题可能很容易解决,至少我希望如此。 我正在尝试使用LSTMCell来预测序列中的下一个标签。

以下是我正在使用的代码:

import tensorflow as tf

max_sequence_length = 1000
vector_length = 1
number_of_classes = 1000
batch_size = 50
num_hidden = 24

# Define graph
data = tf.placeholder(tf.int64, [None, max_sequence_length, vector_length])
# 0 must be a free class so that the mask can work
target = tf.placeholder(tf.int64, [None, max_sequence_length, number_of_classes + 1])
labels = tf.argmax(target, 2)
cell = tf.nn.rnn_cell.LSTMCell(num_hidden, state_is_tuple=True)

然后我尝试获得批处理中每个序列的实际长度

no_of_batches = tf.shape(data)[0]

sequence_lengths = tf.zeros([batch_size])
for i in xrange(max_sequence_length):
    data_at_t = tf.squeeze(tf.slice(data, [0,i,0],[-1,1,-1]))
    t = tf.scalar_mul(i, tf.ones([batch_size]))
    boolean = tf.not_equal(data_at_t, tf.zeros([no_of_batches, batch_size], dtype = tf.int64))
    sequence_lengths = tf.select(boolean, t, sequence_lengths)

最后我尝试调用tf.nn.dynamic_rnn:

outputs, state = tf.nn.dynamic_rnn(
    cell = cell,
    inputs = data,
    sequence_length = max_sequence_length,
    dtype = tf.float64
    )

然后,我得到一个TypeError:

Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 830, in dynamic_rnn
    dtype=dtype)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 997, in _dynamic_rnn_loop
    swap_memory=swap_memory)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 1973, in while_loop
    result = context.BuildLoop(cond, body, loop_vars)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 1860, in BuildLoop
    pred, body, original_loop_vars, loop_vars)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/control_flow_ops.py", line 1810, in _BuildLoop
    body_result = body(*packed_vars_for_body)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 980, in _time_step
    skip_conditionals=True)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 394, in _rnn_step
    new_output, new_state = call_cell()
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn.py", line 968, in <lambda>
    call_cell = lambda: cell(input_t, state)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn_cell.py", line 489, in __call__
    dtype, self._num_unit_shards)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn_cell.py", line 323, in _get_concat_variable
    sharded_variable = _get_sharded_variable(name, shape, dtype, num_shards)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn_cell.py", line 353, in _get_sharded_variable
    dtype=dtype))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 830, in get_variable
    custom_getter=custom_getter)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 673, in get_variable
    custom_getter=custom_getter)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 217, in get_variable
    validate_shape=validate_shape)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 202, in _true_getter
    caching_device=caching_device, validate_shape=validate_shape)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 536, in _get_single_variable
    validate_shape=validate_shape)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 211, in __init__
    dtype=dtype)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 281, in _init_from_args
    self._initial_value = ops.convert_to_tensor(initial_value(),
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 526, in <lambda>
    init_val = lambda: initializer(shape.as_list(), dtype=dtype)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/init_ops.py", line 210, in _initializer
    dtype, seed=seed)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/random_ops.py", line 235, in random_uniform
    minval = ops.convert_to_tensor(minval, dtype=dtype, name="min")
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 621, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 180, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 163, in constant
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 353, in make_tensor_proto
    _AssertCompatible(values, dtype)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 290, in _AssertCompatible
    (dtype.name, repr(mismatch), type(mismatch).__name__))
TypeError: Expected int64, got -0.34641016151377546 of type 'float' instead.

我不明白这个浮点数的来源,因为脚本中的所有其他值都是整数。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

1)RNN小区状态为 tf.float64 。您可以在 tf.nn.dynamic_rnn 调用(dtype)中明确设置它。然后,张量流引擎用RNN的默认random_uniform初始化器初始化状态。这就是你在那里有-0.34浮点值的原因。

我不确定你想要达到的目标。请参阅https://www.tensorflow.org/versions/r0.11/api_docs/python/nn.html#dynamic_rnn

2)sequence_length = max_sequence_length必须是int32 / int64向量大小[batch_size]而不是标量1000

3)您可能也想要初始化LSTMCell状态:

cell = tf.nn.rnn_cell.LSTMCell(num_hidden, state_is_tuple=True), 
    initializer=tf.constant_initializer(value=0, dtype=tf.int32))

答案 1 :(得分:0)

我的代码有类似的问题,我通过将占位符中的int32替换为float来修复它,检查是否有效