Tiago Freitas Pereira
Tiago Freitas Pereira

Reputation: 690

Tensorflow, restore variables in a specific device

Maybe my question is a bit naive, but I really didn't find anything in the tensorflow documentation.

I have a trained tensorflow model where the variables of it was placed in the GPU. Now I would like to restore this model and test it using the CPU.

If I do this via 'tf.train.Saver.restore` as in the example: saver = tf.train.import_meta_graph("/tmp/graph.meta") saver.restore(session, "/tmp/model.ckp")

I have the following excpetion:

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

How can I make restore these variables in the CPU?

Thanks

Upvotes: 3

Views: 3563

Answers (2)

user6621932
user6621932

Reputation:

I'm using tensorflow 0.12 and clear_devices=True and tf.device('/cpu:0') was not working with me (saver.restore was still trying to assign variables to /gpu:0).

I really needed to force everything to /cpu:0 since I was loading several models which wouldn't fit in GPU memory anyways. Here are two alternatives to force everything to /cpu:0

  1. Set os.environ['CUDA_VISIBLE_DEVICES']=''
  2. Use the device_count of ConfigProto like tf.Session(config=tf.ConfigProto(device_count={"GPU": 0, "CPU": 1}))

Upvotes: 1

Yaroslav Bulatov
Yaroslav Bulatov

Reputation: 57953

Use clear_devices flag, ie

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

Upvotes: 8

Related Questions