源程序文件下载[1]test_tf_train_batch.csv[2]
import tensorflow as tf
BATCH_SIZE = 400
NUM_THREADS = 2
MAX_NUM = 500
def read_data(file_queue):
reader = tf.TextLineReader(skip_header_lines=1)
key, value = reader.read(file_queue)
defaults = [[0], [0.], [0.]]
NUM, C, Tensile = tf.decode_csv(value, defaults)
vertor_example = tf.stack([C])
vertor_label = tf.stack([Tensile])
vertor_num = tf.stack([NUM])
return vertor_example, vertor_label, vertor_num
def create_pipeline(filename, batch_size, num_threads):
file_queue = tf.train.string_input_producer([filename]) # 设置文件名队列
example, label, no = read_data(file_queue) # 读取数据和标签
example_batch, label_batch, no_batch = tf.train.batch(
[example, label, no], batch_size=batch_size, num_threads=num_threads, capacity=MAX_NUM)
return example_batch, label_batch, no_batch
x_train_batch, y_train_batch, no_train_batch = create_pipeline('test_tf_train_batch.csv', batch_size=BATCH_SIZE,
num_threads=NUM_THREADS)
init_op = tf.global_variables_initializer()
local_init_op = tf.local_variables_initializer()
with tf.Session() as sess:
sess.run(local_init_op)
sess.run(init_op)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
example, label, num = sess.run([x_train_batch, y_train_batch, no_train_batch])
print(example)
print(label)
print(num)
coord.request_stop()
coord.join(threads)
我们将 csv 文件中的真实 Tensile 值放在第一列,将使用 tf.train.batch 函数得到的 Tensile 和 no 分别放在第二列和第三列
TureTensile | FalseTensile | NO |
---|---|---|
0.830357143 | [ 0.52678573] | [ 66] |
0.526785714 | [ 0.83035713] | [ 65] |
0.553571429 | [ 0.4375 ] | [ 68] |
0.4375 | [ 0.5535714 ] | [ 67] |
0.517857143 | [ 0.33035713] | [ 70] |
0.330357143 | [ 0.51785713] | [ 69] |
0.482142857 | [ 0.6785714 ] | [ 72] |
0.678571429 | [ 0.48214287] | [ 71] |
0.419642857 | [ 0.02678571] | [ 74] |
0.026785714 | [ 0.41964287] | [ 73] |
0.401785714 | [ 0.4017857 ] | [ 75] |
[1]源程序文件下载: https://github.com/Asurada2015/Python-Data-Analysis-Learning-Notes/blob/master/TensorFlow/demo_00/test_tf_train_batch.py
[2]test_tf_train_batch.csv: https://github.com/Asurada2015/Python-Data-Analysis-Learning-Notes/blob/master/TensorFlow/demo_00/test_tf_train_batch.csv