2017-01-24 5 views
0

Ich studiere derzeit TensorFlow. Ich versuche ein NN zu erstellen, das ein Vorhersagemodell genau beurteilen und ihm eine Punktzahl zuweisen kann. Mein Plan ist jetzt, Punkte von bereits existierenden Programmen zu kombinieren, sie durch ein mlp laufen zu lassen und sie mit wahren Werten zu vergleichen. Ich habe mit den MNIST-Daten herumgespielt und versuche das Gelernte auf mein Projekt anzuwenden. Leider habe ich ein ProblemTensorflow ValueError: Dimension 0 in beiden Formen muss gleich sein

def multilayer_perceptron(x, w1): 
    # Hidden layer with RELU activation 
    layer_1 = tf.matmul(x, w1) 
    layer_1 = tf.nn.relu(layer_1) 
    # Output layer with linear activation 
    #out_layer = tf.matmul(layer_1, w2) 
    return layer_1 

def my_mlp (trainer, trainer_awn, learning_rate, training_epochs, n_hidden, n_input, n_output): 
trX, trY= trainer, trainer_awn 
#create placeholders 
x = tf.placeholder(tf.float32, shape=[9517, 5]) 
y_ = tf.placeholder(tf.float32, shape=[9517, ]) 
#create initial weights 
w1 = tf.Variable(tf.zeros([5, 1])) 
#predicted class and loss function 
y = multilayer_perceptron(x, w1) 
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y, y_)) 
#training 
train_step = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cross_entropy) 
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) 
with tf.Session() as sess: 
    # you need to initialize all variables 
    sess.run(tf.initialize_all_variables()) 
    print("1") 
    for i in range(training_epochs + 1): 
     sess.run([train_step], feed_dict={x: [trX['V7'], trX['V8'], trX['V9'], trX['V10'], trX['V12']], y_: trY}) 
return 

Der Code mir diesen Fehler

ValueError: Dimension 0 in both shapes must be equal, but are 9517 and 1 

Dieser Fehler tritt auf, wenn gibt die Linie für cross_entropy läuft. Ich verstehe nicht, warum das so ist, wenn du mehr Informationen brauchst, ich würde es dir gerne geben.

Antwort

0

In Ihrem Fall hat y Form [9517, 1], während y_ Form hat [9517]. Sie sind nicht kampierbar. Bitte versuchen Sie y_ mit tf.reshape umzuformen (y_, [-1, 1])

+0

Vielen Dank das hat perfekt funktioniert! –

Verwandte Themen