【问题标题】:Can't seem to use GPU with tensorflow似乎无法将 GPU 与 tensorflow 一起使用
【发布时间】:2021-09-25 20:36:25
【问题描述】:

我正在尝试让 tensorflow 与我的 GPU 对话,这样我就可以更快地对我的神经网络进行建模。我已经使用 pip 安装了 tensorflow-gpu 和 cuDNN,并且我已经安装了驱动程序和 cuda。我检查了 tensorflow 是否可以使用以下方式查看我的 GPU:

from tensorflow.python.client import device_lib 
print(device_lib.list_local_devices()) 

它返回的:

[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 5916901003862901746
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 4848943104
locality {
  bus_id: 1
  links {
  }
}
incarnation: 16957123506888322798
physical_device_desc: "device: 0, name: NVIDIA GeForce GTX 1060 6GB, pci bus id: 
0000:01:00.0, compute capability: 6.1"
]

所以我猜它找到了我的 GPU。并使用以下命令检查 cuda:

import tensorflow as tf
print(tf.test.is_built_with_cuda())

返回 True 所以它找到了 cuda 安装。但是当我尝试初始化它时:

sess = tf.compat.v1.Session()                                
tf.compat.v1.disable_eager_execution()
sess.run(tf.compat.v1.global_variables_initializer())

我得到一个无效的论点:

InvalidArgumentError: Cannot assign a device for operation assert_greater_3/Assert/Const: 
Could not satisfy explicit device specification '/device:GPU:0' because no supported kernel 
for GPU devices is available.
Colocation Debug Info:
Colocation group had the following types and supported devices: 
Root Member(assigned_device_name_index_=-1 requested_device_name_='/device:GPU:0' 
assigned_device_name_='' resource_device_name_='' supported_device_types_=[CPU] 
possible_devices_=[]
Const: CPU 

Colocation members, user-requested devices, and framework assigned devices, if any:
  assert_greater_3/Assert/Const (Const) /device:GPU:0 

然后它回退到使用我的 CPU。

除了缺少内核外,我不太了解错误消息。我试图在网上寻找错误,但没有任何结果。有没有其他人遇到过这样的事情并且知道方法?

【问题讨论】:

  • 如果可能的话,我建议忽略TF 1.x,而改用TF 2.X,最好是2.5
  • @Chi 你用的是哪个版本的TensorFlow?
  • 嗨 Swaroop,它是 tf 2.5
  • import tensorflow 之后,您是否收到任何警告或消息?
  • 嗨,Dominik,不,一切都好。

标签: python tensorflow gpu


【解决方案1】:

感谢特洛伊船长的评论。我只是想调整 TF2.x 的代码(编码经验有限),发现如果我使用以下方式进行升级:

pip install --upgrade tensorflow-gpu

虽然我仍然无法让 tf 与我的 GPU 对话并且不确定它是否是 TF1.x/TF2.x 问题,但错误消失了。我跑了:

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)

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

print (sess.run)

它返回了:

Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: NVIDIA GeForce 
GTX 1060 6GB, pci bus id: 0000:01:00.0, compute capability: 6.1

<bound method BaseSession.run of <tensorflow.python.client.session.Session 
object at 0x00000217323C0B50>>

所以它能够映射我的 GPU。

但我无法获得我的原始代码:

with tf.device('/gpu:0'):
    feature_columns = [tf.feature_column.numeric_column('image', shape = [255, 255, 3])]
    classifier = tf.estimator.DNNClassifier(feature_columns = feature_columns,                                                     
                                            hidden_units = [128, 64, 32, 16],                                                      
                                            activation_fn = tf.nn.relu,
                                            n_classes = 3,
                                            model_dir = 'model',
                                            optimizer = tf.compat.v1.train.RMSPropOptimizer(learning_rate=0.1, decay=1e-6),        
                                            dropout = 0.1)
classifier.train(input_fn = train_input_fn, steps = 1000)                                                                        
result = classifier.evaluate(input_fn = val_input_fn)
print(result);
print('Classification accuracy: {0:.2%}'.format(result['accuracy'])) 

仍然与我的 GPU 对话。它仍然回落到我的 CPU 上。有人知道我是不是把我的 GPU 设备叫错了吗?

【讨论】:

  • 这应该是您问题的答案吗?
  • 我猜它回答了错误消息,但不是原来的问题 - 我应该为它打开一个新线程吗?
猜你喜欢
  • 2017-03-07
  • 2017-05-02
  • 1970-01-01
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-17
  • 2016-04-03
相关资源
最近更新 更多