【问题标题】:generating features and labels using iterator in tensorflow在 tensorflow 中使用迭代器生成特征和标签
【发布时间】:2019-07-24 09:51:38
【问题描述】:

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

x,y,lb = train_data 

我的train_data具有索引中的特征和标签,比如0 to 100。我希望x 有来自1 to 100feature 样本,y 应该有来自0 to 99labelslb 应该有索引100 的标签。

此外,我想使用迭代器在滑动批次中执行此操作。目前我有以下代码从0 to 100y0 to 100生成x。下一批从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)

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 该死的,这很容易。我从来没有想过我以后可以切片。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 2013-07-19
    • 2016-07-01
    • 2016-09-04
    相关资源
    最近更新 更多