使用自己的数据创建batch
下面以一个例子说明:
data = np.array([x for x in range(1, 101)]).reshape(10, 10)
label = np.array([x for x in range(1, 11)]).reshape(10, 1)
将data代表的矩阵作为输入的数据,其中每一行作为一个样例,每一列作为一个特征,这里要说明的是:如果你的数据是2维的数据完全可以存为这种格式,就像是mnist集中的数据也是将每一张图片flatten后存为一维的行向量。
附上所有代码:
import numpy as np
import tensorflow as tf
...
if __name__ == '__main__':
data = np.array([x for x in range(1, 101)]).reshape(10, 10)
label = np.array([x for x in range(1, 11)]).reshape(10, 1)
batch_size = 3
capacity = 100 + 3 * batch_size
input_queue = tf.train.slice_input_producer([data, label])
label = input_queue[1]
image = input_queue[0]
image_batch, label_batch = tf.train.shuffle_batch([image, label],
batch_size=batch_size,
num_threads=32,
capacity=capacity,
min_after_dequeue=50)
# 这里根据自己的需要将数据转换为所想要的形状和格式
# label_batch = tf.reshape(label_batch, [batch_size])
# image_batch = tf.cast(image_batch, tf.float32)
with tf.Session() as sess:
tf.global_variables_initializer().run()
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
for i in range(20):
cur_example_batch, cur_label_batch = sess.run(
[image_batch, label_batch])
print(cur_label_batch)
coord.request_stop()
coord.join(threads)