Tensorflow重叠小批量
给定数据集中的某些数据(或张量)Tensorflow重叠小批量,tensorflow,replace,sample,mini-batch,Tensorflow,Replace,Sample,Mini Batch,给定数据集中的某些数据(或张量) tensor=tf.常数([1,2,3,4,5,6,7]) 我需要创建N批次的M元组,通过绘制(比如4x3)替换。示例minibatch可以是 [[1 2 3], [3, 4 5], [2, 3, 4], [5, 6, 7]] 目的是避免以这种形式创建数据集 [[1, 2, 3] [2, 3, 4] [4, 5, 6] ] 因为大量的冗余。当我将新的小批量输入到培训过程中时,应该动态创建批次。我找到了一种方法,您认为这是最佳的吗?还是直接部署队列
tensor=tf.常数([1,2,3,4,5,6,7])
我需要创建N
批次的M
元组,通过绘制(比如4x3
)替换。示例minibatch可以是
[[1 2 3], [3, 4 5], [2, 3, 4], [5, 6, 7]]
目的是避免以这种形式创建数据集
[[1, 2, 3]
[2, 3, 4]
[4, 5, 6]
]
因为大量的冗余。当我将新的小批量输入到培训过程中时,应该动态创建批次。我找到了一种方法,您认为这是最佳的吗?还是直接部署队列更好
此代码基于上述链接
import tensorflow as tf
import numpy as np
def gen_batch():
# compute number of batches to emit
num_of_batches = round(((len(sequence) - batch_size) / stride))
# emit batches
for i in range(0, num_of_batches * stride, stride):
result = np.array(sequence[i:i + batch_size])
yield result
sequence = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
batch_size = 3
stride = 1
ds = tf.data.Dataset.from_generator(gen_batch, tf.float64)
ds = ds.shuffle(100)
ds_out = ds.make_one_shot_iterator().get_next()
sess = tf.Session()
print(sess.run(ds_out))
print(sess.run(ds_out))
print(sess.run(ds_out))
print(sess.run(ds_out))
print(sess.run(ds_out))
印刷品:
[3. 4. 5.]
[1. 2. 3.]
[2. 3. 4.]
[4. 5. 6.]
[5. 6. 7.]
您使用数据集API还是会话提要?