<jupyter_start><jupyter_text>Time windows - 2 methods<jupyter_code>import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator
def window_dataset(series, time_steps, batch_size=32,
                   shuffle_buffer=1000):
    dataset = tf.data.Dataset.from_tensor_slices(series)
    dataset = dataset.window(time_steps + 1, shift=1, drop_remainder=True)
    dataset = dataset.flat_map(lambda window: window.batch(time_steps + 1))
    #dataset = dataset.shuffle(shuffle_buffer)
    dataset = dataset.map(lambda window: (window[:-1], window[-1]))
    dataset = dataset.batch(batch_size).prefetch(1)
    return dataset
x = np.arange(10)
print(x)
time_steps = 4
batch_size = 1
train_set = window_dataset(x, time_steps, batch_size) 
for x, y in train_set:
    print("x :\n", x.numpy())
    print("y :\n", y.numpy())
# define dataset
series = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
series = np.arange(10)

time_steps = 4  # steps here

generator = TimeseriesGenerator(series, series, length=time_steps, batch_size=1)

# number of samples
print('Samples: %d' % len(generator))

for i in range(len(generator)):
	x, y = generator[i]
	print('%s => %s' % (x, y))<jupyter_output>Samples: 6
[[0 1 2 3]] => [4]
[[1 2 3 4]] => [5]
[[2 3 4 5]] => [6]
[[3 4 5 6]] => [7]
[[4 5 6 7]] => [8]
[[5 6 7 8]] => [9]
{'fraction_non_alphanumeric': 0.16011860637509265, 'fraction_numerical': 0.04670126019273536, 'mean_word_length': 4.335968379446641, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '505263', 'license': 'MIT', 'n_tokens_mistral': 546, 'n_tokens_neox': 502, 'n_words': 137, 'path': '4. Time Series/AZ/Data Sampling/01_sample_window-01.ipynb', 'repo_name': 'AmirRazaMBA/TensorFlow-Certification', 'size': '1349'}