2016-11-06 106 views
3

也许我的问题有点天真,但我在tensorflow文档中找不到任何东西。Tensorflow,在特定设备中恢复变量

我有一个训练过的tensorflow模型,其中的变量放在GPU中。现在我想恢复这个模型并使用CPU进行测试。

如果我这样做,通过“tf.train.Saver.restore`作为例子: saver = tf.train.import_meta_graph("/tmp/graph.meta") saver.restore(session, "/tmp/model.ckp")

我有以下错误时抛出:

InvalidArgumentError: Cannot assign a device to node 'b_fc8/b_fc8/Adam_1': Could not satisfy explicit device specification '/device:GPU:0' because no devices matching that specification are registered in this process; available devices: /job:localhost/replica:0/task:0/cpu:0

我怎样才能让这些恢复CPU中的变量?

感谢

回答

3

使用clear_devices标志,即

saver = tf.train.import_meta_graph("/tmp/graph.meta", clear_devices=True) 
+0

感谢DEVICE_COUNT, 其实这个资源是可用的版本'0.11'。 我使用0.10。 –

+0

@TiagoFreitasPereira,然后你可以加载GraphDef,遍历节点,清除字段“设备”,然后将这个GraphDef导入到你的图中 –

+0

也可以在文本编辑器中打开'graph.pbtxt',并删除每一行以'设备:' –

0

我使用tensorflow 0.12和clear_devices=Truetf.device('/cpu:0')不和我一起工作(saver.restore仍试图分配变量/ GPU: 0)。

我真的需要强制一切到/ cpu:0,因为我加载了几个模型,无论如何不适合GPU内存。这里有两个备选方案,以强制所有内容/ CP​​U:0

  1. os.environ['CUDA_VISIBLE_DEVICES']=''
  2. 使用ConfigProto像tf.Session(config=tf.ConfigProto(device_count={"GPU": 0, "CPU": 1}))
相关问题