<jupyter_start><jupyter_text>Time Series Prediction - NN Single Layer (linear regresion)<jupyter_code>import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)
def plot_series(time, series, format='-', start=0, end=None):
  plt.plot(time[start:end], series[start:end], format)
  plt.xlabel('Time')
  plt.ylabel('Value')
  plt.grid(True)

def trend(time, slope=0):
  return slope * time

def seasonal_pattern(season_time):
  '''
  Just an arbitrary patten, you can change it if you wish
  '''
  return np.where(
      season_time < 0.4,
      np.cos(season_time * 2 * np.pi),
      1 / np.exp(3 * season_time)
  )

def seasonality(time, period, amplitude=1, phase=0):
  '''
  Repeats the same pattern at each period
  '''
  season_time = ((time + phase) % period) / period
  return amplitude * seasonal_pattern(season_time)

def noise(time, noise_level=1, seed=None):
  rnd = np.random.RandomState(seed)
  return rnd.randn(len(time)) * noise_level
baseline = 10
slope = 0.05
amplitude = 40
noise_level = 5
time = np.arange(4 * 365 + 1, dtype='float32')
trend_series = trend(time, slope)
seasonality_series = seasonality(time, period=365, amplitude=amplitude)
noise_series = noise(time, noise_level, seed=42)

series = baseline + trend_series + seasonality_series + noise_series
split_time = 1000
time_train = time[:split_time]
x_train = series[:split_time]
time_valid = time[split_time:]
x_valid = series[split_time:]
window_size = 20
batch_size = 32
shuffle_buffer_size = 1000
def windowed_dataset(series, window_size, batch_size, shuffle_buffer):
  dataset = tf.data.Dataset.from_tensor_slices(series)
  dataset = dataset.window(window_size + 1, shift=1, drop_remainder=True)
  dataset = dataset.flat_map(lambda window: window.batch(window_size + 1))
  dataset = dataset.shuffle(shuffle_buffer).map(
      lambda window: (window[:-1], window[-1]))
  dataset = dataset.batch(batch_size).prefetch(1)
  return dataset
dataset = windowed_dataset(x_train, window_size, batch_size, shuffle_buffer_size)
dataset
l0 = tf.keras.layers.Dense(1, input_shape=[window_size])
model = tf.keras.Sequential([l0])

model.compile(
    loss='mse',
    optimizer = tf.keras.optimizers.SGD(lr=1e-6, momentum=0.9)
)

model.fit(dataset, epochs=100, verbose=0)
print('layer weights:', l0.get_weights())
forecast = []
for time in range(len(series) - window_size):
  prediction = model.predict(series[time: time + window_size][np.newaxis])
  forecast.append(prediction)

forecast = forecast[split_time - window_size:]
results = np.array(forecast)[:, 0, 0]
plt.figure(figsize=(10, 6))
plot_series(time_valid, x_valid)
plot_series(time_valid, results)
tf.keras.metrics.mean_absolute_error(x_valid, results).numpy()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12989840348330914, 'fraction_numerical': 0.02358490566037736, 'mean_word_length': 5.595693779904306, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, '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': '162809', 'license': 'Apache-2.0', 'n_tokens_mistral': 1013, 'n_tokens_neox': 962, 'n_words': 210, 'path': 'deeplearning.ai/tf/c4_w2_ts_linear_regresion.ipynb', 'repo_name': 'martin-fabbri/colab-notebooks', 'size': '2756'}