在tensorflow中使用迭代器生成特征和标签

时间:2019-07-24 09:51:38

标签: python python-3.x tensorflow tensorflow-datasets

我有一个包含要素和标签的数据集。我想从中产生3件事:

x,y,lb = train_data 

我的train_data具有功能,索引中的标签可以说0 to 100。我希望xfeature的{​​{1}}个样本,1 to 100应该有y的{​​{1}},而labels应该有标签位于索引0 to 99

此外,我想使用迭代器在批量滑动中执行此操作。目前,我有以下代码,它们根据lb生成100,并根据x生成0 to 100。下一批从y0 to 100开始,依此类推。

x : 1 to 101

1 个答案:

答案 0 :(得分:0)

您只能拥有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)