caicloud / tensorflow-tutorial

Example TensorFlow codes and Caicloud TensorFlow as a Service dev environment.
2.93k stars 2.08k forks source link

第八章 使用循环神经网络实现语言模型 #28

Closed weizhenzhao closed 6 years ago

weizhenzhao commented 7 years ago
    # 定义输出列表,在这里先将不同时刻LSTM结构的输出收集起来,再通过一个全连接层得到最终的输出
    outputs = []
    # state存储不同batch种LSTM的状态,将其初始化为0
    state = self.initial_state
    with tf.variable_scope("RNN"):
        for time_step in range(num_steps):
            if time_step > 0: tf.get_variable_scope().reuse_variables()
            # 从输入数据中获取当前时刻的输入并传入LSTM结构
            cell_output, state = cell(inputs[:, time_step, :], state)                      **-----------------------这一行报错了**
            # 当前输出加入输出队列
            outputs.append(cell_output)

    # 把输出队列展开成[batch*hidden_size*num_steps]的形状,然后再
    # reshape成[batch*numsteps,hidden_size]的形状
    output = tf.reshape(tf.concat(1, outputs), [-1, HIDDEN_SIZE])

Traceback (most recent call last): File "C:\Users\weizhen\workspace\TextUtil\LanguageModel.py", line 159, in tf.app.run() File "C:\Users\weizhen\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\platform\app.py", line 48, in run _sys.exit(main(_sys.argv[:1] + flags_passthrough)) File "C:\Users\weizhen\workspace\TextUtil\LanguageModel.py", line 135, in main train_model = PTBModel(True, TRAIN_BATCH_SIZE, TRAIN_NUM_STEP) File "C:\Users\weizhen\workspace\TextUtil\LanguageModel.py", line 69, in init cell_output, state = cell(inputs[:, time_step, :], state) File "C:\Users\weizhen\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\rnn\python\ops\core_rnn_cell_impl.py", line 953, in call cur_inp, new_state = cell(cur_inp, cur_state) File "C:\Users\weizhen\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\rnn\python\ops\core_rnn_cell_impl.py", line 713, in call output, new_state = self._cell(inputs, state, scope) File "C:\Users\weizhen\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\rnn\python\ops\core_rnn_cell_impl.py", line 235, in call with _checked_scope(self, scope or "basic_lstm_cell", reuse=self._reuse): File "C:\Users\weizhen\AppData\Local\Programs\Python\Python35\lib\contextlib.py", line 59, in enter return next(self.gen) File "C:\Users\weizhen\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\rnn\python\ops\core_rnn_cell_impl.py", line 77, in _checked_scope type(cell).name)) ValueError: Attempt to reuse RNNCell <tensorflow.contrib.rnn.python.ops.core_rnn_cell_impl.BasicLSTMCell object at 0x00000215F1A63B00> with a different variable scope than its first use. First use of cell was with scope 'language_model/RNN/multi_rnn_cell/cell_0/basic_lstm_cell', this attempt is with scope 'language_model/RNN/multi_rnn_cell/cell_1/basic_lstm_cell'. Please create a new instance of the cell if you would like it to use a different set of weights. If before you were using: MultiRNNCell([BasicLSTMCell(...)] * numlayers), change to: MultiRNNCell([BasicLSTMCell(...) for in range(num_layers)]). If before you were using the same cell instance as both the forward and reverse cell of a bidirectional RNN, simply create two instances (one for forward, one for reverse). In May 2017, we will start transitioning this cell's behavior to use existing stored weights, if any, when it is called with scope=None (which can lead to silent model degradation, so this error will remain until then.)

perhapszzy commented 7 years ago

请确认tensorflow的版本和python的版本,如果版本正确的话应该是不会有问题的

ScorpioCPH commented 7 years ago

@weizhenzhao ping.

saselovejulie commented 7 years ago

@weizhenzhao 你试试把output = tf.reshape(tf.concat(1, outputs), [-1, HIDDEN_SIZE]) 改为 output = tf.reshape(tf.concat(outputs, 1), [-1, HIDDEN_SIZE])

caicloud-bot commented 6 years ago

Issues go stale after 90d of inactivity. Mark the issue as fresh with /remove-lifecycle stale. Stale issues rot after an additional 30d of inactivity and eventually close.

Prevent issues from auto-closing with an /lifecycle frozen comment.

If this issue is safe to close now please do so with /close.

/lifecycle stale

caicloud-bot commented 6 years ago

Stale issues rot after 30d of inactivity. Mark the issue as fresh with /remove-lifecycle rotten. Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

/lifecycle rotten /remove-lifecycle stale