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

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# %matplotlib inline
plt.rcParams['figure.figsize'] = [8, 6]
plt.rcParams['font.size'] = 16

# ## Unscaled data
#
# Load the hill valley dataset from https://archive.ics.uci.edu/ml/datasets/Hill-Valley

# +
df_train = pd.read_csv('data/Hill_Valley_with_noise_Training.data')
df_test = pd.read_csv('data/Hill_Valley_with_noise_Testing.data')
X_train = df_train.loc[:, [x for x in df_train.columns if x.startswith('X')]]
Y_train = df_train['class']
X_test = df_test.loc[:, [x for x in df_test.columns if x.startswith('X')]]
Y_test = df_test['class']

print(f'Train set size: {X_train.shape}')
print(f'Test set size: {X_test.shape}')
# -

X_train.iloc[range(10), :].T.plot(legend=False)


# ## Scaled data

def scale_rows(X):
    """Move median to zero and scale the peak to 1 or -1"""

    z = X.sub(X.median(axis=1), axis=0)
    scale = abs(z).max(axis=1)
    return z.div(scale, axis=0)


X_train = scale_rows(X_train)
X_test = scale_rows(X_test)

X_train.iloc[range(10), :].T.plot(legend=False)

# ## LSTM model

# +
model = tf.keras.Sequential()
model.add(layers.LSTM(10, input_shape=(100, 1)))
model.add(layers.Dense(2, activation='softmax'))

model.summary()

# +
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(), 
              optimizer='adam',
              metrics=['accuracy'])

history = model.fit(np.expand_dims(X_train, 2),
                    Y_train,
                    validation_data=(np.expand_dims(X_test, 2), Y_test),
                    batch_size=8,
                    epochs=10)
# -

plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'], label='test')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend()



StarcoderdataJupyterScriptsDedupFiltered