尝试使用具有tensorflow的gpu时出错

时间:2017-09-14 15:15:30

标签: python tensorflow gpu

我正在尝试实施KNN机器学习模型,我无法使用gpu设备运行我的代码。 我不能用CPU设备运行它,因为我的数据库是一个形状为[1500,2,1000,6]的4D numpy数组,并且需要花费大量时间才能完成运行。 已经安装了CUDA和CuDNN。

我的代码是:

# Placeholders
with tf.device('/gpu:0'):

    x_data_train = tf.placeholder(shape=[1500,2,1000, 6], dtype=tf.float32)
    x_data_test = tf.placeholder(shape=[1500,2,1000, 6], dtype=tf.float32)
    y_target_train = tf.placeholder(shape=[1500,1], dtype=tf.float32)
    y_target_test = tf.placeholder(shape=[1500,1], dtype=tf.float32)

    # Declare distance metric
    # L1
distance = tf.reduce_sum(tf.abs(tf.subtract(x_data_train, tf.expand_dims(x_data_test,1))), axis=2)

# L2
#distance = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(x_data_train, tf.expand_dims(x_data_test,1))), reduction_indices=1))

# Predict: Get min distance index (Nearest neighbor)
top_k_xvals, top_k_indices = tf.nn.top_k(tf.negative(distance), k=k)
prediction_indices = tf.gather(y_target_train, top_k_indices)
# Predict the mode category
count_of_predictions = tf.reduce_sum(prediction_indices, axis=1)
prediction = tf.argmax(count_of_predictions, axis=1)

# Calculate how many loops over training data
num_loops = int(np.ceil(len(x_vals_test)/batch_size))

test_output = []
actual_vals = []
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)):
    for i in range(num_loops):
        min_index = i*batch_size
        max_index = min((i+1)*batch_size,len(x_vals_train))
        x_batch = x_vals_test[min_index:max_index]
        y_batch = y_vals_test[min_index:max_index]
        predictions = sess.run(prediction, feed_dict={x_data_train: x_vals_train, x_data_test: x_batch,
                                             y_target_train: y_vals_train, y_target_test: y_batch})
        test_output.extend(predictions)
        actual_vals.extend(np.argmax(y_batch, axis=1))

    accuracy = sum([1./test_size for i in range(test_size) if test_output[i]==actual_vals[i]])
    print('Accuracy on test set: ' + str(accuracy))

错误是:

Device mapping: no known devices.
Traceback (most recent call last):
  line 111, in <module>
    y_target_train: y_vals_train, y_target_test: y_batch})
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 789, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 997, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1132, in _do_run
    target_list, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1152, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation 'Placeholder_3': Operation was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/cpu:0 ]. Make sure the device specification refers to a valid device.
     [[Node: Placeholder_3 = Placeholder[dtype=DT_FLOAT, shape=[1500,1], _device="/device:GPU:0"]()]]

Caused by op u'Placeholder_3', defined at:
 line 83, in <module>
    y_target_test = tf.placeholder(shape=[1500,1], dtype=tf.float32)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 1530, in placeholder
    return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1954, in _placeholder
    name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1269, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Cannot assign a device for operation 'Placeholder_3': Operation was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/cpu:0 ]. Make sure the device specification refers to a valid device.
     [[Node: Placeholder_3 = Placeholder[dtype=DT_FLOAT, shape=[1500,1], _device="/device:GPU:0"]()]]

[Finished in 2.1s with exit code 1]

[
[path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]

1 个答案:

答案 0 :(得分:1)

它看起来像是一个cuda安装问题。在安装cuda之后,在官方文档中有一些测试,以确保你做得很好。

相关问题