2016-09-12 8 views
0

TensorFlowを使用するのは初めてのことですが、私の問題は解決しやすいかもしれません。 私はシーケンスの次のラベルを予測するためにLSTMCellで作業しようとしています。LSTMCellでdynamic_rnnを呼び出すときのTypeError

ここで私が使用しているコードです:

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) 

をそれから私は、バッチ私はtf.nnを呼び出すしよう

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) 

そして最後に各シーケンスの本当の長さを取得しようとするが、 .dynamic_rnn:

01:次に

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. 

このフロートがどこから来ているのか分かりませんが、スクリプト内の他のすべての値は整数です。どうすればこの問題を解決できますか?

答えて

0

1)RNNセルの状態は、tf.float64です。 tf.nn.dynamic_rnnコール(dtype)内でこれを明示的に設定します。テンソルフローエンジンは、RNNのデフォルトのrandom_uniform初期化子を使用して状態を初期化しました。これは、そこに-0.34浮動小数点値がある理由です。

私はあなたが達成したいとは思っていません。 sequence_length = max_sequence_lengthはINT32/Int64のベクトルはスカラー1000年

3)あなたはあまりにもLSTMCell状態を初期化することもできますの代わりに[BATCH_SIZE]サイズにする必要がありますhttps://www.tensorflow.org/versions/r0.11/api_docs/python/nn.html#dynamic_rnn

2)をご参照ください:

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

私は私のコードで同様の問題を抱えています、私は浮動小数点として、プレースホルダでint32を置き換えることによってそれを修正しました、それが動作するかどうかを確認します。

関連する問題