- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
尝试为音频回归任务堆叠 Conv -> Lstm -> Fully connected 层时出现不兼容的形状错误。我无法弄清楚为什么我会收到我收到的错误 - 图表构建良好然后抛出错误 - 谁能帮忙?
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
处中断。离开那个,你可以开始缩小你的问题,最终成为这一行:
init_state = lstm_cell.zero_state(128, tf.float32)
这个初始化是为了确定batch size。由于您有 155 个单位并且声明的批量大小为 128,因此预计输入为 128 x 155
。但是,您的批处理似乎是 100,因此如果您更改该行,它应该会起作用。
关于python - TensorFlow:不兼容的形状:[100,155] 与 [128,155] 结合使用 CNN 和 LSTM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42822844/
我无法准确理解 LSTM 单元的范围——它如何映射到网络层。来自格雷夫斯 (2014): 在我看来,在单层网络中,layer = lstm 单元。这实际上如何在多层 rnn 中工作? 三层RNN LS
这是代码 model = Sequential() model.add(LSTM(256, input_shape=(None, 1), return_sequences=True)) model.a
为什么我们需要在pytorch中初始化LSTM中的隐藏状态h0。由于 h0 无论如何都会被计算并被覆盖?是不是很像 整合一个一 = 0 一个= 4 即使我们不做a=0,也应该没问题.. 最佳答案 重点
我正在尝试使用 LSTM 在 Deeplearning4j 中进行一些简单的时间序列预测,但我很难让它工作。 我有一个简单的文本文件,其中包含如下所示的数字列表,并希望网络学习预测下一个数字。 有没有
在大量阅读和绘制图表之后,我想我已经提出了一个模型,我可以将其用作更多测试我需要调整哪些参数和功能的基础。但是,我对如何实现以下测试用例感到困惑(所有数字都比最终模型小几个数量级,但我想从小处着手):
我正在尝试实现“Livelinet:用于预测教育视频中的活力的多模式深度循环神经网络”中的结构。 为了简单说明,我将 10 秒音频剪辑分成 10 个 1 秒音频剪辑,并从该 1 秒音频剪辑中获取频谱图
我正在 Tensorflow 中制作 LSTM 神经网络。 输入张量大小为 92。 import tensorflow as tf from tensorflow.contrib import rnn
我正在尝试 keras IMDB 数据的示例,数据形状是这样的: x_train shape: (25000, 80) 我只是把keras例子的原始代码改成了这样的代码: model = Sequen
我需要了解如何使用 torch.nn 的不同组件正确准备批量训练的输入。模块。具体来说,我希望为 seq2seq 模型创建一个编码器-解码器网络。 假设我有一个包含这三层的模块,按顺序: nn.Emb
我很难概念化 Keras 中有状态 LSTM 和无状态 LSTM 之间的区别。我的理解是,在每个批处理结束时,在无状态情况下“网络状态被重置”,而对于有状态情况,网络状态会为每个批处理保留,然后必须在
nn.Embedding() 是学习 LSTM 所必需的吗? 我在 PyTorch 中使用 LSTM 来预测 NER - 此处是类似任务的示例 - https://pytorch.org/tutori
我正在尝试找出适合我想要拟合的模型的正确语法。这是一个时间序列预测问题,我想在将时间序列输入 LSTM 之前使用一些密集层来改进时间序列的表示。 这是我正在使用的虚拟系列: import pandas
我在理解堆叠式 LSTM 网络中各层的输入-输出流时遇到了一些困难。假设我已经创建了一个如下所示的堆叠式 LSTM 网络: # parameters time_steps = 10 features
LSTM 类中的默认非线性激活函数是 tanh。我希望在我的项目中使用 ReLU。浏览文档和其他资源,我无法找到一种简单的方法来做到这一点。我能找到的唯一方法是定义我自己的自定义 LSTMCell,但
在 PyTorch 中,有一个 LSTM 模块,除了输入序列、隐藏状态和单元状态之外,它还接受 num_layers 参数,该参数指定我们的 LSTM 有多少层。 然而,还有另一个模块 LSTMCel
没什么好说的作为介绍:我想在 TensorFlow 中将 LSTM 堆叠在另一个 LSTM 上,但一直被错误阻止,我不太明白,更不用说单独解决了。 代码如下: def RNN(_X, _istate,
有人可以解释一下吗?我知道双向 LSTM 具有前向和反向传递,但是与单向 LSTM 相比,它有什么优势? 它们各自更适合什么? 最佳答案 LSTM 的核心是使用隐藏状态保留已经通过它的输入信息。 单向
我想构建一个带有特殊词嵌入的 LSTM,但我对它的工作原理有一些疑问。 您可能知道,一些 LSTM 对字符进行操作,因此它是字符输入,字符输出。我想做同样的事情,通过对单词的抽象来学习使用嵌套的 LS
我编写了一个LSTM回归模型。它是最后一个LSTM层的BATCH_SIZE=1和RETURN_Sequence=True的模型。我还设置了VERIFICATION_DATA和耐心进行培训。但似乎存在一
给定一个训练有素的 LSTM 模型,我想对单个时间步执行推理,即以下示例中的 seq_length = 1。在每个时间步之后,需要为下一个“批处理”记住内部 LSTM(内存和隐藏)状态。在推理的最开始
我是一名优秀的程序员,十分优秀!