0

Je suis suivants le long d'un tutoriel tensorflow sur youtube et je l'ai couru dans une erreur:Obtenir une erreur dans tensorflow

Voici mon code:

import tensorflow as tf 

from tensorflow.examples.tutorials.mnist import input_data 
mnist = input_data.read_data_sets("/tmp/data/",one_hot=True) 

n_nodes_hl1 = 500 
n_nodes_hl2 = 500 
n_nodes_hl3 = 500 

n_classes = 10 
batch_size = 100 

# height x width 
x = tf.placeholder("float") 
y = tf.placeholder("float") 

def neural_network_model(data): 
    hidden_1_layer = {"weights":tf.Variable(tf.random_normal([784,n_nodes_hl1])),"biases":tf.Variable(tf.random_normal([n_nodes_hl1]))} 

    hidden_2_layer = {"weights":tf.Variable(tf.random_normal([n_nodes_hl1,n_nodes_hl2])),"biases":tf.Variable(tf.random_normal([n_nodes_hl2]))} 

    hidden_3_layer = {"weights":tf.Variable(tf.random_normal([n_nodes_hl2,n_nodes_hl3])),"biases":tf.Variable(tf.random_normal([n_nodes_hl3]))} 

    output_layer = {"weights":tf.Variable(tf.random_normal([n_nodes_hl3,n_classes])),"biases":tf.Variable(tf.random_normal([n_classes]))} 

    # (input_data * weights + biases 
    l1 = tf.add(tf.matmul(data,hidden_1_layer["weights"]), hidden_1_layer["biases"]) 
    l1 = tf.nn.relu(l1) 

    l2 = tf.add(tf.matmul(l1,hidden_2_layer["weights"]), hidden_2_layer["biases"]) 
    l2 = tf.nn.relu(l2) 

    l3 = tf.add(tf.matmul(l2,hidden_3_layer["weights"]), hidden_3_layer["biases"]) 
    l3 = tf.nn.relu(l3) 

    output = tf.matmul(l3,output_layer["weights"]) + output_layer["biases"] 

    return output 

def train_neural_network(x): 
    prediction = neural_network_model(x) 
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=prediction,logits=y)) 
    optimizer = tf.train.AdamOptimizer().minimize(cost) 

    # cycles of 
    hm_epochs = 10 

    with tf.Session() as sess: 
     sess.run(tf.initialize_all_variables()) 

     for epoch in range(hm_epoch): 
      epoch_loss = 0 
      for i in range(int(mnist.train.num.examples/batch_size)): 
       epoch_x, epoch_y = mnist.train.next_batch(batch_size) 
       i, c = sess.run([optimizer,cost],feed_dict = {x:epoch_x,y:epoch_y}) 
       epoch_loss += c 
      print("Epoch", epoch, "completed out of", hm_epochs, "loss:" , epoch_loss) 
     correct = tf.equal(tf.argmax(prediction,1), tf.argmax(y1)) 

     accuracy = tf.reduce_mean(tf,cast(correct, "float")) 
     print("Accuracy", accuracy.eval({x:mnist.test.images, y:mnist.test.labels})) 



train_neural_network(x) 

et, c'est l'erreur:

Traceback (most recent call last): 
    File "/home/markus/Documents/NN-Tutorial-04.py", line 65, in <module> 
    train_neural_network(x) 
    File "/home/markus/Documents/NN-Tutorial-04.py", line 43, in train_neural_network 
    optimizer = tf.train.AdamOptimizer().minimize(cost) 
    File "/home/markus/.local/lib/python3.5/site-packages/tensorflow/python/training/optimizer.py", line 322, in minimize 
    ([str(v) for _, v in grads_and_vars], loss)) 
ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients, between variables ["<tf.Variable 'Variable:0' shape=(784, 500) dtype=float32_ref>", "<tf.Variable 'Variable_1:0' shape=(500,) dtype=float32_ref>", "<tf.Variable 'Variable_2:0' shape=(500, 500) dtype=float32_ref>", "<tf.Variable 'Variable_3:0' shape=(500,) dtype=float32_ref>", "<tf.Variable 'Variable_4:0' shape=(500, 500) dtype=float32_ref>", "<tf.Variable 'Variable_5:0' shape=(500,) dtype=float32_ref>", "<tf.Variable 'Variable_6:0' shape=(500, 10) dtype=float32_ref>", "<tf.Variable 'Variable_7:0' shape=(10,) dtype=float32_ref>"] and loss Tensor("Mean:0", dtype=float32). 

Toute aide serait la bienvenue, c'était la vidéo que je suivais (https://www.youtube.com/watch?v=PwAGxqrXSCs).

IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII

Répondre

1

Vous avez des étiquettes confuses et logits dans l'appel à tf.nn.softmax_cross_entropy_with_logits(). De cette façon, tensorflow essayera probablement d'assigner des dégradés aux étiquettes (je ne pense pas que cela devrait être possible, parce que ce sont les espaces réservés), et les variables du graphique n'obtiendront aucun dégradé. C'est incorrect.

Au lieu de

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
     labels=prediction,logits=y) 
)  

vous devriez écrire

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
     labels=y,logits=prediction) 
) 

De plus, il y a des erreurs de syntaxe et vous écrasez également la variable i (ce qui est le compteur de la boucle extérieure), dans le boucle interne avec l'optimiseur renvoyé. Pour l'instant, vous n'utilisez pas i, mais si vous le faites, il serait difficile de diagnostiquer les bogues. Renommez simplement la variable renvoyée en _ (convention python pour les valeurs de retour inutilisées):

_, c = sess.run([optimizer,cost],feed_dict = {x:epoch_x,y:epoch_y})