【问题标题】:Issues saving and restoring tensorflow model (LSTM)保存和恢复张量流模型(LSTM)的问题
【发布时间】:2017-07-17 03:47:31
【问题描述】:

我正在研究生成文本的 LSTM,但在重用之前训练的模型时遇到了问题。我在使用tensorflow website 作为资源时分解了下面的代码。

在这里我建立了我的所有变量:

graph = tf.Graph()

with graph.as_default():
    global_step = tf.Variable(0)

    data = tf.placeholder(tf.float32, [batch_size, len_section, char_size])
    labels = tf.placeholder(tf.float32, [batch_size, char_size])

    .....

    #Reset at the beginning of each test
    reset_test_state = tf.group(test_output.assign(tf.zeros([1, hidden_nodes])), 
                                test_state.assign(tf.zeros([1, hidden_nodes])))

    #LSTM
    test_output, test_state = lstm(test_data, test_output, test_state)
    test_prediction = tf.nn.softmax(tf.matmul(test_output, w) + b)

    saver = tf.train.Saver()

在这里,我正在训练我的模型并在 30 次迭代时保存一个检查点

with tf.Session(graph = graph) as sess:
    tf.global_variables_initializer().run()
    offset = 0

    for step in range(10000):

        offset = offset % len(X)

        if offset <= (len(X) - batch_size):

            batch_data = X[offset: offset + batch_size]
            batch_labels = y[offset:offset+batch_size]
            offset += batch_size

        else: 
            to_add = batch_size - (len(X) - offset)
            batch_data = np.concatenate((X[offset: len(X)], X[0: to_add]))
            batch_labels = np.concatenate((y[offset: len(X)], y[0: to_add]))
            offset = to_add

        _, training_loss = sess.run([optimizer, loss], feed_dict = {data : batch_data, labels : batch_labels})

        if step % 10 == 0:
            print('training loss at step %d: %.2f (%s)' % (step, training_loss, datetime.datetime.now()))

        if step % save_every == 0:
            saver.save(sess, checkpoint_directory + '/model.ckpt', global_step=step)

        if step == 30:
            break

我查看了那个目录并创建了以下文件:

据说我在这里恢复训练有素的模型并对其进行测试:

with tf.Session(graph=graph) as sess:
    #standard init step
    offset = 0
    saver = tf.train.Saver()
    saver.restore(sess, "/ckpt/model-150.meta")
    tf.global_variables_initializer().run()

    test_start = "I plan to make this world a better place "
    test_generated = test_start

....

执行此操作后,我收到以下错误:

DataLossError (see above for traceback): Unable to open table file /ckpt/model.ckpt-30.meta: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?

我不太确定我做错了什么。该教程看起来很简单,但我显然遗漏了一些东西。任何形式的反馈都将不胜感激。

【问题讨论】:

    标签: tensorflow restore lstm recurrent-neural-network


    【解决方案1】:

    首先,请注意,如果您在从检查点恢复后初始化所有变量,您将获得它们的随机初始值,而不是经过训练的值。

    其次,如果您使用tf.estimator.Estimator 而不是自己实现此功能,则更容易正确保存/恢复。

    第三,我不明白您如何通过model-150.meta 进行恢复,但看到有关model-30.meta 的错误。不过,我相信您应该只传递 model-30(不带 .meta 后缀)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多