# ---
# 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
# ---

# + colab={"base_uri": "https://localhost:8080/", "height": 118} colab_type="code" id="aty8eqFppk0G" outputId="31db7c7b-3723-437e-e4f3-30afb9b91396"
from google.colab import drive
drive.mount('/content/drive')

# + colab={"base_uri": "https://localhost:8080/", "height": 50} colab_type="code" id="bp9fpo1Ikfwn" outputId="9e5281a9-9f36-47eb-e897-3bee5ff96774"
# If you are running on Google Colab, uncomment below to install the necessary dependencies 
# before beginning the exercise.

print("Setting up colab environment")
# !pip uninstall -y -q pyarrow
# !pip install -q https://s3-us-west-2.amazonaws.com/ray-wheels/latest/ray-0.8.0.dev5-cp36-cp36m-manylinux1_x86_64.whl
# !pip install -q ray[debug]

# A hack to force the runtime to restart, needed to include the above dependencies.
print("Done installing! Restarting via forced crash (this is not an issue).")
import os
os._exit(0)

# + colab={} colab_type="code" id="YHb-I_Vpkiu9"
# If you are running on Google Colab, please install TensorFlow 2.0 by uncommenting below..

try:
  # # %tensorflow_version only exists in Colab.
  # %tensorflow_version 2.x
except Exception:
  pass

# + colab={"base_uri": "https://localhost:8080/", "height": 895} colab_type="code" id="aoxLAjPXnbGM" outputId="2ecd4564-dafc-4ad8-e368-cd7915bd6939"
# !pip3 install ray xlrd pandas  tensorboardX

# + colab={"base_uri": "https://localhost:8080/", "height": 33} colab_type="code" id="rgMRfSCAeesr" outputId="8596c1a9-f5ed-42a3-ddde-a175e23dd373"
print("Setting up colab environment")
# !pip3 uninstall -y -q pyarrow
# !pip3 install -q ray[debug]


# + colab={} colab_type="code" id="KU1Eb-fkhb1l"
# # !pip3 install tensorflow keras scikit-learn pandas

# + colab={} colab_type="code" id="ApMfN5xMnJSU"
import numpy as np
np.random.seed(0)

import tensorflow as tf
try:
    tf.get_logger().setLevel('INFO')
except Exception as exc:
    print(exc)
import warnings
warnings.simplefilter("ignore")

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM,Dropout

from tensorflow.keras.optimizers import SGD, Adam
from tensorflow.keras.callbacks import ModelCheckpoint

from sklearn.model_selection import train_test_split

import ray
from ray import tune
from ray.tune.examples.utils import get_iris_data

import inspect
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('ggplot')
# %matplotlib inline

# + colab={"base_uri": "https://localhost:8080/", "height": 570} colab_type="code" id="JE3O0Kbthb1s" outputId="a1081327-4325-4d6f-880f-660bbd1999eb"
# !pip3 install ray[tune] ray[rllib] matplotlib

# + colab={} colab_type="code" id="4HRWGKtZpu3i"
import os
import pandas as pd
path = "/content/drive/My Drive/excels/"
files = os.listdir(path)

files_xls = [f for f in files if f[-3:] == 'xls']
df = pd.DataFrame()
for f in files_xls:
    fp=path+f
    data = pd.read_excel(fp, 'Sheet')
    data.columns = ['Time', 'output','outputPitch','outputVolm','Player1BPM','Player1Pitch','Player1Volm', 'Player2BPM','Player2Pitch','Player2Volm', 'Player3BPM','Player3Pitch','Player3Volm', 'Player4BPM','Player4Pitch','Player4Volm']
    df = df.append(data)   
df.columns = ['Time', 'output','outputPitch','outputVolm','Player1BPM','Player1Pitch','Player1Volm', 'Player2BPM','Player2Pitch','Player2Volm', 'Player3BPM','Player3Pitch','Player3Volm', 'Player4BPM','Player4Pitch','Player4Volm']
y =  df[['output']]
X = df[['Time','Player1BPM','Player1Pitch','Player1Volm', 'Player2BPM','Player2Pitch','Player2Volm', 'Player3BPM','Player3Pitch','Player3Volm', 'Player4BPM','Player4Pitch','Player4Volm']]

