我正试图让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。还有一张支票,上面写着:
import tensorflow as tf
print(tf.test.is_built_with_cuda())
return 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。
除了缺少内核之外,我不太理解错误消息。我试着在网上查找错误,但一无所获。有没有其他人遇到过这样的事情,并且知道如何解决?
感谢Trojan上尉的评论。我只是试图为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设备打错了吗?