2017-09-14 62 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之後,官方文檔中會有一些測試來確保你做得很好。