InvalidArgumentError:要整形的输入是具有12000个值的张量,但请求的形状需要140的倍数

时间:2018-10-21 16:30:07

标签: python tensorflow rnn

我试图详细计算每个步骤的尺寸,试图发现问题,但是失败了,运行网络对于像我这样的初学者来说是一个挑战。

总共有5800行,每行数据有7维,每行数据对应一个一维标签,这些数据是文本时间序列数据。

normalized_train_data.shape
#(5800, 8)

我选择每次训练的步长为20,批量为60,输入大小为7,输出大小为1,代码如下所示。

batch_index = []
train_x,train_y = [],[]
time_step = 20
batch_size = 60
for i in range(len(normalized_train_data)-time_step):
    if i%batch_size == 0:
        batch_index.append(i)
    x = normalized_train_data[i:i+time_step,:7]
    y = normalized_train_data[i:i+time_step,7,np.newaxis]
    train_x.append(x.tolist())
    train_y.append(y.tolist())
batch_index.append(len(normalized_train_data)-time_step)
input_size = 7
rnn_unit = 10
output_size = 1
time_step = 20
batch_size = 60
learning_rate = 1e-4

weights = {
'in':tf.Variable(tf.random_normal([input_size,rnn_unit])),
'out':tf.Variable(tf.random_normal([rnn_unit,output_size]))
}
biases = {
'in':tf.Variable(tf.constant(value=0.1,shape=[rnn_unit,])),
'out':tf.Variable(tf.constant(value=0.1,shape=[output_size,]))
}

def RNN(inputs):
    w_in = weights['in']
    b_in = biases['in']
    batch_size = tf.shape(inputs)[0]
    time_step = tf.shape(inputs)[1]
    input_x = tf.reshape(inputs,shape=[-1,input_size])
    input_rnn = tf.matmul(input_x,w_in) + b_in
    input_rnn = tf.reshape(input_rnn,[-1,time_step,input_size])
    cell = tf.nn.rnn_cell.BasicRNNCell(rnn_unit)
    init_state = cell.zero_state(batch_size,dtype=tf.float32)
    output_rnn,final_states = tf.nn.dynamic_rnn(cell=cell,inputs=input_rnn,initial_state=init_state,dtype=tf.float32)
    output = tf.reshape(output_rnn,[-1,rnn_unit])
    w_out = weights['out']
    b_out = biases['out']
    pred = tf.matmul(output,w_out)+b_out
    return pred,final_states


X_holder = tf.placeholder(tf.float32,[None,time_step,input_size])
Y_holder = tf.placeholder(tf.float32,[None,time_step,output_size])

pred,_ = RNN(X_holder)
loss = tf.reduce_mean(tf.square(tf.reshape(pred,[-1])-tf.reshape(Y_holder,
[-1])))
train = tf.train.AdamOptimizer(learning_rate).minimize(loss)
# saver = tf.trian.saver(tf.global_variables,max_to_keep=15)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

培训过程代码如下:

total_loss = []
for i in range(1000):
    for step in range(len(batch_index)-1):
        _,loss_ = sess.run([train,loss],feed_dict={X_holder:train_x[batch_index[step]:batch_index[step+1]],Y_holder:train_y[batch_index[step]:batch_index[step+1]]})
        total_loss.append(loss_)

最终错误如下:

    InvalidArgumentError      Traceback (most recent call last)  
J:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
   1360     try:
-> 1361       return fn(*args)
   1362     except errors.OpError as e:

J:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
   1339           return tf_session.TF_Run(session, options, feed_dict, fetch_list,
-> 1340                                    target_list, status, run_metadata)
   1341 

J:\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    515             compat.as_text(c_api.TF_Message(self.status.status)),
--> 516             c_api.TF_GetCode(self.status.status))
    517     # Delete the underlying status object from memory otherwise it stays alive

