A bit about tensors

About shapes

Tensors have shapes. Some vocabulary:

Introduction to Tensors  |  TensorFlow Core

Untitled

Untitled

Untitled

Preparing Time Series Features and Labels

This is mainly achieved by a windowing technique where in you group consecutive measurement values into one feature and the next measurement will be the label. For example, in hourly measurements, you can use values taken at hours 1 to 11 to predict the value at hour 12.

import tensorflow as tf
# Generate a tf dataset with 10 elements (i.e. numbers 0 to 9)
dataset = tf.data.Dataset.range(10)

# Preview the result
for val in dataset:
   print(val.numpy())

Untitled

Windowing the data

you want to group consecutive elements of your data and use that to predict a future value. This is called windowing and you can use that with the window() method as shown below. Here, you will take 5 elements per window (i.e. size parameter) and you will move this window 1 element at a time (i.e. shift parameter). One caveat to using this method is that each window returned is a Dataset in itself. This is a Python iterable and, as of the current version (TF 2.8), it won't show the elements if you use the print() method on it. It will just show a description of the data structure (e.g. <_VariantDataset shapes: (), types: tf.int64>).

**# Generate a tf dataset with 10 elements (i.e. numbers 0 to 9)
dataset = tf.data.Dataset.range(10)

# Window the data
dataset = dataset.window(size=5, shift=1)

# Print the result
for window_dataset in dataset:
  print(window_dataset)**

Untitled

If you want to see the elements, you will have to iterate over each iterable. This can be done by modifying the print statement above with a nested for-loop or list comprehension. The code below shows the list comprehension while in the lecture video, you saw the for-loop.

Untitled

Now that you can see the elements of each window, you'll notice that the resulting sets are not sized evenly because there are no more elements after the number 9. You can use the drop_remainder flag to make sure that only 5-element windows are retained.

# Generate a tf dataset with 10 elements (i.e. numbers 0 to 9)
dataset = tf.data.Dataset.range(10)

# Window the data but only take those with the specified size
dataset = dataset.window(size=5, shift=1, drop_remainder=True)

# Print the result
for window_dataset in dataset:
  print([item.numpy() for item in window_dataset])

Untitled

Flattening The window