# + colab={} colab_type="code" id="qjYWgmARnJSY"
# df = pd.read_excel('/content/drive/My Drive/excels/A Classic Education - NightOwl.stem.xls', 'Sheet')
# df.columns = ['Time', 'output','outputPitch','outputVolm','Player1BPM','Player1Pitch','Player1Volm', 'Player2BPM','Player2Pitch','Player2Volm', 'Player3BPM','Player3Pitch','Player3Volm', 'Player4BPM','Player4Pitch','Player4Volm']
# y =  df[['output']]
# X = df[['Time','Player1BPM','Player1Pitch','Player1Volm', 'Player2BPM','Player2Pitch','Player2Volm', 'Player3BPM','Player3Pitch','Player3Volm', 'Player4BPM','Player4Pitch','Player4Volm']]

# + colab={"base_uri": "https://localhost:8080/", "height": 191} colab_type="code" id="vNj36EimnJSd" outputId="3f9f4677-09d6-435e-abda-5d18716ae1e1"
X.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 191} colab_type="code" id="ubR9W1htnJSh" outputId="305af672-fee9-4401-9164-c27f4c967351"
y.head()

# + colab={} colab_type="code" id="LaJHpUXWnJSv"
train_x, test_x, train_y, test_y = train_test_split(X, y, test_size=0.1,shuffle=False,random_state=42)

# + colab={} colab_type="code" id="b1J3d_1inJSk"
from sklearn.preprocessing import MinMaxScaler
scaler_X = MinMaxScaler()
X_train_scaled = scaler_X.fit_transform(train_x)
X_test_scaled = scaler_X.transform(test_x)

# + colab={"base_uri": "https://localhost:8080/", "height": 50} colab_type="code" id="wbdX_iVMWuQ4" outputId="6c0d5b92-fde2-4278-8593-68a86888a3ba"
train_x = np.array(X_train_scaled) #X_train_scaled
train_x = train_x[:, :, None]
print(train_x.shape)
test_x = np.array(X_test_scaled)#X_test_scaled
test_x = test_x[:, :, None]
print(test_x.shape)


# + colab={} colab_type="code" id="XXVvvnBNnJSp"
###############------------- ORIGINAL ONE WITH 3 LAYERS------------############ RUNNING SUCCESSFully
# def create_model(learning_rate, dense_1, dense_2 ,dense_3):
#     assert learning_rate > 0 and dense_1 > 0 and dense_2 > 0, "Did you set the right configuration?"
#     model = Sequential()
#     model.add(Dense(int(dense_1), input_shape=(13, ), activation='relu', name='fc1'))
#     model.add(Dense(int(dense_2), activation='relu', name='fc2'))
#     model.add(Dense(int(dense_2), activation='relu', name='fc3'))
#     model.add(Dense(1, activation='relu', name='output'))
#     optimizer = SGD(lr=learning_rate)
#     model.compile(optimizer, loss='mae', metrics=['mse'])
#     return model

# + colab={} colab_type="code" id="wIUdTSSjTuGo"
###############------------- LSTM ONE WITH 4 LAYERS------------############
def create_model(learning_rate, lstm_1, lstm_2 ,lstm_3, lstm_4):
    model = Sequential()
    model.add(LSTM(int(lstm_1), return_sequences=True, input_shape=(13, 1)))
    model.add(Dropout(0.3))
    model.add(LSTM(int(lstm_2), return_sequences=True))
    model.add(Dropout(0.3))
    model.add(LSTM(int(lstm_3), return_sequences=True))
    model.add(Dropout(0.3))
    model.add(LSTM(int(lstm_4)))
    model.add(Dropout(0.0))
    model.add(Dense(units=1))
    optimizer = SGD(lr=learning_rate)
    model.compile(optimizer, loss='mae', metrics=['mse'])
    return model


# + colab={} colab_type="code" id="gBL0LyZPnJS0"
# model = create_model(learning_rate=0.1, dense_1 = 2, dense_2 = 2,dense_3=2)

# + colab={} colab_type="code" id="JphdsLdoTcav"
model = create_model(learning_rate=0.1, lstm_1 = 50, lstm_2 = 50,lstm_3=50, lstm_4 = 50)

# + colab={"base_uri": "https://localhost:8080/", "height": 440} colab_type="code" id="5xYKcbyznJS3" outputId="16f436c7-bdcb-4b19-c2ea-5090a5ebe21e"
model.summary()


# + colab={} colab_type="code" id="RvJ_wAUdnJS6"
# test_model = model.fit(train_x, train_y, validation_data=(test_x, test_y), verbose=1, batch_size = 512, epochs = 20)

