本文介绍了TensorFlow:不兼容的形状:结合使用CNN和LSTM时的[100,155]与[128,155]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试堆叠Conv-> Lstm->音频回归任务的全连接层时,出现了形状不兼容的错误.我无法弄清为什么我得到了我得到的错误-图形建立良好,然后抛出了错误-有人可以帮忙吗?

I'm getting an incompatible shape error when trying to stack a Conv -> Lstm -> Fully connected layers for an audio regression task. I can't work out why I'm getting the error I'm getting - the graph builds fine then throws the error - can anyone help?

lstm_num_hidden = 128
lstm_number_layers = 3
x = tf.placeholder(tf.float32, [None, 1024])
y = tf.placeholder(tf.float32, [None, 155])
keep_probability = tf.placeholder(tf.float32)

def conv2d(x, weights):
    return tf.nn.conv2d(x, weights, strides=[1, 1, 1, 1], padding='SAME')

x_spectrogram = tf.reshape(x, [-1, 32, 32, 1])

conv1_weights = tf.Variable(tf.truncated_normal([5, 5, 1, 32], stddev=0.1))
conv1_bias = tf.Variable(tf.constant(0.1, shape=[32]))
conv1_hidden = tf.nn.relu(conv2d(x_spectrogram, conv1_weights) + conv1_bias)
conv1_pooling = tf.nn.max_pool(conv1_hidden, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')

conv2_weights = tf.Variable(tf.truncated_normal([5, 5, 32, 64], stddev=0.1))
conv2_bias = tf.Variable(tf.constant(0.1, shape=[64]))
conv2_hidden = tf.nn.relu(conv2d(conv1_pooling, conv2_weights) + conv2_bias)
conv2_pooling = tf.nn.max_pool(conv2_hidden, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
conv2_output = tf.reshape(conv2_pooling, [-1, 64, 64])

# changes to [8, BatchSize, 8, 64]
tr_x = tf.transpose(conv2_output, [1, 0, 2])
re_x = tf.reshape(tr_x, [-1, 64])
sp_x = tf.split(0, 64, re_x)

lstm_cell = rnn_cell.LSTMCell(lstm_num_hidden, forget_bias=1.0, state_is_tuple=True)
lstm_cell = rnn_cell.MultiRNNCell([lstm_cell] * lstm_number_layers, state_is_tuple=True)
init_state = lstm_cell.zero_state(128, tf.float32)
lstm_output, _ = rnn.rnn(cell=lstm_cell, inputs=sp_x, dtype=tf.float32, initial_state=init_state)
lstm_weights = tf.Variable(tf.truncated_normal([lstm_num_hidden, 155], stddev=0.1))
lstm_bias = tf.Variable(tf.truncated_normal([155], stddev=0.1))
out = tf.add(tf.matmul(lstm_output[-1], lstm_weights), lstm_bias)

fully_connected1_weights = tf.Variable(tf.truncated_normal([155, 1024], stddev=0.1))
fully_connected1_biases = tf.Variable(tf.truncated_normal([1024], stddev=0.1))
fully_connected1 = tf.nn.relu(tf.matmul(out, fully_connected1_weights) + fully_connected1_biases)
fully_connected1_dropout = tf.nn.dropout(fully_connected1, keep_probability)

fully_connected2_weights = tf.Variable(tf.truncated_normal([1024, 155], stddev=0.1))
fully_connected2_biases = tf.Variable(tf.truncated_normal([155], stddev=0.1))
prediction = tf.matmul(fully_connected1_dropout, fully_connected2_weights) + fully_connected2_biases

def error(labels, prediction):
    return tf.sqrt(tf.reduce_mean(tf.square(tf.sub(labels, prediction))))

rmse = error(y, prediction)
optimise = tf.train.AdamOptimizer(1e-4).minimize(rmse)

# Get training and testing batch.
train_batch_x = np.load("train_x.npy")
train_batch_y = np.load("train_y.npy")
test_batch_x = np.load("test_x.npy")
test_batch_y = np.load("test_y.npy")

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for i in range(100):

    for batch in trange(99, desc="Training"):
        start = batch * 100
        end = batch * 100 + 100
        sess.run(optimise, { x: train_batch_x[start:end],
                             y: train_batch_y[start:end],
                             keep_probability: 0.5 })

    rmse_error = sess.run(rmse, { x: test_batch_x,
                                  y: test_batch_y,
                                  keep_probability: 1.0 })
    print "Root Mean Squared Error:" + str(rmse_error)

错误

W tensorflow/core/framework/op_kernel.cc:975] Invalid argument: Incompatible shapes: [100,155] vs. [128,155]
     [[Node: gradients/Sub_grad/BroadcastGradientArgs = BroadcastGradientArgs[T=DT_INT32, _device="/job:localhost/replica:0/task:0/gpu:0"](gradients/Sub_grad/Shape, gradients/Sub_grad/Shape_1)]]
W tensorflow/core/framework/op_kernel.cc:975] Invalid argument: Incompatible shapes: [100,155] vs. [128,155]
     [[Node: gradients/Sub_grad/BroadcastGradientArgs = BroadcastGradientArgs[T=DT_INT32, _device="/job:localhost/replica:0/task:0/gpu:0"](gradients/Sub_grad/Shape, gradients/Sub_grad/Shape_1)]]

