2017-02-07 51 views
1

我試圖在tensorflow上進行第一次嘗試 - 我試圖通過輸入矢量來學習線性迴歸10維,X和輸出一個標量Y.具體而言,我試圖使用基於閉式梯度的解決方案。Tensorflow InvalidArgumentError(參見上面的回溯):不兼容的形狀:[10000,10]與[10000]

我收到以下錯誤,我不確定我做錯了什麼。任何指向我正確的方向將不勝感激!

PS C:\Users\Dave\Documents\School\Deep Learning\Assignment_1> python test1.py 
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_loader.cc:128] successfu 
lly opened CUDA library cublas64_80.dll locally 
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_loader.cc:128] successfu 
lly opened CUDA library cudnn64_5.dll locally 
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_loader.cc:128] successfu 
lly opened CUDA library cufft64_80.dll locally 
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_loader.cc:128] successfu 
lly opened CUDA library nvcuda.dll locally 
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\stream_executor\dso_loader.cc:128] successfu 
lly opened CUDA library curand64_80.dll locally 
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:885] F 
ound device 0 with properties: 
name: GeForce GTX 1080 
major: 6 minor: 1 memoryClockRate (GHz) 1.86 
pciBusID 0000:01:00.0 
Total memory: 8.00GiB 
Free memory: 6.63GiB 
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:906] D 
MA: 0 
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:916] 0 
: Y 
I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:975] C 
reating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0) 
Traceback (most recent call last): 
    File "C:\Users\Dave\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 10 
    return fn(*args) 
    File "C:\Users\Dave\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 10 
    status, run_metadata) 
    File "C:\Users\Dave\AppData\Local\Programs\Python\Python35\lib\contextlib.py", line 66, in __exit__ 
    next(self.gen) 
    File "C:\Users\Dave\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", 
n_on_not_ok_status 
    pywrap_tensorflow.TF_GetCode(status)) 
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [10000,10] vs. [10000] 
     [[Node: sub = Sub[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"](Add, _recv_Placeholder_1_0/_7)]] 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "test1.py", line 43, in <module> 
    c = sess.run(cost, feed_dict={X: train_X, Y: train_Y}) 
    File "C:\Users\Dave\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 76 
    run_metadata_ptr) 
    File "C:\Users\Dave\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 96 
    feed_dict_string, options, run_metadata) 
    File "C:\Users\Dave\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 10 
    target_list, options, run_metadata) 
    File "C:\Users\Dave\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 10 
    raise type(e)(node_def, op, message) 
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [10000,10] vs. [10000] 
     [[Node: sub = Sub[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"](Add, _recv_Placeholder_1_0/_7)]] 

Caused by op 'sub', defined at: 
    File "test1.py", line 25, in <module> 
    cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples) 
    File "C:\Users\Dave\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\math_ops.py", line 814, 
    return func(x, y, name=name) 
    File "C:\Users\Dave\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 
    result = _op_def_lib.apply_op("Sub", x=x, y=y, name=name) 
    File "C:\Users\Dave\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py 
    op_def=op_def) 
    File "C:\Users\Dave\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 224 
    original_op=self._default_original_op, op_def=op_def) 
    File "C:\Users\Dave\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 112 
    self._traceback = _extract_stack() 

InvalidArgumentError (see above for traceback): Incompatible shapes: [10000,10] vs. [10000] 
     [[Node: sub = Sub[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"](Add, _recv_Placeholder_1_0/_7)]] 

這裏是我的代碼:

import numpy as np 
import tensorflow as tf 
import matplotlib.pyplot as plt 
rng = np.random 

#from IPython import get_ipython 
#get_ipython().run_line_magic('matplotlib', 'inline') 

learning_rate = 0.01 
training_epochs = 1000 
display_step = 50 

train_X = np.loadtxt('data.txt', usecols=[0,1,2,3,4,5,6,7,8,9]) 
train_Y = np.loadtxt('data.txt', usecols=[10]) 
n_samples = train_X.shape[0] 
X = tf.placeholder(tf.float32) 
Y = tf.placeholder(tf.float32) 
W = tf.Variable(rng.randn(), name = "weight") 
b = tf.Variable(rng.randn(), name = "bias") 

#build the model 
pred = tf.add(tf.mul(X,W), b) 

#mean squared error 
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples) 

#gradient descent 
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 

#initialize the variables 
init = tf.global_variables_initializer() 
#launch the graph 
with tf.Session() as sess: 
    sess.run(init) 

    #fit training data 
    for epoch in range(training_epochs): 
     for (x, y) in zip(train_X, train_Y): 
      sess.run(optimizer, feed_dict = {X: x, Y: y}) 

     #display logs 
     if (epoch+1) % display_step ==0: 
      c = sess.run(cost, feed_dict={X: train_X, Y: train_Y}) 
      # print "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f".format(c), \ 
       #"W=", sess.run(W), "b=" sess.run(b) 

    #print "Optimization done" 
    training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y}) 
    #print "Training cost=", training_cost, "W=", sess.run(W), "b=" sess.run(b), '\n' 

    #display graphically 
    plt.plot(train_X, train_Y, 'ro', label = 'Orig data') 
    plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label = 'Fitted Line') 
    plt.legend() 
    plt.show() 

回答

1

我不知道tensorflow,我不知道到底發生了什麼事情在你的代碼,所以我試圖做一個基於猜測關於numpy的行爲。我想添加這個作爲評論,但它太長了。

當加載訓練數據,train_X是形狀(10000,10)的,因爲它具有10列(它是一個二維數組),而train_Y是形狀(10000,)的,因爲它是一個單一的柱(它是一個二維數組)。這兩種形狀不能一起廣播,因此predYpred-Y中具有不兼容的形狀。您需要爲此轉置train_X,或者將train_Y轉換爲(10000,1)形狀的數組,以使它們兼容。前者你可以通過傳遞unpack=Truenp.loadtxt;後者有類似train_Y = train_Y[:,None](至少我的懷疑是unpack=True在這種情況下不起作用,但無論如何都值得一試)。

但是如果你換了你的陣列,你需要小心你的訓練循環仍然有效。目前,您的(10000,10)形陣列相當於length-ten列表的length-10000列表,而您的(10000,)形陣列相當於一個長度爲10000的列表。這些很可能是zip在一起。如果你轉,說train_X使廣播的工作,那麼你就需要修改這個循環:

for (x, y) in zip(train_X.T, train_Y): 
     sess.run(optimizer, feed_dict = {X: x, Y: y}) 

現在回想起來,我只是大概的意思是,你需要轉才能回train_X,以確保第一維度的train_Xtrain_Y與壓縮匹配。

換位也可能影響後續步驟,如繪圖。如果有任何奇怪的輸出或錯誤,則需要轉回。或者甚至更好:只爲特定於tensorflow的操作進行轉置(但我不熟悉這部分,所以我不知道是否以及如何通過慣用方式來完成)。

相關問題