Tensorflow GPU适用于linux shell,但不适用于python virtualen

2024-03-28 21:57:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一台电脑与TensorFlow 1.13.1 GPU版本和CUDA 10.2安装。为了测试它,我运行了一个非常简单的脚本

 import tensorflow as tf
    with tf.device('/gpu:0'):
    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
    c = tf.matmul(a, b)
    with tf.Session() as sess:
    print (sess.run(c))

在我的Linux shell上运行它时,下面是输出

python script_teste.py 
2019-10-15 15:06:40.148532: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-10-15 15:06:40.230094: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-10-15 15:06:40.230482: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x56500662d8b0 executing computations on platform CUDA. Devices:
2019-10-15 15:06:40.230494: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): GeForce RTX 2080 Ti, Compute Capability 7.5
2019-10-15 15:06:40.251376: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3600000000 Hz
2019-10-15 15:06:40.251900: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x565005855770 executing computations on platform Host. Devices:
2019-10-15 15:06:40.251911: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): <undefined>, <undefined>
2019-10-15 15:06:40.251994: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1433] Found device 0 with properties: 
name: GeForce RTX 2080 Ti major: 7 minor: 5 memoryClockRate(GHz): 1.545
pciBusID: 0000:01:00.0
totalMemory: 10.76GiB freeMemory: 9.91GiB
2019-10-15 15:06:40.252005: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0
2019-10-15 15:06:40.252387: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-10-15 15:06:40.252395: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990]      0 
2019-10-15 15:06:40.252399: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003] 0:   N 
2019-10-15 15:06:40.252443: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 9645 MB memory) -> physical GPU (device: 0, name: GeForce RTX 2080 Ti, pci bus id: 0000:01:00.0, compute capability: 7.5)
[[22. 28.]
 [49. 64.]]

在虚拟环境中运行时,输出如下

(venv) me@me-server:~/PhD-Market-Nets$ python script_teste.py 
2019-10-15 15:12:15.534641: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-10-15 15:12:15.555442: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3600000000 Hz
2019-10-15 15:12:15.556054: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x13de4b0 executing computations on platform Host. Devices:
2019-10-15 15:12:15.556080: I tensorflow/compiler/xla/service/service.cc:158]   StreamExecutor device (0): <undefined>, <undefined>
Traceback (most recent call last):
  File "/home/me/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1334, in _do_call
    return fn(*args)
  File "/home/me/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1317, in _run_fn
    self._extend_graph()
  File "/home/me/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1352, in _extend_graph
    tf_session.ExtendSession(self._session)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation MatMul: {{node MatMul}}was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0, /job:localhost/replica:0/task:0/device:XLA_CPU:0 ]. Make sure the device specification refers to a valid device. The requested device appears to be a GPU, but CUDA is not enabled.
     [[{{node MatMul}}]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "script_teste.py", line 7, in <module>
    print (sess.run(c))
  File "/home/me/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 929, in run
    run_metadata_ptr)
  File "/home/me/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1152, in _run
    feed_dict_tensor, options, run_metadata)
  File "/home/me/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1328, in _do_run
    run_metadata)
  File "/home/me/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1348, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation MatMul: node MatMul (defined at script_teste.py:5) was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0, /job:localhost/replica:0/task:0/device:XLA_CPU:0 ]. Make sure the device specification refers to a valid device. The requested device appears to be a GPU, but CUDA is not enabled.
     [[node MatMul (defined at script_teste.py:5) ]]

Caused by op 'MatMul', defined at:
  File "script_test.py", line 5, in <module>
    c = tf.matmul(a, b)
  File "/home/me/.local/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py", line 2455, in matmul
    a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
  File "/home/me/.local/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py", line 5333, in mat_mul
    name=name)
  File "/home/me/.local/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
    op_def=op_def)
  File "/home/me/.local/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "/home/me/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3300, in create_op
    op_def=op_def)
  File "/home/me/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1801, in __init__
    self._traceback = tf_stack.extract_stack()

InvalidArgumentError (see above for traceback): Cannot assign a device for operation MatMul: node MatMul (defined at script_teste.py:5) was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0, /job:localhost/replica:0/task:0/device:XLA_CPU:0 ]. Make sure the device specification refers to a valid device. The requested device appears to be a GPU, but CUDA is not enabled.
     [[node MatMul (defined at script_teste.py:5) ]]

所以,似乎我需要配置tensorflow gpu,cuda等在虚拟环境中的方式一样,我从外面做呢?在虚拟环境中我错过了哪些步骤?你知道吗


Tags: inpyhomegpudevicelibpackageslocal