【发布时间】:2018-07-19 07:26:34
【问题描述】:
我尝试制作一个堆叠 2 层的简单 LSTM 网络。为此,我使用 MultiRNNCell。我遵循了教程和其他堆栈主题,但我仍然无法运行我的网络。您可以在下面找到我在堆栈上找到的初始状态声明。
cell_count = 10 # timesteps
num_hidden = 4 # hidden layer num of features
num_classes = 1
num_layers = 2
state_size = 4
init_c = tf.Variable(tf.zeros([batch_size, cell_count]), trainable=False)
init_h = tf.Variable(tf.zeros([batch_size, cell_count]), trainable=False)
initial_state = rnn.LSTMStateTuple(init_c, init_h) #[num_layers, 2, batch_size, state_size])
您可以在下面找到我的模型的样子:
def generate_model_graph(self, data):
L1 = self.generate_layer(self.cell_count)
L2 = self.generate_layer(self.cell_count)
#outputs from L1
L1_outs, _ = L1(data, self.initial_state)
#reverse output array
L2_inputs = L1_outs[::-1]
L2_outs, _ = L2(L2_inputs, self.initial_state)
predicted_vals = tf.add(tf.matmul(self.weights["out"], L2_outs), self.biases["out"])
L2_out = tf.nn.sigmoid(predicted_vals)
return L2_out
def generate_layer(self, size):
cells = [rnn.BasicLSTMCell(self.num_hidden) for _ in range(size)]
return rnn.MultiRNNCell(cells)
并运行会话:
def train_model(self, generator):
tr, cost = self.define_model()
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for _ in range(self.n_epochs):
batch_x, batch_y = self._prepare_data(generator)
init_state = tf.zeros((self.cell_count, self.num_hidden))
t, c = sess.run([tr, cost], feed_dict={self.X: batch_x, self.Y:batch_y, self.initial_state:init_state})
print(c)
很遗憾,我仍然收到错误消息 'Variable' object is not iterable。
File "detector_lstm_v2.py", line 104, in <module>
c.train_model(data_gen)
File "detector_lstm_v2.py", line 38, in train_model
tr, cost = self.define_model()
File "detector_lstm_v2.py", line 51, in define_model
predicted_vals = self.generate_model_graph(self.X)
File "detector_lstm_v2.py", line 65, in generate_model_graph
L1_outs, _ = L1(data, self.initial_state)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py", line 232, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/layers/base.py", line 329, in __call__
outputs = super(Layer, self).__call__(inputs, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 703, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py", line 1325, in call
cur_inp, new_state = cell(cur_inp, cur_state)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py", line 339, in __call__
*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/layers/base.py", line 329, in __call__
outputs = super(Layer, self).__call__(inputs, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py", line 703, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/rnn_cell_impl.py", line 633, in call
c, h = state
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/variables.py", line 491, in __iter__
raise TypeError("'Variable' object is not iterable.")
TypeError: 'Variable' object is not iterable.
有人知道如何解决这个问题吗?
【问题讨论】:
标签: python python-3.x tensorflow lstm