在张量流中使用迭代器生成特征和标签

generating features and labels using iterator in tensorflow

我有一个包含特征和标签的数据集。我想从中生成 3 个东西:

x,y,lb = train_data 

我的 train_data 具有来自索引的特征和标签让我们说 0 to 100。我希望 x 有来自 1 to 100feature 个样本,y 应该有来自 0 to 99labelslb 应该有标签在索引 100 处。

此外,我想使用迭代器在滑动批次中执行此操作。目前我有以下代码,它从 0 to 100 生成 x 并从 0 to 100 生成 y。而下一批从x : 1 to 101y:1 to 101开始,依此类推。

features_placeholder = tf.placeholder(tf.float32, shape=[None,None],name="input_features")
labels_placeholder = tf.placeholder(tf.float32, shape=[None,1],name = "input_labels")

iterator = (tf.data.Dataset.from_tensor_slices((features_placeholder, labels_placeholder))
           .apply(sliding.sliding_window_batch(timestep=100, stride=1))
           .batch(10)
           .make_initializable_iterator()
           )
next_element = iterator.get_next(name="batch")
init_op = iterator.initializer
saveable = tf.contrib.data.make_saveable_from_iterator(iterator)

您可以只有 windows 个 101 个元素,然后再相应地切片:

import tensorflow as tf
from tensorflow.contrib.data.python.ops import sliding

features_placeholder = tf.placeholder(tf.float32, shape=[1000, 10],name="input_features")
labels_placeholder = tf.placeholder(tf.float32, shape=[1000, 1],name = "input_labels")
iterator = (tf.data.Dataset.from_tensor_slices((features_placeholder, labels_placeholder))
           .apply(sliding.sliding_window_batch(window_size=101, window_shift=1))
           .batch(10)
           .make_initializable_iterator()
           )
x_it, y_it = iterator.get_next(name="batch")
x, y, lb = x_it[:, 1:], y_it[:, :-1], y_it[:, -1]
init_op = iterator.initializer
saveable = tf.contrib.data.make_saveable_from_iterator(iterator)