gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:44:27 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com