I'm learning the demo of tensorflow eager execution. When I tried the cell "GPU usage" (see below), there is an error saying the variable is not placed on GPU.
import tensorflow as tf
import tensorflow.contrib.eager as tfe
tf.enable_eager_execution()
A = tf.constant([[2.0, 0.0], [0.0, 3.0]])
if tf.test.is_gpu_available() > 0:
with tf.device(tf.test.gpu_device_name()):
print(tf.matmul(A, A))
Full error message:
Traceback (most recent call last):
File "", line 4, in print(tf.matmul(A, A))
File "c:\python\python35_64\lib\site-packages\tensorflow\python\ops\math_ops.py", line 2108, in matmul a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
File "c:\python\python35_64\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 4517, in mat_mul _six.raise_from(_core._status_to_exception(e.code, message), None)
File "", line 3, in raise_from
InvalidArgumentError: Tensors on conflicting devices: cannot compute MatMul as input #0 was expected to be on /job:localhost/replica:0/task:0/device:GPU:0 but is actually on /job:localhost/replica:0/task:0/device:CPU:0 (operation running on /job:localhost/replica:0/task:0/device:GPU:0) Tensors can be copied explicitly using .gpu() or .cpu(), or transparently copied by using tfe.enable_eager_execution(tfe.DEVICE_PLACEMENT_SILENT). Copying tensors between devices may slow down your model [Op:MatMul] name: MatMul/
Following the instruction, I tried tfe.enable_eager_execution(tfe.DEVICE_PLACEMENT_SILENT), but it returned another error message (the value returned from tfe.DEVICE_PLACEMENT_SILENT was 2):
Traceback (most recent call last):
File "", line 1, in tfe.enable_eager_execution(tfe.DEVICE_PLACEMENT_SILENT)
File "c:\python\python35_64\lib\site-packages\tensorflow\python\framework\ops.py", line 5229, in enable_eager_execution "config must be a tf.ConfigProto, but got %s" % type(config))
TypeError: config must be a tf.ConfigProto, but got
How to solve the errors? I also don't know how does Tensors can be copied explicitly using .gpu() or .cpu() work.
Thanks.
Thanks to @ash, the revised code works (need to restart the notebook).
import tensorflow as tf
import tensorflow.contrib.eager as tfe
tfe.enable_eager_execution(device_policy=tfe.DEVICE_PLACEMENT_SILENT)
A = tf.constant([[2.0, 0.0], [0.0, 3.0]])
if tf.test.is_gpu_available() > 0:
with tf.device(tf.test.gpu_device_name()):
print(tf.matmul(A, A))
Alternatively (need to restart the notebook),
import tensorflow as tf
import tensorflow.contrib.eager as tfe
tfe.enable_eager_execution()
A = tf.constant([[2.0, 0.0], [0.0, 3.0]])
if tf.test.is_gpu_available() > 0:
with tf.device(tf.test.gpu_device_name()):
A = A.gpu()
print(tf.matmul(A, A))