How do I determine the dimensionality of a sliding window on a time series?

I’ve been trying to feed windows of data into a Conv1D network followed by an LSTM. I keep having data shape errors and want to understand what is being passed into the network.

When I use tf.rank on the windowed dataset I get this error:

ValueError: Attempt to convert a value (<PrefetchDataset element_spec=(TensorSpec(shape=(None, None, 7), dtype=tf.float64, name=None), TensorSpec(shape=(None, None, 7), dtype=tf.float64, name=None))>) with an unsupported type (<class 'tensorflow.python.data.ops.dataset_ops.PrefetchDataset'>) to a Tensor

This is returned when I apply tf.rank() to the dataset returned by the function below. “series” is a time-series of seven variables:

def windowed_dataset(series, batch_size, n_past=24, n_future=24, shift=1):
    ds = tf.data.Dataset.from_tensor_slices(series)
    ds = ds.window(size=n_past + n_future, shift=shift, drop_remainder=True)
    ds = ds.flat_map(lambda w: w.batch(n_past + n_future))
    ds = ds.map(lambda w: (w[:n_past], w[n_past:]))
    return ds.batch(batch_size).prefetch(1)

Working my way through the online TS forecasting tutorial

They implement a windowing function (below) where they comment that “Slicing does not preserve static shape information, so set the shapes manually. That way the ‘tf.data.Datasets’ are easier to inspect.”

Note: This calls a class WindowGenerator defined earlier in the tutorial that defines indexes and offsets for the desired window.

def split_window(self, features):
  inputs = features[:, self.input_slice, :]
  labels = features[:, self.labels_slice, :]
  if self.label_columns is not None:
    labels = tf.stack(
        [labels[:, :, self.column_indices[name]] for name in self.label_columns],
        axis=-1)

  # Slicing doesn't preserve static shape information, so set the shapes
  # manually. This way the `tf.data.Datasets` are easier to inspect.
  inputs.set_shape([None, self.input_width, None])
  labels.set_shape([None, self.label_width, None])

  return inputs, labels

Hi @brendonwp ,

The tf.rank function cannot be applied directly to the dataset object returned by the windowed_dataset function. tf.rank is used to return the rank of a tensor, which is the number of dimensions it has.A dataset object is not a tensor.

Want to understand what is being passed into the network?

You can use the element_spec attribute of the dataset object. You can use ds.element_spec to get the type and shape of the elements in your dataset.

You can go through this link for more details

I hope this will help you.

Thanks.

1 Like

Hi @brendonwp

Maybe you would like to count the dimensions directly, like a preview calculator,
you can try the following:

n = Number of data points
w = History window
f = Forecast steps
s = Stride/shift

dim_x = [n - ((w + f) - s), w]

1 Like