2017-09-14 76 views
1

我想实现KNN机器学习模型,我不能使用GPU设备运行我的代码。 我不能同时运行CPU设备,因为我的数据库是一个形状为[1500,2,1000,6]的4D numpy数组,它需要很长时间才能完成运行。 已经安装了CUDA和CuDNN。错误当试图使用GPU与张量流

我的代码是:

# 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

您是否安装了支持GPU的Tensorflow? –

+0

是的,它已经安装。 – user37353

+0

我解决了这个问题,我安装了cuda和cudnn,尽管我确信我已经正确安装了它。它现在无论如何都有效;) – user37353

回答

0

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