# + colab={} colab_type="code" id="j59HXjLvnJS8"
def train_on_iris():
    model = create_model(learning_rate=0.1, lstm_1 = 512, lstm_2 = 512, lstm_3 = 512, lstm_4 = 512)
    # This saves the top model. `accuracy` is only available in TF2.0.
    checkpoint_callback = ModelCheckpoint("model.h5", monitor='mse', save_best_only=True, save_freq=2)

    # Train the model
    model.fit(
        train_x, train_y, 
        validation_data=(test_x, test_y),
        verbose=1, batch_size=512, epochs=20, callbacks=[checkpoint_callback])
    return model


# + colab={} colab_type="code" id="s6nvr-VBnJS-"
#original_model = train_on_iris()

# + colab={"base_uri": "https://localhost:8080/", "height": 234} colab_type="code" id="N_NZOohwZ1LR" outputId="ee4ad520-c4d9-4556-a6cb-5b013d755f32"
# Open the file
with open('/content/drive/My Drive/ZOOOptSearchreport_orig.txt','w') as fh:
    # Pass the file handle in as a lambda function to make it callable
    original_model.summary(print_fn=lambda x: fh.write(x + '\n'))

print(original_model.summary())

# + colab={} colab_type="code" id="HDNFmEP1nJTB"
import tensorflow.keras as keras
from ray.tune import track

# + colab={} colab_type="code" id="2jqJzMlGBcsg"
    # config = {
    #     "num_samples": 10 ,
    #     "config": {
    #         "iterations": 100,
    #     },
    #     "stop": {
    #         "timesteps_total": 100
    #     },
    # }


# + colab={} colab_type="code" id="_g6k5UzqnJTD"
class TuneReporterCallback(keras.callbacks.Callback):
    def __init__(self, logs={}):
        self.iteration = 0
        super(TuneReporterCallback, self).__init__()

    def on_epoch_end(self, batch, logs={}):
        self.iteration += 1
        track.log(keras_info=logs, mean_accuracy=logs.get("mse"), mean_loss=logs.get("mae"))


# + colab={} colab_type="code" id="z9_z4Kl9V_-u"
def tune_iris(config):  # TODO: Change me.
    model = create_model(learning_rate=config["lr"], lstm_1=config["lstm_1"], lstm_2=config["lstm_2"],lstm_3=config["lstm_3"],lstm_4=config["lstm_4"])
    checkpoint_callback = ModelCheckpoint(
        "model.h5", monitor='loss', save_best_only=True, save_freq=2)

    # Enable Tune to make intermediate decisions by using a Tune Callback hook. This is Keras specific.
    callbacks = [checkpoint_callback, TuneReporterCallback()]
    
    # Train the model
    model.fit(
        train_x, train_y, 
        validation_data=(test_x, test_y),
        verbose=1, 
        batch_size=512, 
        epochs=10, 
        callbacks=callbacks)


# + colab={} colab_type="code" id="dpFv-Q8LnJTJ"
assert len(inspect.getargspec(tune_iris).args) == 1, "The `tune_iris` function needs to take in the arg `config`."

# + colab={"base_uri": "https://localhost:8080/", "height": 361} colab_type="code" id="ejdTIlRUnJTM" outputId="7c516ce6-990b-4a31-9560-c09a7512d169"
print("Test-running to make sure this function will run correctly.")
tune.track.init()  # For testing purposes only.
tune_iris({"lr": 0.1, "lstm_1": 64, "lstm_2": 32, "lstm_3": 8, "lstm_4":4})
print("Success!")

# + colab={} colab_type="code" id="a-_fsOVynJTO"
hyperparameter_space = {
    "lr": tune.loguniform(0.001, 0.1),  
    "lstm_1": tune.uniform(2, 128),
    "lstm_2": tune.uniform(2, 128),
    "lstm_3": tune.uniform(2, 128),
    "lstm_4": tune.uniform(2, 128),
}

# + colab={"base_uri": "https://localhost:8080/", "height": 212} colab_type="code" id="0L4ed3Em_UDd" outputId="63cf0ee3-d537-4<PASSWORD>"
pip install -U zoopt


# + colab={"base_uri": "https://localhost:8080/"} colab_type="code" id="kEE6ckuFnJTQ" outputId="c9ce3879-a577-485a-c595-83d982082c99"
# This seeds the hyperparameter sampling.
import numpy as np; np.random.seed(5)  
hyperparameter_space = hyperparameter_space  # TODO: Fill me out.
num_samples = 25

