我很抱歉,但我仍然遇到一些处理张量流的问题,希望能在这里得到一些帮助....
我做了Sraw在Tensorflow does not terminate using batches建议的第一个解决方案,但我现在收到以下错误
OutOfRangeError (see above for traceback): RandomShuffleQueue '_1_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 30, current size 0)
[[Node: shuffle_batch = QueueDequeueManyV2[component_types=[DT_FLOAT, DT_FLOAT], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](shuffle_batch/random_shuffle_queue, shuffle_batch/n)]]
我已经尝试过使用
了try:
while not coord.should_stop():
# Run training steps or whatever
sess.run(train_op)
except tf.errors.OutOfRangeError:
但它不起作用。 文件dataset.csv已存在。但我无法弄清楚是否有办法检查我的脚本是否正确读取了它。
孔文件看起来像
from __future__ import print_function
import numpy as np
import tensorflow as tf
from urllib.request import urlopen
num_attributes = 15
num_types = 7
def read_from_cvs(filename_queue):
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
record_defaults = [[] for col in range((num_attributes+2))] # no defaults
attributes = tf.decode_csv(value, record_defaults=record_defaults)
features = tf.stack(attributes[1:-1])
#labels = tf.stack(attributes[-1])
labels = tf.one_hot(tf.cast(tf.stack(attributes[-1]), tf.uint8), num_types)
return features, labels
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 = batch_size
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, y_ = input_pipeline()
W = tf.Variable(tf.zeros([num_attributes, num_types]), name="weights")
b = tf.Variable(tf.zeros([num_types]), name="bias")
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)
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, ))
sess.close()
def main():
tensorflow()
if __name__ == '__main__':
main()
我希望有些人可以提供一些帮助。
修改 我发现问题是我的csv文件包含格式失败。