如何在 TensorFlow 中使用 "group_by_window" 函数
How do I use the "group_by_window" function in TensorFlow
在 TensorFlow 的新输入管道函数集中,可以使用 "group_by_window" 函数将记录集分组在一起。它在此处的文档中进行了描述:
https://www.tensorflow.org/api_docs/python/tf/contrib/data/Dataset#group_by_window
我没有完全理解这里用来描述功能的解释,我倾向于通过示例来学习。我无法在互联网上的任何地方找到此功能的任何示例代码。有人可以为此功能制作一个准系统和可运行的示例来展示它是如何工作的,以及为这个功能提供什么吗?
对于tensorflow版本1.9.0
这是我可以想出的一个简单示例:
import tensorflow as tf
import numpy as np
components = np.arange(100).astype(np.int64)
dataset = tf.data.Dataset.from_tensor_slices(components)
dataset = dataset.apply(tf.contrib.data.group_by_window(key_func=lambda x: x%2, reduce_func=lambda _, els: els.batch(10), window_size=100)
iterator = dataset.make_one_shot_iterator()
features = iterator.get_next()
sess = tf.Session()
sess.run(features) # array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18], dtype=int64)
第一个参数 key_func
将数据集中的每个元素映射到一个键。
window_size
定义了分配给 reduce_fund
的桶大小。
在 reduce_func
中,您会收到一块 window_size
元素。您可以随心所欲地随机播放、批处理或填充。
使用 group_by_window 函数编辑动态填充和分桶 more here :
如果你有一个 tf.contrib.dataset
包含 (sequence, sequence_length, label)
并且序列是 tf.int64 的张量:
def bucketing_fn(sequence_length, buckets):
"""Given a sequence_length returns a bucket id"""
t = tf.clip_by_value(buckets, 0, sequence_length)
return tf.argmax(t)
def reduc_fn(key, elements, window_size):
"""Receives `window_size` elements"""
return elements.shuffle(window_size, seed=0)
# Create buckets from 0 to 500 with an increment of 15 -> [0, 15, 30, ... , 500]
buckets = [tf.constant(num, dtype=tf.int64) for num in range(0, 500, 15)
window_size = 1000
# Bucketing
dataset = dataset.group_by_window(
lambda x, y, z: bucketing_fn(x, buckets),
lambda key, x: reduc_fn(key, x, window_size), window_size)
# You could pad it in the reduc_func, but I'll do it here for clarity
# The last element of the dataset is the dynamic sentences. By giving it tf.Dimension(None) it will pad the sencentences (with 0) according to the longest sentence.
dataset = dataset.padded_batch(batch_size, padded_shapes=(
tf.TensorShape([]), tf.TensorShape([]), tf.Dimension(None)))
dataset = dataset.repeat(num_epochs)
iterator = dataset.make_one_shot_iterator()
features = iterator.get_next()
在 TensorFlow 的新输入管道函数集中,可以使用 "group_by_window" 函数将记录集分组在一起。它在此处的文档中进行了描述:
https://www.tensorflow.org/api_docs/python/tf/contrib/data/Dataset#group_by_window
我没有完全理解这里用来描述功能的解释,我倾向于通过示例来学习。我无法在互联网上的任何地方找到此功能的任何示例代码。有人可以为此功能制作一个准系统和可运行的示例来展示它是如何工作的,以及为这个功能提供什么吗?
对于tensorflow版本1.9.0 这是我可以想出的一个简单示例:
import tensorflow as tf
import numpy as np
components = np.arange(100).astype(np.int64)
dataset = tf.data.Dataset.from_tensor_slices(components)
dataset = dataset.apply(tf.contrib.data.group_by_window(key_func=lambda x: x%2, reduce_func=lambda _, els: els.batch(10), window_size=100)
iterator = dataset.make_one_shot_iterator()
features = iterator.get_next()
sess = tf.Session()
sess.run(features) # array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18], dtype=int64)
第一个参数 key_func
将数据集中的每个元素映射到一个键。
window_size
定义了分配给 reduce_fund
的桶大小。
在 reduce_func
中,您会收到一块 window_size
元素。您可以随心所欲地随机播放、批处理或填充。
使用 group_by_window 函数编辑动态填充和分桶 more here :
如果你有一个 tf.contrib.dataset
包含 (sequence, sequence_length, label)
并且序列是 tf.int64 的张量:
def bucketing_fn(sequence_length, buckets):
"""Given a sequence_length returns a bucket id"""
t = tf.clip_by_value(buckets, 0, sequence_length)
return tf.argmax(t)
def reduc_fn(key, elements, window_size):
"""Receives `window_size` elements"""
return elements.shuffle(window_size, seed=0)
# Create buckets from 0 to 500 with an increment of 15 -> [0, 15, 30, ... , 500]
buckets = [tf.constant(num, dtype=tf.int64) for num in range(0, 500, 15)
window_size = 1000
# Bucketing
dataset = dataset.group_by_window(
lambda x, y, z: bucketing_fn(x, buckets),
lambda key, x: reduc_fn(key, x, window_size), window_size)
# You could pad it in the reduc_func, but I'll do it here for clarity
# The last element of the dataset is the dynamic sentences. By giving it tf.Dimension(None) it will pad the sencentences (with 0) according to the longest sentence.
dataset = dataset.padded_batch(batch_size, padded_shapes=(
tf.TensorShape([]), tf.TensorShape([]), tf.Dimension(None)))
dataset = dataset.repeat(num_epochs)
iterator = dataset.make_one_shot_iterator()
features = iterator.get_next()