Traceback (most recent call last):
  File "conv_rnn_experiment.py", line 81, in <module>
    keep_probability: 0.5 })
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 766, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 964, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1014, in _do_run
    target_list, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1034, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [100,155] vs. [128,155]
     [[Node: gradients/Sub_grad/BroadcastGradientArgs = BroadcastGradientArgs[T=DT_INT32, _device="/job:localhost/replica:0/task:0/gpu:0"](gradients/Sub_grad/Shape, gradients/Sub_grad/Shape_1)]]

Caused by op u'gradients/Sub_grad/BroadcastGradientArgs', defined at:
  File "conv_rnn_experiment.py", line 63, in <module>
    optimise = tf.train.AdamOptimizer(1e-4).minimize(rmse)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py", line 269, in minimize
    grad_loss=grad_loss)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py", line 335, in compute_gradients
    colocate_gradients_with_ops=colocate_gradients_with_ops)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gradients_impl.py", line 482, in gradients
    in_grads = grad_fn(op, *out_grads)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_grad.py", line 594, in _SubGrad
    rx, ry = gen_array_ops._broadcast_gradient_args(sx, sy)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 390, in _broadcast_gradient_args
    name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2240, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1128, in __init__
    self._traceback = _extract_stack()

  ...which was originally created as op u'Sub', defined at:
  File "conv_rnn_experiment.py", line 62, in <module>
    rmse = error(y, prediction)
  File "conv_rnn_experiment.py", line 60, in error
    return tf.sqrt(tf.reduce_mean(tf.square(tf.sub(labels, prediction))))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 2758, in sub
    result = _op_def_lib.apply_op("Sub", x=x, y=y, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2240, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1128, in __init__
    self._traceback = _extract_stack()

  InvalidArgumentError (see above for traceback): Incompatible shapes: [100,155] vs. [128,155]
     [[Node: gradients/Sub_grad/BroadcastGradientArgs = BroadcastGradientArgs[T=DT_INT32, _device="/job:localhost/replica:0/task:0/gpu:0"](gradients/Sub_grad/Shape, gradients/Sub_grad/Shape_1)]]

推荐答案

如果在打印sess.run()之前打印了几行内容,您会发现它在lstm_output处中断.除此之外,您可以开始缩小问题范围,最终成为以下一行:

If you print a couple things before when you do your sess.run() you'll notice that it breaks at lstm_output. Going off that, you can start narrowing down your issue, which ended up being this line:

init_state = lstm_cell.zero_state(128, tf.float32)

此初始化用于确定批处理大小.由于您有155个单位,并且声明的批处理大小为128,因此预期输入为128 x 155.但是,看来您的批次是100,因此,如果更改该行,它应该可以工作.

This initialization is to determine the batch size. Since you have 155 units and declared a batch size of 128, it's expected that the input is 128 x 155. However, it seems like your batch is 100, so if you change that line it should work.

这篇关于TensorFlow:不兼容的形状:结合使用CNN和LSTM时的[100,155]与[128,155]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 02:38