Python Tensorflow不会使用批终止
我不熟悉使用tensorflow,在处理它时有些困难。 我尝试使用softmax模型运行一个简单的分类工作,类似于MNIS示例 我尝试创建批量数据,并将dem放入run方法中。 我的第一个方法是使用Python Tensorflow不会使用批终止,python,tensorflow,deep-learning,Python,Tensorflow,Deep Learning,我不熟悉使用tensorflow,在处理它时有些困难。 我尝试使用softmax模型运行一个简单的分类工作,类似于MNIS示例 我尝试创建批量数据,并将dem放入run方法中。 我的第一个方法是使用 sess.run(train_step, feed_dict={x: feature_batch, y_: labels_batch}) 这导致了张量不能被输入的错误 经过一些研究,我发现我应该使用 feat, lab = sess.run([feature_batch, feature_batc
sess.run(train_step, feed_dict={x: feature_batch, y_: labels_batch})
这导致了张量不能被输入的错误
经过一些研究,我发现我应该使用
feat, lab = sess.run([feature_batch, feature_batch])
sess.run(train_step, feed_dict={x: feat, y_: lab})
尝试后,我的脚本不会终止计算,但也不会打印任何错误
有没有人给我一些提示,为什么它不起作用
孔文件看起来像:
def input_pipeline(filename='dataset.csv', batch_size=30, num_epochs=None):
filename_queue = tf.train.string_input_producer([filename], num_epochs=num_epochs, shuffle=True)
features, labels = read_from_cvs(filename_queue)
min_after_dequeue = 10000
capacity = min_after_dequeue + 3 * batch_size
feature_batch, label_batch = tf.train.shuffle_batch(
[features, labels], batch_size=batch_size, capacity=capacity,
min_after_dequeue=min_after_dequeue)
return feature_batch, label_batch
def tensorflow():
x = tf.placeholder(tf.float32, [None, num_attributes])
W = tf.Variable(tf.zeros([num_attributes, num_types]))
b = tf.Variable(tf.zeros([num_types]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, num_types])
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
feature_batch, label_batch = input_pipeline()
for _ in range(1200):
feat, lab = sess.run([feature_batch, feature_batch])
sess.run(train_step, feed_dict={x: feat, y_: lab})
coord.request_stop()
coord.join(threads)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#print(sess.run(accuracy, feed_dict={x: feature_batch, y_: label_batch}))
可以在模型定义中直接使用张量。例如:
def tensorflow():
x, y_ = input_pipeline()
W = tf.Variable(tf.zeros([num_attributes, num_types]))
b = tf.Variable(tf.zeros([num_types]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for _ in range(1200):
sess.run(train_step)
#...omit
features_placeholder = tf.placeholder(...)
labels_placeholder = tf.placeholder(...)
x, y_ = tf.train.shuffle_batch(
[features_placeholder, labels_placeholder], batch_size=batch_size, capacity=capacity,
min_after_dequeue=min_after_dequeue)
W = tf.Variable(tf.zeros([num_attributes, num_types]))
b = tf.Variable(tf.zeros([num_types]))
#...omit
for _ in range(1200):
sess.run(train_step, feed_dict={features_placeholder: ..., labels_placeholder: ...})
或者您应该在tf.train.shuffle\u batch
中使用占位符。例如:
def tensorflow():
x, y_ = input_pipeline()
W = tf.Variable(tf.zeros([num_attributes, num_types]))
b = tf.Variable(tf.zeros([num_types]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for _ in range(1200):
sess.run(train_step)
#...omit
features_placeholder = tf.placeholder(...)
labels_placeholder = tf.placeholder(...)
x, y_ = tf.train.shuffle_batch(
[features_placeholder, labels_placeholder], batch_size=batch_size, capacity=capacity,
min_after_dequeue=min_after_dequeue)
W = tf.Variable(tf.zeros([num_attributes, num_types]))
b = tf.Variable(tf.zeros([num_types]))
#...omit
for _ in range(1200):
sess.run(train_step, feed_dict={features_placeholder: ..., labels_placeholder: ...})
可以在模型定义中直接使用张量。例如:
def tensorflow():
x, y_ = input_pipeline()
W = tf.Variable(tf.zeros([num_attributes, num_types]))
b = tf.Variable(tf.zeros([num_types]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for _ in range(1200):
sess.run(train_step)
#...omit
features_placeholder = tf.placeholder(...)
labels_placeholder = tf.placeholder(...)
x, y_ = tf.train.shuffle_batch(
[features_placeholder, labels_placeholder], batch_size=batch_size, capacity=capacity,
min_after_dequeue=min_after_dequeue)
W = tf.Variable(tf.zeros([num_attributes, num_types]))
b = tf.Variable(tf.zeros([num_types]))
#...omit
for _ in range(1200):
sess.run(train_step, feed_dict={features_placeholder: ..., labels_placeholder: ...})
或者您应该在tf.train.shuffle\u batch
中使用占位符。例如:
def tensorflow():
x, y_ = input_pipeline()
W = tf.Variable(tf.zeros([num_attributes, num_types]))
b = tf.Variable(tf.zeros([num_types]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for _ in range(1200):
sess.run(train_step)
#...omit
features_placeholder = tf.placeholder(...)
labels_placeholder = tf.placeholder(...)
x, y_ = tf.train.shuffle_batch(
[features_placeholder, labels_placeholder], batch_size=batch_size, capacity=capacity,
min_after_dequeue=min_after_dequeue)
W = tf.Variable(tf.zeros([num_attributes, num_types]))
b = tf.Variable(tf.zeros([num_types]))
#...omit
for _ in range(1200):
sess.run(train_step, feed_dict={features_placeholder: ..., labels_placeholder: ...})
我怀疑问题在于这两行的顺序:
threads = tf.train.start_queue_runners(coord=coord)
feature_batch, label_batch = input_pipeline()
调用将为所有已定义到该点的输入管道阶段启动后台线程。调用input\u pipeline()
将创建两个新的输入管道阶段(在对和的调用中)。这意味着两个新阶段的后台线程将不会启动,程序将挂起
解决方案是颠倒这些行的顺序:
feature_batch, label_batch = input_pipeline()
threads = tf.train.start_queue_runners(coord=coord)
我怀疑问题在于这两行的顺序:
threads = tf.train.start_queue_runners(coord=coord)
feature_batch, label_batch = input_pipeline()
调用将为所有已定义到该点的输入管道阶段启动后台线程。调用input\u pipeline()
将创建两个新的输入管道阶段(在对和的调用中)。这意味着两个新阶段的后台线程将不会启动,程序将挂起
解决方案是颠倒这些行的顺序:
feature_batch, label_batch = input_pipeline()
threads = tf.train.start_queue_runners(coord=coord)
我将如何编辑您的第一个解决方案以使用培训和测试数据集?没有调用input_pipeline()两次以上?我有单独的培训和测试数据文件。我认为获得两个批处理对的最简单方法是调用两次input_管道。我如何编辑您的第一个解决方案以处理培训和测试数据集?没有调用input_pipeline()两次以上?我有单独的培训和测试数据文件。我认为获得两个批处理对的最简单方法是调用input_管道两次。