Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anna_lstm 两个版本的都出现问题 #22

Open
1mrliu opened this issue Jul 17, 2018 · 4 comments
Open

anna_lstm 两个版本的都出现问题 #22

1mrliu opened this issue Jul 17, 2018 · 4 comments

Comments

@1mrliu
Copy link

1mrliu commented Jul 17, 2018

运行两个版本的LSTM代码的时候,都是出现这个问题,一直没找到这个bug,所以想请教一下。
问题出现的代码位置:

运行RNN

outputs, state = tf.nn.dynamic_rnn(cell, x_one_hot, initial_state=self.initial_state)

提示出现的错误信息:(貌似是Tensor的形状问题,但是检查了代码,没有发现这个问题出在哪里了)
Dimensions must be equal, but are 1024 and 595 for 'rnn/while/rnn/multi_rnn_cell/cell_0/basic_lstm_cell/MatMul_1' (op: 'MatMul') with input shapes: [1,1024], [595,2048].

@PengInGitHub
Copy link

我的也是,没看出是为什么:
ValueError: Dimensions must be equal, but are 1024 and 595 for 'rnn/while/rnn/multi_rnn_cell/cell_0/basic_lstm_cell/MatMul_1' (op: 'MatMul') with input shapes: [100,1024], [595,2048].

@snow123321
Copy link

你好,请问问题解决了吗?我也遇到了这个问题,不知道是为什么

@stud2008
Copy link

stud2008 commented Mar 8, 2019

按以下代码重写build_lstm函数,TF1.11测试通过
def lstm_cell():
lstm = tf.nn.rnn_cell.BasicLSTMCell(lstm_size)
return tf.nn.rnn_cell.DropoutWrapper(lstm, output_keep_prob=keep_prob)

def build_lstm(lstm_size, num_layers, batch_size, keep_prob):
'''
构建lstm层
keep_prob
lstm_size: lstm隐层中结点数目
num_layers: lstm的隐层数目
batch_size: batch_size
'''
cell = tf.nn.rnn_cell.MultiRNNCell([lstm_cell() for _ in range(num_layers)])
initial_state = cell.zero_state(batch_size, tf.float32)
return cell, initial_state

@li-aolong
Copy link

按以下代码重写build_lstm函数,TF1.11测试通过
def lstm_cell():
lstm = tf.nn.rnn_cell.BasicLSTMCell(lstm_size)
return tf.nn.rnn_cell.DropoutWrapper(lstm, output_keep_prob=keep_prob)

def build_lstm(lstm_size, num_layers, batch_size, keep_prob):
'''
构建lstm层
keep_prob
lstm_size: lstm隐层中结点数目
num_layers: lstm的隐层数目
batch_size: batch_size
'''
cell = tf.nn.rnn_cell.MultiRNNCell([lstm_cell() for _ in range(num_layers)])
initial_state = cell.zero_state(batch_size, tf.float32)
return cell, initial_state

没错,因为原始代码相当于只构建了一个lstm单元lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size),然后在cell = tf.contrib.rnn.MultiRNNCell([drop for _ in range(num_layers)])这里相当于将刚刚构建的lstm单元重复添加了,所以需要定义一个函数来保证每次添加lstm单元时都是新生成的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants