13

在 TensorFlow 的一组新输入管道函数中,可以使用“group_by_window”函数将记录集分组在一起。它在此处的文档中进行了描述:

https://www.tensorflow.org/api_docs/python/tf/contrib/data/Dataset#group_by_window

我不完全理解这里用于描述功能的解释,我倾向于通过示例来学习。我在互联网上的任何地方都找不到此功能的任何示例代码。有人可以提出这个函数的准系统和可运行的例子来展示它是如何工作的,以及赋予这个函数什么?

4

1 回答 1

13

对于 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 功能编辑动态填充和分桶:

如果你有一个tf.contrib.datasetwhich 成立(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()
于 2017-09-16T10:41:04.933 回答