MultiRNN和static_rnn错误:尺寸必须相等,但分别为256和129 [英] MultiRNN and static_rnn error: Dimensions must be equal, but are 256 and 129

查看:105
本文介绍了MultiRNN和static_rnn错误:尺寸必须相等,但分别为256和129的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个具有3层的LSTM网络.这是代码:

I want to build an LSTM network with 3 Layers. Here's the code:

num_layers=3
time_steps=10
num_units=128
n_input=1
learning_rate=0.001
n_classes=1
...

x=tf.placeholder("float",[None,time_steps,n_input],name="x")
y=tf.placeholder("float",[None,n_classes],name="y")
input=tf.unstack(x,time_steps,1)

lstm_layer=rnn_cell.BasicLSTMCell(num_units,state_is_tuple=True)
network=rnn_cell.MultiRNNCell([lstm_layer for _ in range(num_layers)],state_is_tuple=True)

outputs,_=rnn.static_rnn(network,inputs=input,dtype="float")

使用num_layers=1可以正常工作,但是在一层以上的情况下,我在此行得到了错误:

With num_layers=1 it works fine, but with more than one layer I get the error at this line:

outputs,_=rnn.static_rnn(network,inputs=input,dtype="float")

ValueError:尺寸必须相等,但对于256和129 'rnn/rnn/multi_rnn_cell/cell_0/cell_0/basic_lstm_cell/MatMul_1'(操作: 输入形状为[?,256],[129,512]的"MatMul").

ValueError: Dimensions must be equal, but are 256 and 129 for 'rnn/rnn/multi_rnn_cell/cell_0/cell_0/basic_lstm_cell/MatMul_1' (op: 'MatMul') with input shapes: [?,256], [129,512].

任何人都可以解释值129和512的来源吗?

Can anyone explain where the values 129 and 512 are coming from?

推荐答案

您不应在第一层和更深层使用相同的单元,因为它们的输入不同,因此内核矩阵也不同.试试这个:

You should not reuse the same cell for the first and deeper layers, because their inputs are different, hence kernel matrices are different. Try this:

# Extra function is for readability. No problem to inline it.
def make_cell(lstm_size):
  return tf.nn.rnn_cell.BasicLSTMCell(lstm_size, state_is_tuple=True)

network = rnn_cell.MultiRNNCell([make_cell(num_units) for _ in range(num_layers)], 
                                state_is_tuple=True)

这篇关于MultiRNN和static_rnn错误:尺寸必须相等,但分别为256和129的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