2017-09-21 3 views
1

Je suis nouveau sur Tensorflow. Après avoir regardé la documentation de tf, plusieurs tutoriels, et les questions StackOverflow, je n'arrive pas à trouver de réponse, pour le moment. Je lis dans les caractéristiques et l'étiquette d'un fichier CSV.Tesorflow Apprendre en profondeur à partir de CSV ... problème avec les gradients

import tensorflow as tf 

input_nodes = 13 


n_nodes_hl1 = 25 
n_nodes_hl2 = 25 
n_nodes_hl3 = 25 

n_classes = 1 

x = tf.placeholder('float', [None, input_nodes]) 
y = tf.placeholder('float') 


def neural_network_model(data): 
    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([input_nodes,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]))} 

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

    #Obtain data from CSV File 
    #Training data looks like this... The first 13 columns are inputs, the last column is the result 
    #1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0 
    #0.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.0 
    train_filename_queue = tf.train.string_input_producer(["training_data.csv"]) 
    reader = tf.TextLineReader() 
    key, value = reader.read(train_filename_queue) 

    #Set defaults 
    record_defaults = [[1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0], [1.0]] 
    col01, col02, col03, col04, col05, col06, col07, col08, col09, col10, col11, col12, col13, outcome = tf.decode_csv(value, record_defaults=record_defaults) 
    features = tf.stack([col01, col02, col03, col04, col05, col06, col07, col08, col09, col10, col11, col12, col13]) 
    outputs = tf.stack([outcome]) 


    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) #this line throws the error below 

    hm_epochs = 10 

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

     coord = tf.train.Coordinator() 
     threads = tf.train.start_queue_runners(coord=coord) 
     for epoch in range(hm_epochs): 
      epoch_loss = 0 
      for _ in range(2): 
       _, c = sess.run([optimizer], feed_dict= {x: features, y: outputs}) 
       epoch_loss += c 
      print('Epoch', epoch + 1, 'completed out of', hm_epochs, 'loss', epoch_loss) 

     coord.request_stop() 
     coord.join(threads) 

print('Beginning to Train') 
train_neural_network(x) 
print('Done') 

Voici l'erreur que je reçois:

ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients, between variables ["<tf.Variable 'Variable:0' shape=... 

Alors, voici ma question, suis-je mieux définir mon propre algorithme coût/optimiseur ou suis-je manque tout simplement un moyen simple d'ajouter des gradients aux valeurs existantes? Si la façon d'ajouter des dégradés me manque, pouvez-vous me diriger dans la bonne direction?

Répondre

1

Thy variables locales d'initialisation ainsi:

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    sess.run(tf.local_variables_initializer()) 
+0

Avec cette suggestion, l'erreur présente encore. Le problème est avec la fonction de coût. J'essaie toujours de savoir si j'utilise un optimiseur incorrect ou s'il existe un moyen facile d'ajouter des dégradés à la fonction de coût. – AustinJ

+0

Essayez de définir le nombre de classes à plus de 1. Actuellement, vous avez défini n_classes = 1, ce qui n'a pas de sens car vous ne classifiez qu'une classe, ce qui signifie qu'il ne peut s'agir que d'un seul résultat. Si vous voulez classer entre 0 et 1, ce sont deux classes. –