python - 从 CSV 进行 Tensorflow 深度学习...梯度问题

标签 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.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=...

所以,这是我的问题,我是否最好定义自己的成本/优化器算法,或者我只是缺少一种向现有值添加梯度的简单方法?如果我缺少添加渐变的方法,您能给我指出正确的方向吗?

最佳答案

你也初始化局部变量:

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

关于python - 从 CSV 进行 Tensorflow 深度学习...梯度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46353964/

相关文章:

python - 是否可以将 C 预处理器宏可靠地转换为 python 代码?

Python 文本处理 : NLTK and pandas

python - 来自平滑数据的样本

linux - 在 Linux 中打印 CSV 文件中的一列

postgresql - 在 PostgreSQL+PostGIS 上插入数百万行、转换数据和处理它的有效方法

mysql - 索引此 sql 查询以获得最佳优化

python - resampy之类的包为什么显示 'guvectorize() missing 1 required positional argument: '签名''

python - 将嵌套列表逐列写入 CSV

sql-server - 将 SQL Server 数据导出到 CSV

python - 加速 Pandas 过滤