# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + colab={"base_uri": "https://localhost:8080/"} id="7GcIghRRR5ty" outputId="47ff8360-491b-48f7-99b2-ee870fc29891"
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import csv

# !wget --no-check-certificate \
#     https://storage.googleapis.com/laurencemoroney-blog.appspot.com/Sunspots.csv \
#     -O /tmp/sunspots.csv

# + id="66QuiBPaSIdx"
time_step = []
sunspots=[]
with open('/tmp/sunspots.csv') as csvfile:
  reader = csv.reader(csvfile, delimiter=',')
  next(reader)
  for row in reader:
    sunspots.append(float(row[2]))
    time_step.append(int(row[0]))

series = np.array(sunspots)
time = np.array(time_step)

# + colab={"base_uri": "https://localhost:8080/", "height": 388} id="RJmSPyDUSyJ6" outputId="4e5bd5d2-014d-4147-b39b-2226a918f7ac"
 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)
plt.figure(figsize=(10, 6))
plot_series(time, series)

# + id="Qzaugx2PS7J6"
split_time = 3000
time_train = time[:split_time]
x_train = series[:split_time]
time_valid = time[split_time:]
x_valid = series[split_time:]

window_size = 30
batch_size = 32
shuffle_buffer_size = 1000


# + id="jDCxfoJMTRr0"
def windowed_dataset(series, window_size, batch_size, shuffle_buffer):
    series = tf.expand_dims(series, axis=-1)
    ds = tf.data.Dataset.from_tensor_slices(series)
    ds = ds.window(window_size + 1, shift=1, drop_remainder=True)
    ds = ds.flat_map(lambda w: w.batch(window_size + 1))
    ds = ds.shuffle(shuffle_buffer)
    ds = ds.map(lambda w: (w[:-1], w[1:]))
    return ds.batch(batch_size).prefetch(1)

def model_forecast(model, series, window_size):
    ds = tf.data.Dataset.from_tensor_slices(series)
    ds = ds.window(window_size, shift=1, drop_remainder=True)
    ds = ds.flat_map(lambda w: w.batch(window_size))
    ds = ds.batch(32).prefetch(1)
    forecast = model.predict(ds)
    return forecast


# + id="TUhYZAI-UJ96"
tf.keras.backend.clear_session()
tf.random.set_seed(51)
np.random.seed(51)

dataset = windowed_dataset(x_train, window_size, batch_size, shuffle_buffer_size)

model = tf.keras.models.Sequential([
  tf.keras.layers.Dense(10, input_shape=[None, 1], activation='relu'), 
  tf.keras.layers.Dense(10, activation='relu'), 
  tf.keras.layers.Dense(1)
])

optimizer = tf.keras.optimizers.SGD(lr=1e-7, momentum=0.9)
model.compile(loss='mse', optimizer=optimizer, metrics=["mae"])
history = model.fit(dataset,epochs=100, verbose=0)

# + colab={"base_uri": "https://localhost:8080/", "height": 388} id="1irLoHEWU8N3" outputId="0c8a2b33-901e-4320-854c-8f786cbbc576"
rnn_forecast = model_forecast(model, series[..., np.newaxis], window_size)
rnn_forecast = rnn_forecast[split_time - window_size:-1, -1, 0]
plt.figure(figsize=(10, 6))
plot_series(time_valid, x_valid)
plot_series(time_valid, rnn_forecast)

# + colab={"base_uri": "https://localhost:8080/", "height": 312} id="O1qq2oOJW5yq" outputId="65e3d2eb-8c95-452a-a43d-c32138ff943e"
import matplotlib.image  as mpimg
import matplotlib.pyplot as plt

#-----------------------------------------------------------
# Retrieve a list of list results on training and test data
# sets for each training epoch
#-----------------------------------------------------------
loss=history.history['loss']

epochs=range(len(loss)) # Get number of epochs


#------------------------------------------------
# Plot training and validation loss per epoch
#------------------------------------------------
plt.plot(epochs, loss, 'r')
plt.title('Training loss')
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.legend(["Loss"])

plt.figure()


# + colab={"base_uri": "https://localhost:8080/"} id="A2ZevrkqXTM4" outputId="7b9a3f85-0c9d-4e13-e7e5-4a9ef186ccc8"
tf.keras.metrics.mean_absolute_error(x_valid, rnn_forecast).numpy()

StarcoderdataJupyterScriptsDedupFiltered