tensorflow数据集中窗口的规范化

tensorflow数据集中窗口的规范化,tensorflow,Tensorflow,我试图从一个单变量时间序列构建一个窗口数据集。 这个想法是,如果这个系列看起来像[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],[4,5],[6] def windowed_dataset(series): #

我试图从一个单变量时间序列构建一个窗口数据集。 这个想法是,如果这个系列看起来像
[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],[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对w中的p进行规范化

我想我可以通过
ds.map

    def normalize_window(w):
      return [((i/w[0]) -1) for i in w]


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

有人知道应该怎么做吗

编辑

我得到的回溯是

<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.
() ---->1数据集=模型预测数据集(btc模型,np数据[:6]) 11帧 /包装器中的usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py(*args,**kwargs) 263例外情况为e:#pylint:disable=broad except 264如果hasattr(即“ag\u错误\u元数据”): -->265将e.ag\u错误\u元数据引发到\u异常(e) 266其他: 267提高 OperatorNotAllowedInGraphError:在用户代码中: :12规范化\u窗口* 返回[(i/w[0]-1)表示i在w中] /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:561__ self.\u不允许\u迭代() /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:557不允许迭代 self.\u不允许在图模式下(“迭代`tf.Tensor`”) /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:537在图形模式下不允许 “此函数带有@tf.function.”.format(任务)) OperatorNotAllowedInGraphError:在图形执行中不允许迭代'tf.Tensor'。使用渴望执行或用@tf.function修饰此函数。
您需要一个将计算矢量化的函数,例如

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\u map(…)之后进行)

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