tf.nn.bidirectional_dynamic_rnn的怪异错误

时间:2018-12-04 20:37:44

标签: python python-3.x tensorflow

tf.nn.dynamic_rnn的代码对我来说很有效:

tf.reset_default_graph()
graph = tf.Graph()
with graph.as_default():
    W = tf.Variable(tf.constant(0.0, shape=[max_features, embedding_dim]),
                    trainable=True, name="W")

    embedding_placeholder = tf.placeholder(tf.float32, [max_features, embedding_dim])
    emb = W.assign(embedding_placeholder)

    X = tf.placeholder(tf.int32, shape=[None,maxlen])
    seq_length = tf.placeholder(tf.int32, [None])
    y = tf.placeholder(tf.int32, [None])

    X_emb = tf.nn.embedding_lookup(W,X)

    rnn_layers = [tf.nn.rnn_cell.LSTMCell(size) for size in [40,40]]
    multi_rnn_cell = tf.nn.rnn_cell.MultiRNNCell(rnn_layers)
    outputs, state = tf.nn.dynamic_rnn(cell=multi_rnn_cell,
                                       inputs=X_emb,
                                       dtype=tf.float32,
                                      sequence_length=seq_length)
    _,c = state
    _,h = c
    logits = tf.contrib.layers.fully_connected(h,2)
    loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,labels=y))
    optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)

    correct = tf.nn.in_top_k(logits,y,1)
    accuracy = tf.reduce_mean(tf.cast(correct,tf.float32))

但是,当我将其修改为使用tf.nn.bidirectional_dynamic_rnn时:

tf.reset_default_graph()
graph = tf.Graph()
with graph.as_default():
    W = tf.Variable(tf.constant(0.0, shape=[max_features, embedding_dim]),
                    trainable=True, name="W")

    embedding_placeholder = tf.placeholder(tf.float32, [max_features, embedding_dim])
    emb = W.assign(embedding_placeholder)

    X = tf.placeholder(tf.int32, shape=[None,maxlen])
    seq_length = tf.placeholder(tf.int32, [None])
    y = tf.placeholder(tf.int32, [None])

    X_emb = tf.nn.embedding_lookup(W,X)

    rnn_layers_fw = [tf.nn.rnn_cell.LSTMCell(size) for size in [40,40]]
    rnn_layers_bw = [tf.nn.rnn_cell.LSTMCell(size) for size in [40,40]]
    multi_rnn_cell_fw = tf.nn.rnn_cell.MultiRNNCell(rnn_layers_fw)
    multi_rnn_cell_bw = tf.nn.rnn_cell.MultiRNNCell(rnn_layers_bw)
    outputs, state = tf.nn.bidirectional_dynamic_rnn(cell_fw=multi_rnn_cell_fw,
                                                     cell_bw=multi_rnn_cell_bw,
                                       inputs=X_emb,
                                       dtype=tf.float32,
                                      sequence_length=seq_length)
    state_fw, state_bw = state
    _,c_fw = state_fw
    _,h_fw = c_fw
    _,c_bw = state_bw
    _,h_bw = c_bw
    h = tf.concat([h_fw,h_bw],1)
    logits = tf.contrib.layers.fully_connected(h,2)
    loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,labels=y))
    optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)

    correct = tf.nn.in_top_k(logits,y,1)
    accuracy = tf.reduce_mean(tf.cast(correct,tf.float32))

我在运行该会话时遇到了奇怪的错误,表明它在嵌入方面存在一些问题,您知道为什么吗?

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
~/miniconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1333     try:
-> 1334       return fn(*args)
   1335     except errors.OpError as e:

~/miniconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1318       return self._call_tf_sessionrun(
-> 1319           options, feed_dict, fetch_list, target_list, run_metadata)
   1320 

~/miniconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1406         self._session, options, feed_dict, fetch_list, target_list,
-> 1407         run_metadata)
   1408 

InvalidArgumentError: seq_lens(684) > input.dims(1)
     [[{{node bidirectional_rnn/bw/ReverseSequence}} = ReverseSequence[T=DT_FLOAT, Tlen=DT_INT32, batch_dim=0, seq_dim=1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](embedding_lookup, _arg_Placeholder_2_0_1)]]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-102-1ee4d81bf548> in <module>()
      6     for i in range(100):
      7         lossp, _, accuracyp = sess.run([loss,optimizer,accuracy],
----> 8                                    feed_dict={X:train_X[:1000],y:train_y[:1000],seq_length:train_len[:1000]})
      9         print(i)
     10         print(lossp)

~/miniconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    927     try:
    928       result = self._run(None, fetches, feed_dict, options_ptr,
--> 929                          run_metadata_ptr)
    930       if run_metadata:
    931         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~/miniconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1150     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1151       results = self._do_run(handle, final_targets, final_fetches,
-> 1152                              feed_dict_tensor, options, run_metadata)
   1153     else:
   1154       results = []

~/miniconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1326     if handle is None:
   1327       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1328                            run_metadata)
   1329     else:
   1330       return self._do_call(_prun_fn, handle, feeds, fetches)

~/miniconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1346           pass
   1347       message = error_interpolation.interpolate(message, self._graph)
-> 1348       raise type(e)(node_def, op, message)
   1349 
   1350   def _extend_graph(self):

InvalidArgumentError: seq_lens(684) > input.dims(1)
     [[node bidirectional_rnn/bw/ReverseSequence (defined at <ipython-input-101-ff219724e424>:27)  = ReverseSequence[T=DT_FLOAT, Tlen=DT_INT32, batch_dim=0, seq_dim=1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](embedding_lookup, _arg_Placeholder_2_0_1)]]

Caused by op 'bidirectional_rnn/bw/ReverseSequence', defined at:
  File "/Users/adremja/miniconda3/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/Users/adremja/miniconda3/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 486, in start
    self.io_loop.start()
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 450, in _handle_events
    self._handle_recv()
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 480, in _handle_recv
    self._run_callback(callback, msg)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 432, in _run_callback
    callback(*args, **kwargs)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
    handler(stream, idents, msg)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 208, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 537, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2728, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2850, in run_ast_nodes
    if self.run_code(code, result):
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-101-ff219724e424>", line 27, in <module>
    sequence_length=seq_length)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tensorflow/python/ops/rnn.py", line 466, in bidirectional_dynamic_rnn
    inputs_reverse = nest.map_structure(_map_reverse, inputs)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tensorflow/python/util/nest.py", line 381, in map_structure
    structure[0], [func(*x) for x in entries])
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tensorflow/python/util/nest.py", line 381, in <listcomp>
    structure[0], [func(*x) for x in entries])
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tensorflow/python/ops/rnn.py", line 464, in _map_reverse
    batch_axis=batch_axis)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tensorflow/python/ops/rnn.py", line 453, in _reverse
    seq_axis=seq_axis, batch_axis=batch_axis)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 2651, in reverse_sequence
    name=name)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 6818, in reverse_sequence
    seq_dim=seq_dim, batch_dim=batch_dim, name=name)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3274, in create_op
    op_def=op_def)
  File "/Users/adremja/miniconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1770, in __init__
    self._traceback = tf_stack.extract_stack()

InvalidArgumentError (see above for traceback): seq_lens(684) > input.dims(1)
     [[node bidirectional_rnn/bw/ReverseSequence (defined at <ipython-input-101-ff219724e424>:27)  = ReverseSequence[T=DT_FLOAT, Tlen=DT_INT32, batch_dim=0, seq_dim=1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](embedding_lookup, _arg_Placeholder_2_0_1)]]

0 个答案:

没有答案