2017-02-07 3 views
1

Ich versuche meinen ersten Versuch mit Tensorflow - ich versuche einen linearen Regressor zu lernen, indem ich einen Eingabevektor von 10 Dimensionen, X und Ausgabe eines Skalars, Y. Insbesondere versuche ich, die auf einem geschlossenen Gradienten basierende Lösung zu verwenden.Tensorflow InvalidArgumentError (siehe oben für Traceback): Inkompatible Shapes: [10000,10] vs. [10000]

Ich habe den folgenden Fehler erhalten und ich bin mir nicht sicher, was ich falsch gemacht habe. Alles, was mich in die richtige Richtung weist, wäre sehr willkommen!

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)]] 

Hier ist mein Code:

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() 

Antwort

1

Ich weiß nicht, tensorflow und ich bin nicht sicher genau, was in Ihrem Code vor sich geht, so versuche ich, eine Vermutung zu machen, basierend auf numpys Verhalten. Ich wollte dies als Kommentar hinzufügen, aber es wurde zu lang.

Wenn Sie die Trainingsdaten geladen werden, ist train_X der Form (10000,10), da es 10 Spalten hat (es ist ein 2D-Array), während train_Y der Form ist (10000,), da es sich um eine einzelne Spalte (es ist ein 2D-Array). Diese beiden Formen können nicht zusammen übertragen werden, daher haben pred und Y in pred-Y inkompatible Formen. Sie müssen dazu entweder train_X transponieren oder die train_Y in ein Array mit der Form (10000,1) umwandeln, damit sie kompatibel sind. Ersteres kann man tun, indem man unpack=True an np.loadtxt übergibt; Letzteres mit etwas wie train_Y = train_Y[:,None] (zumindest mein Verdacht ist, dass unpack=True wird in diesem Fall nicht helfen, aber es ist einen Versuch auf jeden Fall einen Versuch wert).

Aber wenn Sie Ihre Arrays transponieren, müssen Sie darauf achten, dass Ihre Trainingsschleife immer noch funktioniert. Momentan entspricht Ihr (10000,10) -förmiges Array einer Länge-10000-Liste von Längen-Zehn-Listen, während Ihr (10000,) -förmiges Array einer einzelnen Länge-10000-Liste entspricht. Diese können schön zusammen zip ped zusammen sein. Wenn Sie transponieren, sagen train_X zu machen Arbeit überträgt, dann werden Sie diese Schleife ändern müssen:

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

Im Nachhinein, ich habe gerade wahrscheinlich bedeuten, dass Sie zurück train_X um transponieren müssen, um sicherzustellen, dass die ersten Dimensionen von train_X und train_Y zum Zippen zusammenpassen.

Die Transposition kann auch spätere Schritte beeinflussen, z. B. Plotten. Falls es seltsame Ausgaben oder Fehler gibt, müssen Sie diese zurück transponieren. Oder noch besser: Transponiere nur für die Tensorfluss-spezifischen Operationen (aber ich bin mit diesem Teil nicht vertraut, also weiß ich nicht, ob und wie das idiomatisch gemacht werden könnte).

Verwandte Themen