【问题标题】:Normalizing windows in tensorflow dataset规范化张量流数据集中的窗口
【发布时间】:2020-04-20 23:59:43
【问题描述】:

我正在尝试从单变量时间序列构建窗口数据集。 这个想法是,如果系列看起来像 [1, 2, 3, 4, 5, 6] 并且窗口长度为 2,那么 我会采用长度为 3 的窗口来考虑 2 个 X 特征和 Y 目标输出,所以 [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]] 然后我将它们洗牌以避免偏差,并将输入特征从每个窗口的目标输出中分离出来:[[[1, 2], [3]], [[2, 3], [4]], [[3, 4], [5]], [[4, 5], [6]]]

def windowed_dataset(series):
    # Initially the data is (N,) expand dims to (N, 1)
    series = tf.expand_dims(series, axis=-1)

    # Tensorflow Dataset from the array
    ds = tf.data.Dataset.from_tensor_slices(series)

    # Create the windows that will serve as input features and label (hence +1)
    ds = ds.window(window_len + 1, shift=1, drop_remainder=True)
    ds = ds.flat_map(lambda w: w.batch(window_len + 1))

    # randomize order 
    ds = ds.shuffle(shuffle_buffer)
    # Separate  the inputs and the target output(label)
    ds = ds.map(lambda w: (w[:-1], w[-1]))
    return ds.batch(batch_size).prefetch(1)

不过,我想添加一些规范化。例如,如果我的窗口是w=[1, 2, 3],那么我想根据[p/w[0] - 1 for p in w] 进行规范化

我认为我可以通过 ds.map 和

实现这一目标
    def normalize_window(w):
      return [((i/w[0]) -1) for i in w]


    ds = ds.map(normalize_window)

因为 map 应该将该函数应用于数据集中的每个窗口,但这不起作用。 tensorflow 数据集文档中的所有示例都使用 map 和 lambda 函数,但我认为它也适用于常规函数

有人知道应该怎么做吗?

编辑

我得到的回溯是

<ipython-input-39-929295e1b775> in <module>()
----> 1 dataset = model_forecast_datasets(btc_model, np_data[:6])

11 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
    263       except Exception as e:  # pylint:disable=broad-except
    264         if hasattr(e, 'ag_error_metadata'):
--> 265           raise e.ag_error_metadata.to_exception(e)
    266         else:
    267           raise

OperatorNotAllowedInGraphError: in user code:

    <ipython-input-38-b3d0f7e17689>:12 normalize_window  *
        return [(i/w[0] -1) for i in w]
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:561 __iter__
        self._disallow_iteration()
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:557 _disallow_iteration
        self._disallow_in_graph_mode("iterating over `tf.Tensor`")
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:537 _disallow_in_graph_mode
        " this function with @tf.function.".format(task))

    OperatorNotAllowedInGraphError: iterating over `tf.Tensor` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.

【问题讨论】:

  • 您是否收到错误或意外结果?请分享发生的更多细节。
  • @thushv89 使用列表理解添加了尝试的回溯
  • 好吧,问题很明显,是for i in w 部分。您正在尝试迭代该函数中的张量。而是尝试,return w/w[0] - 1
  • @thushv89 我有点困惑为什么w 是张量而不是WindowedDataset 但是,嗯
  • 哦,是的,我没有注意到ds.window 部分。看看上面的方法有没有用,如果不行,我再深入看看

标签: tensorflow


【解决方案1】:

你需要一个向量化计算的函数,比如

def normalize(data):
    mean = tf.math.reduce_mean(data)
    std = tf.math.reduce_std(data)
    data = tf.subtract(data, mean)
    data = tf.divide(data, std)
    return data

ds = ds.map(normalize)

编辑:对于您的特定规范化,这可能有效:

def normalize(data):
    data1 = tf.subtract(data, tf.constant(1))
    data1 = tf.divide(data1, data[0])
    return data1

(这必须在批处理ds = ds.flat_map(...)之后进行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-01
    • 2019-01-18
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 2018-10-12
    • 2018-08-04
    相关资源
    最近更新 更多