代码:
# generating data
x = tf.constant(np.random.randint(256, size =(250,64, 64, 3)), dtype = tf.int32)
# Creating a dataset with sequence length
dataset = tf.data.Dataset.from_tensor_slices(x).batch(100, drop_remainder= True)
for i in dataset:
print(i.shape)
输出:
(100, 64, 64, 3)
(100, 64, 64, 3)
确保drop_remainders = True
最后,创建所需长度的批量大小。
# creating dataset with batch_size
dataset = dataset.batch(32)
for i in dataset:
print(i.shape)
输出:
(2, 100, 64, 64, 3)
如果您的数据大小为 (250,100,64, 64, 3):
dataset = tf.data.Dataset.from_tensor_slices(x).batch(32)
for i in dataset:
print(i.shape)
输出:
(32, 100, 64, 64, 3)
(32, 100, 64, 64, 3)
(32, 100, 64, 64, 3)
(32, 100, 64, 64, 3)
(32, 100, 64, 64, 3)
(32, 100, 64, 64, 3)
(32, 100, 64, 64, 3)
(26, 100, 64, 64, 3)