Python Tesorflow从CSV深度学习…渐变带来的麻烦

Python Tesorflow从CSV深度学习…渐变带来的麻烦,python,csv,optimization,tensorflow,gradient,Python,Csv,Optimization,Tensorflow,Gradient,我是Tensorflow的新手。在看了tf文档、多个教程和StackOverflow问题之后,我似乎还没有找到答案 我正在阅读CSV中的功能和标签 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.placehold

我是Tensorflow的新手。在看了tf文档、多个教程和StackOverflow问题之后,我似乎还没有找到答案

我正在阅读CSV中的功能和标签

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')
以下是我收到的错误:

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

ValueError:没有为任何变量提供渐变,请检查图形中是否有不支持渐变的操作,变量之间[“还有初始化局部变量:

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

根据这一建议,错误仍然存在。问题在于成本函数。我仍在试图找出我是否使用了错误的优化器,或者是否有一种简单的方法可以向成本函数添加渐变。尝试将类数设置为大于1。目前,设置n_classes=1是没有意义的,因为e仅对一个类进行分类,这意味着它只能是一个结果。如果要在0和1之间进行分类,则这是两个类。