####################################################################################################
################ This is just a validation function for tutorial purposes only. ####################
HP_KEYS = ["lr", "lstm_1", "lstm_2", "lstm_3", "lstm_4"]
assert all(key in hyperparameter_space for key in HP_KEYS), (
    "The hyperparameter space is not fully designated. It must include all of {}".format(HP_KEYS))
######################################################################################################

ray.shutdown()  # Restart Ray defensively in case the ray connection is lost. 
ray.init(log_to_driver=False)
# We clean out the logs before running for a clean visualization later.
# ! rm -rf ~/ray_results/tune_iris

from ray.tune import run
from ray.tune.suggest.zoopt import ZOOptSearch
from zoopt import ValueType


dim_dict = {
    "lr": (ValueType.CONTINUOUS, [0.001,0.1], 1e-2),
    "lstm_1": (ValueType.DISCRETE, [2, 512], False),
    "lstm_2": (ValueType.DISCRETE, [2, 512], False),
    "lstm_3": (ValueType.DISCRETE, [2, 512], False),
    "lstm_4": (ValueType.DISCRETE, [2, 512], False),
}


zoopt_search = ZOOptSearch(
    algo="Asracos",  # only support Asracos currently
    budget=num_samples,
    dim_dict=dim_dict,
    metric="keras_info/mse",
    mode="min")

analysis= tune.run(tune_iris,search_alg=zoopt_search,resources_per_trial = {"cpu": 4, "gpu": 1},num_samples = num_samples)


assert len(analysis.trials) ==25, "Did you set the correct number of samples?"

# + colab={} colab_type="code" id="i3zWjJ2VnJTS"
# Obtain the directory where the best model is saved.
print("You can use any of the following columns to get the best model: \n{}.".format(
    [k for k in analysis.dataframe() if k.startswith("keras_info")]))
print("=" * 10)
logdir = analysis.get_best_logdir("keras_info/val_loss", mode="min")
# We saved the model as `model.h5` in the logdir of the trial.
from tensorflow.keras.models import load_model
tuned_model = load_model(logdir + "/model.h5")

# + colab={} colab_type="code" id="Y8ICEBOZa2iJ"
# Open the file
with open('/content/drive/My Drive/ZOOOptSearchreport_tune.txt','w') as fh:
    # Pass the file handle in as a lambda function to make it callable
    tuned_model.summary(print_fn=lambda x: fh.write(x + '\n'))

print(tuned_model.summary())

# + colab={} colab_type="code" id="kelKOf28nJTX"
predicted_value = original_model.predict(test_x)
predicted_value.shape

# + colab={} colab_type="code" id="uoa-qPmkqVDZ"
test_y.shape

# + colab={} colab_type="code" id="hnc7TVAnnJTZ"
predicted_value_tuned = tuned_model.predict(test_x)
predicted_value_tuned.shape

# + colab={} colab_type="code" id="Gnhq3u25nJTe"
# print("Loss is {:0.4f}".format(tuned_loss))
# print("Tuned Error is {:0.4f}".format(tuned_error))
# #print("The original un-tuned model had an accuracy of {:0.4f}".format(original_accuracy))
# predicted_value = tuned_model.predict(test_x)

# + colab={} colab_type="code" id="VvzAquVPnJTg"
predicted_value_tuned

# + colab={} colab_type="code" id="MHMmNkoknJTi"
import matplotlib.pyplot as plt
# plt.plot(test_y)
plt.plot(predicted_value)
plt.plot(predicted_value_tuned)

plt.show()

# + colab={} colab_type="code" id="LBqc-AbPpreY"
import matplotlib.pyplot as plt
plt.plot(predicted_value)

plt.show()

# + colab={} colab_type="code" id="HcE1M2P56One"
import matplotlib.pyplot as plt

plt.plot(predicted_value_tuned)
plt.show()

# + colab={} colab_type="code" id="uPT8IMhnnJTk"
import matplotlib.pyplot as plt
plt.plot(test_y)
plt.plot(predicted_value_tuned)
plt.plot(predicted_value)
plt.show()

# + colab={} colab_type="code" id="9Y41ADfRnJTp"
# %load_ext tensorboard
# %tensorboard --logdir ~/ray_results/tune_iris

# + colab={} colab_type="code" id="abW7Fq1ahb3H"


StarcoderdataJupyterScriptsDedupFiltered