gpt4 book ai didi

python - Tensorflow batch_join 的 allow_smaller_final_batch 不起作用?

转载 作者:行者123 更新时间:2023-11-28 18:17:06 26 4
gpt4 key购买 nike

我正在使用 tenosrlfow 队列来处理我的数据,我需要获取小于 batch size 的最终 batch,但我只能获取 5 个 batch size,无法获取最终 batch。我不明白这有什么问题。

data = np.arange(105)
data_placeholder = tf.placeholder(dtype=tf.int64, shape=[None,])

queue = tf.FIFOQueue(capacity=200,dtypes=tf.int64,shapes=())
enqueue_op = queue.enqueue_many([data_placeholder])

data_list = []
data_ = queue.dequeue()

data_list.append([data_])
batch_data = tf.train.batch_join(data_list,batch_size=20, capacity=100 ,allow_smaller_final_batch=True)

sess = tf.Session()

coord = tf.train.Coordinator()
tf.train.start_queue_runners(sess,coord)

step = 0
under = 0
uper = 0
enqueu_step = len(data)//20 + 1
while step < enqueu_step:
uper = uper + 20
sess.run(enqueue_op, feed_dict={data_placeholder:data[under:uper]})
print("enque step=%d/%d %d-%d" %(step, enqueu_step,under, uper))
step = step + 1
under = uper
i = 0
while i < enqueu_step:
_data = sess.run(batch_data)
print("setp=%d/%d shape=%s" % (i, enqueu_step,_data.shape))

i = i + 1
print("end")

最佳答案

我没有检查您的整个代码,但如果我没看错,您想要获取所有样本,即使最后一批比其余的小,对吧?

好吧,使用这个带有 8 个样本的玩具示例并使用 3 个批处理:

import tensorflow as tf
import numpy as np

num_samples = 8
batch_size = 3
capacity = num_samples % batch_size # set the capacity to the actual remaining samples
data = np.arange(1, num_samples+1)
data_input = tf.constant(data)

batch = tf.train.batch([data_input], enqueue_many=True, batch_size=batch_size, capacity=capacity, allow_smaller_final_batch=True)

with tf.Session() as sess:
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(2):
print(i, sess.run([batch]))
coord.request_stop()
coord.join(threads)
# this should follow a closed queue
print(i+1, sess.run([batch]))

结果:

0 [array([1, 2, 3])]

1 [array([4, 5, 6])]

2 [array([7, 8])]

这里重要的参数是enqueue_many,以便将每个数字视为一个单独的数字和capacity,它设置为实际剩余的样本(例如这里是2)。如果 capacity 设置为 1,您将只得到 1 个样本,如果它是 3,您将错过 allow_smaller_final_batch` 标志效果,因为它将返回 3 个样本(最后一个来自开始)。

希望这能阐明您应该使用 allow_smaller_final_batch 参数的方式。

关于python - Tensorflow batch_join 的 allow_smaller_final_batch 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47653918/

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