InvalidArgumentError: Input to reshape is a tensor with 12000 values, but the requested shape requires a multiple of 140
     [[Node: Reshape_2 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](add, Reshape_1/shape)]]
     [[Node: Mean/_11 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_505_Mean", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

During handling of the above exception, another exception occurred:
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-34-c6007dcf9016> in <module>()
      2 for i in range(1000):
      3     for step in range(len(batch_index)-1):
----> 4         _,loss_ = sess.run([train,loss],feed_dict={X_holder:train_x[batch_index[step]:batch_index[step+1]],Y_holder:train_y[batch_index[step]:batch_index[step+1]]})
      5         total_loss.append(loss_)
      6         if i % 10==0:

J:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    903     try:
    904       result = self._run(None, fetches, feed_dict, options_ptr,
--> 905                          run_metadata_ptr)
    906       if run_metadata:
    907         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

J:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1135     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1136       results = self._do_run(handle, final_targets, final_fetches,
-> 1137                              feed_dict_tensor, options, run_metadata)
   1138     else:
   1139       results = []

J:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1353     if handle is None:
   1354       return self._do_call(_run_fn, self._session, feeds, fetches, targets,
-> 1355                            options, run_metadata)
   1356     else:
   1357       return self._do_call(_prun_fn, self._session, handle, feeds, fetches)

J:\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _do_call(self, fn, *args)
   1372         except KeyError:
   1373           pass
-> 1374       raise type(e)(node_def, op, message)
   1375 
   1376   def _extend_graph(self):

InvalidArgumentError: Input to reshape is a tensor with 12000 values, but the requested shape requires a multiple of 140
     [[Node: Reshape_2 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](add, Reshape_1/shape)]]
     [[Node: Mean/_11 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_505_Mean", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

Caused by op 'Reshape_2', defined at:
  File "J:\Anaconda3\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "J:\Anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "J:\Anaconda3\lib\site-packages\ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "J:\Anaconda3\lib\site-packages\traitlets\config\application.py", line 658, in launch_instance
    app.start()
  File "J:\Anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 486, in start
    self.io_loop.start()
  File "J:\Anaconda3\lib\site-packages\tornado\platform\asyncio.py", line 127, in start
    self.asyncio_loop.run_forever()
  File "J:\Anaconda3\lib\asyncio\base_events.py", line 422, in run_forever
    self._run_once()
  File "J:\Anaconda3\lib\asyncio\base_events.py", line 1432, in _run_once
    handle._run()
  File "J:\Anaconda3\lib\asyncio\events.py", line 145, in _run
    self._callback(*self._args)
  File "J:\Anaconda3\lib\site-packages\tornado\platform\asyncio.py", line 117, in _handle_events
    handler_func(fileobj, events)
  File "J:\Anaconda3\lib\site-packages\tornado\stack_context.py", line 276, in null_wrapper
    return fn(*args, **kwargs)
  File "J:\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 450, in _handle_events
    self._handle_recv()
  File "J:\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 480, in _handle_recv
    self._run_callback(callback, msg)
  File "J:\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 432, in _run_callback
    callback(*args, **kwargs)
  File "J:\Anaconda3\lib\site-packages\tornado\stack_context.py", line 276, in null_wrapper
    return fn(*args, **kwargs)
  File "J:\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "J:\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 233, in dispatch_shell
    handler(stream, idents, msg)
  File "J:\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "J:\Anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 208, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "J:\Anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 537, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "J:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2662, in run_cell
    raw_cell, store_history, silent, shell_futures)
  File "J:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2785, in _run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "J:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2903, in run_ast_nodes
    if self.run_code(code, result):
  File "J:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-17-ed57b1fae759>", line 1, in <module>
    input_rnn = tf.reshape(input_rnn,[-1,time_step,input_size])
  File "J:\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 5101, in reshape
    "Reshape", tensor=tensor, shape=shape, name=name)
  File "J:\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "J:\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3271, in create_op
    op_def=op_def)
  File "J:\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1650, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 12000 values, but the requested shape requires a multiple of 140
     [[Node: Reshape_2 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](add, Reshape_1/shape)]]
     [[Node: Mean/_11 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_505_Mean", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

这个问题困扰了我很长时间。无论我多么努力,我都无法解决。请帮助解决。非常感谢。

0 个答案:

没有答案