所以看来我在这里做错了,我会感激一些帮助。当我将验证集输入网络时,维度与用于培训的维度不同。我希望“shape [0] = [1363,300] vs. shape [1] = [128,300]”因为我的单词嵌入维度是300。
def make_cell():
cell = tf.contrib.rnn.BasicLSTMCell(lstmUnits)
if dropout_rate.eval(session=tf.Session()) == 1:
cell = tf.contrib.rnn.DropoutWrapper(cell, output_keep_prob=dropout_rate)
return cell
labels = tf.placeholder(tf.float32, [None, numClasses])
input_data = tf.placeholder(tf.int32, [None, maxSeqLength])
dropout_rate = tf.placeholder_with_default(1.0, shape=())
rnn_input = tf.Variable(tf.zeros([batchSize, maxSeqLength, numDimensions]), dtype=tf.float32)
rnn_input = tf.nn.embedding_lookup(vectors_fasttext, input_data) # fastext lookup
multiLSTMCell = tf.contrib.rnn.MultiRNNCell([make_cell() for _ in range(num_layers)], state_is_tuple=True)
init_state = state = multiLSTMCell.zero_state(batchSize, tf.float32)
rnn_output, _ = tf.nn.dynamic_rnn(multiLSTMCell, rnn_input, initial_state=init_state, dtype=tf.float32)
# training
for i in range(iterations):
nextBatch, nextBatchLabels = getBatch(train_ids, train_labels)
_, batch_loss = sess.run([optimizer, loss], feed_dict={input_data: nextBatch,
labels: nextBatchLabels,
dropout_rate: 0.5})
if (i % 500 == 0 and i != 0):
acc, prec, rec, f1 = sess.run([accuracy, pre_op, rec_op, f1_op],
feed_dict={input_data: validation_data, labels: validation_data_labels})
print(validation_data_labels.shape)
print(validation_data.shape)
(1363, 2)
(1363, 20)
我收到了这个错误:
InvalidArgumentError Traceback (most recent call last)
c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1322 try:
-> 1323 return fn(*args)
1324 except errors.OpError as e:
c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
1301 feed_dict, fetch_list, target_list,
-> 1302 status, run_metadata)
1303
c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
472 compat.as_text(c_api.TF_Message(self.status.status)),
--> 473 c_api.TF_GetCode(self.status.status))
474 # Delete the underlying status object from memory otherwise it stays alive
InvalidArgumentError: ConcatOp : Dimensions of inputs should match: shape[0] = [1363,300] vs. shape[1] = [128,128]
[[Node: rnn/while/rnn/multi_rnn_cell/cell_0/cell_0/basic_lstm_cell/concat = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](rnn/while/TensorArrayReadV3, rnn/while/Identity_3, rnn/while/rnn/multi_rnn_cell/cell_0/cell_0/basic_lstm_cell/concat/axis)]]
During handling of the above exception, another exception occurred:
InvalidArgumentError Traceback (most recent call last)
<ipython-input-24-2e91b9d38b41> in <module>()
19
20 acc, prec, rec, f1 = sess.run([accuracy, pre_op, rec_op, f1_op],
---> 21 feed_dict={input_data: validation_data, labels: validation_data_labels})
22
23 print('Validation scores at iteration', i, ': accuracy', acc,
c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
887 try:
888 result = self._run(None, fetches, feed_dict, options_ptr,
--> 889 run_metadata_ptr)
890 if run_metadata:
891 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1118 if final_fetches or final_targets or (handle and feed_dict_tensor):
1119 results = self._do_run(handle, final_targets, final_fetches,
-> 1120 feed_dict_tensor, options, run_metadata)
1121 else:
1122 results = []
c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
1315 if handle is None:
1316 return self._do_call(_run_fn, self._session, feeds, fetches, targets,
-> 1317 options, run_metadata)
1318 else:
1319 return self._do_call(_prun_fn, self._session, handle, feeds, fetches)
c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
1334 except KeyError:
1335 pass
-> 1336 raise type(e)(node_def, op, message)
1337
1338 def _extend_graph(self):
InvalidArgumentError: ConcatOp : Dimensions of inputs should match: shape[0] = [1363,300] vs. shape[1] = [128,128]
[[Node: rnn/while/rnn/multi_rnn_cell/cell_0/cell_0/basic_lstm_cell/concat = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](rnn/while/TensorArrayReadV3, rnn/while/Identity_3, rnn/while/rnn/multi_rnn_cell/cell_0/cell_0/basic_lstm_cell/concat/axis)]]
Caused by op 'rnn/while/rnn/multi_rnn_cell/cell_0/cell_0/basic_lstm_cell/concat', defined at:
File "c:\users\tony\appdata\local\programs\python\python36\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\users\tony\appdata\local\programs\python\python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
app.launch_new_instance()
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
app.start()
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\ipykernel\kernelapp.py", line 477, in start
ioloop.IOLoop.instance().start()
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tornado\ioloop.py", line 888, in start
handler_func(fd_obj, events)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tornado\stack_context.py", line 277, in null_wrapper
return fn(*args, **kwargs)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\ipykernel\kernelbase.py", line 235, in dispatch_shell
handler(stream, idents, msg)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\ipykernel\zmqshell.py", line 533, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\IPython\core\interactiveshell.py", line 2728, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\IPython\core\interactiveshell.py", line 2850, in run_ast_nodes
if self.run_code(code, result):
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\IPython\core\interactiveshell.py", line 2910, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-22-54e19d6ef3f0>", line 17, in <module>
rnn_output, _ = tf.nn.dynamic_rnn(multiLSTMCell, rnn_input, initial_state=init_state, dtype=tf.float32)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\rnn.py", line 614, in dynamic_rnn
dtype=dtype)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\rnn.py", line 777, in _dynamic_rnn_loop
swap_memory=swap_memory)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2816, in while_loop
result = loop_context.BuildLoop(cond, body, loop_vars, shape_invariants)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2640, in BuildLoop
pred, body, original_loop_vars, loop_vars, shape_invariants)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 2590, in _BuildLoop
body_result = body(*packed_vars_for_body)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\rnn.py", line 762, in _time_step
(output, new_state) = call_cell()
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\rnn.py", line 748, in <lambda>
call_cell = lambda: cell(input_t, state)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 183, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\layers\base.py", line 575, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 1066, in call
cur_inp, new_state = cell(cur_inp, cur_state)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 891, in __call__
output, new_state = self._cell(inputs, state, scope)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 183, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\layers\base.py", line 575, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 441, in call
value=self._linear([inputs, h]), num_or_size_splits=4, axis=1)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\rnn_cell_impl.py", line 1189, in __call__
res = math_ops.matmul(array_ops.concat(args, 1), self._weights)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1099, in concat
return gen_array_ops._concat_v2(values=values, axis=axis, name=name)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 705, in _concat_v2
"ConcatV2", values=values, axis=axis, name=name)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\ops.py", line 2956, in create_op
op_def=op_def)
File "c:\users\tony\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1470, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): ConcatOp : Dimensions of inputs should match: shape[0] = [1363,300] vs. shape[1] = [128,128]
[[Node: rnn/while/rnn/multi_rnn_cell/cell_0/cell_0/basic_lstm_cell/concat = ConcatV2[N=2, T=DT_FLOAT, Tidx=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](rnn/while/TensorArrayReadV3, rnn/while/Identity_3, rnn/while/rnn/multi_rnn_cell/cell_0/cell_0/basic_lstm_cell/concat/axis)]]
提前感谢您的帮助。
答案 0 :(得分:0)
快速查看:您定义
rnn_input = tf.Variable(tf.zeros([batchSize, maxSeqLength, numDimensions]), dtype=tf.float32)
取决于批量大小(128),但随后输入验证数据,其中包含len(1363)
我想
rnn_input = tf.Variable(tf.zeros([tf.get_shape(input_data)[0], maxSeqLength, numDimensions]), dtype=tf.float32)
可能会解决您的问题,但我恐怕无法检查:) 您还可以将验证拆分为与训练数据大小相同的批次,然后合并结果......