【问题标题】:Problem with tf.train.Saver() and GPU - TensorFlowtf.train.Saver() 和 GPU 的问题 - TensorFlow
【发布时间】:2018-10-31 11:41:44
【问题描述】:

我的代码结构如下:

with tf.device('/gpu:1'):
...
model = get_model(input_pl)
...
    with tf.Session() as sess:
        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        for epoch in range(num_epochs):
           ...
           for n in range(num_batches):
              ...
              sess.run(...)
           # eval epoch
        saver.save(sess, ...)

我想在训练阶段后保存模型。当我运行它给我这个错误:

InvalidArgumentError (see above for traceback): Cannot assign a device for operation 'save/SaveV2': Could not satisfy explicit device specification '/device:GPU:1' because no supported kernel for GPU devices is available.

阅读this question我是这样改代码的:

saver = tf.train.Saver()
with tf.device('/gpu:1'):
...
model = get_model(pointcloud_pl)
...
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for epoch in range(num_epochs):
           ...
           for n in range(num_batches):
              ...
              sess.run(...)
           # eval epoch
        saver.save(sess, ...)

但现在我得到了这个错误:

ValueError: No variables to save

我也尝试过这样做:

with tf.Session() as sess:
    saver = tf.train.Saver()
    ...
    with tf.device('/gpu:1'):
        sess.run(tf.global_variables_initializer())
        for epoch in range(num_epochs):
        ...
            for n in range(num_batches):
               ...
               sess.run()
            # eval epoch
        saver.save(sess, ...)

我仍然遇到同样的错误。错误总是在saver = tf.train.Saver() 行中。

我该如何解决这个问题?

【问题讨论】:

  • 你在哪里构建图表?能否在构建图的代码中添加注释?
  • 编辑了第一个代码块。在with tf.device(): 之后和tf.Session() 之前

标签: python tensorflow gpu


【解决方案1】:

解决了这个问题:

  1. tf.Session()
  2. 型号
  3. saver = tf.train.Saver()
  4. with tf.device():

这里是一个示例代码

with tf.Session() as sess:
    ...
    model = get_model(input_pl)
    saver = tf.train.Saver()
    ...
    with tf.device('/gpu:1'):
        sess.run(tf.global_variables_initializer())
        for epoch in range(num_epochs):
        ...
            for n in range(num_batches):
               ...
               sess.run()
            # eval epoch
        saver.save(sess, ...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-13
    • 2016-08-25
    • 1970-01-01
    • 2021-06-11
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多