{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\n# import matplotlib.pyplot as plt\nimport numpy as np\nfrom datetime import datetime\n!pip install polars\nimport polars as pl\nimport random\n\nimport numpy as np\nimport tensorflow as tf  # or import torch for PyTorch\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import MinMaxScaler\n","metadata":{"ExecuteTime":{"end_time":"2023-10-13T02:42:38.335664300Z","start_time":"2023-10-13T02:42:35.514711400Z"},"execution":{"iopub.status.busy":"2023-11-04T01:56:08.531953Z","iopub.execute_input":"2023-11-04T01:56:08.532268Z","iopub.status.idle":"2023-11-04T01:56:22.558559Z","shell.execute_reply.started":"2023-11-04T01:56:08.532245Z","shell.execute_reply":"2023-11-04T01:56:22.557572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series = pl.scan_parquet(\"/kaggle/input/detect-sleep-states-memory-decrease/train_series.parquet\")","metadata":{"collapsed":false,"ExecuteTime":{"end_time":"2023-10-13T03:06:02.860166900Z","start_time":"2023-10-13T03:06:02.806646200Z"},"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-11-04T01:56:22.560322Z","iopub.execute_input":"2023-11-04T01:56:22.560865Z","iopub.status.idle":"2023-11-04T01:56:22.566802Z","shell.execute_reply.started":"2023-11-04T01:56:22.560841Z","shell.execute_reply":"2023-11-04T01:56:22.565685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series = train_series.with_columns(\n    pl.col(\"timestamp\").dt.hour().alias(\"hour\")\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:56:22.568091Z","iopub.execute_input":"2023-11-04T01:56:22.568398Z","iopub.status.idle":"2023-11-04T01:56:22.582734Z","shell.execute_reply.started":"2023-11-04T01:56:22.568369Z","shell.execute_reply":"2023-11-04T01:56:22.581849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_series.head(5).collect())","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:56:22.584513Z","iopub.execute_input":"2023-11-04T01:56:22.585442Z","iopub.status.idle":"2023-11-04T01:56:28.774287Z","shell.execute_reply.started":"2023-11-04T01:56:22.58541Z","shell.execute_reply":"2023-11-04T01:56:28.773572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def classify_day_quarter(val):\n    \"\"\"\n    \n    :param row: \n    :return: will append  dayquarter column which contains time divided portion for a daya eg. D1 0-6,, D2 6-12, D3 12-18  D4 18-24  \n    \"\"\"\n    hour = val\n    if hour >= 0 and hour <= 6:\n        return 1\n    elif hour > 6 and hour <= 12:\n        return 2\n    elif hour > 12 and hour <= 18:\n        return 3\n\n    elif hour > 18 and hour <= 24:\n        return 4\n    \n    else:\n        return 4","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:56:28.777655Z","iopub.execute_input":"2023-11-04T01:56:28.779104Z","iopub.status.idle":"2023-11-04T01:56:28.7904Z","shell.execute_reply.started":"2023-11-04T01:56:28.779025Z","shell.execute_reply":"2023-11-04T01:56:28.788169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series = train_series.with_columns(pl.col(\"hour\").map_elements(classify_day_quarter).alias(\"hour_window\"))\n","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:56:28.792882Z","iopub.execute_input":"2023-11-04T01:56:28.793558Z","iopub.status.idle":"2023-11-04T01:56:28.814448Z","shell.execute_reply.started":"2023-11-04T01:56:28.793532Z","shell.execute_reply":"2023-11-04T01:56:28.812695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series_baseline = train_series.select(pl.col('id_map'),pl.col(\"step\"),pl.col(\"anglez\"),pl.col(\"enmo\"))","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:56:28.816852Z","iopub.execute_input":"2023-11-04T01:56:28.818311Z","iopub.status.idle":"2023-11-04T01:56:28.826757Z","shell.execute_reply.started":"2023-11-04T01:56:28.818284Z","shell.execute_reply":"2023-11-04T01:56:28.824692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series_baseline.collect().write_parquet(\"train_series_baseline_parquets\")","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:56:28.830074Z","iopub.execute_input":"2023-11-04T01:56:28.831096Z","iopub.status.idle":"2023-11-04T01:56:50.470806Z","shell.execute_reply.started":"2023-11-04T01:56:28.830864Z","shell.execute_reply":"2023-11-04T01:56:50.46981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"series_map_id = pl.scan_parquet(\"/kaggle/input/detect-sleep-states-memory-decrease/train_id_map.parquet\")\n# series_map_id.collect().write_parquet(\"series_map_id\")","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:56:50.471857Z","iopub.execute_input":"2023-11-04T01:56:50.472166Z","iopub.status.idle":"2023-11-04T01:56:50.478674Z","shell.execute_reply.started":"2023-11-04T01:56:50.472137Z","shell.execute_reply":"2023-11-04T01:56:50.477683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events = pl.scan_parquet(\"/kaggle/input/detect-sleep-states-memory-decrease/train_events.parquet\")","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:56:50.483073Z","iopub.execute_input":"2023-11-04T01:56:50.483294Z","iopub.status.idle":"2023-11-04T01:56:50.488998Z","shell.execute_reply.started":"2023-11-04T01:56:50.483275Z","shell.execute_reply":"2023-11-04T01:56:50.488144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(series_map_id.collect())","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:56:50.489953Z","iopub.execute_input":"2023-11-04T01:56:50.490812Z","iopub.status.idle":"2023-11-04T01:56:50.502796Z","shell.execute_reply.started":"2023-11-04T01:56:50.490761Z","shell.execute_reply":"2023-11-04T01:56:50.501769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_events = train_events.select(pl.col(\"id_map\"),pl.col(\"step\"),pl.col(\"night\"),pl.col(\"event\"))","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:56:50.504103Z","iopub.execute_input":"2023-11-04T01:56:50.50437Z","iopub.status.idle":"2023-11-04T01:56:50.510047Z","shell.execute_reply.started":"2023-11-04T01:56:50.50435Z","shell.execute_reply":"2023-11-04T01:56:50.509123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"joined_frame =  train_series.join(train_events,on = [\"id_map\",\"step\"],how = \"left\")","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:56:50.511188Z","iopub.execute_input":"2023-11-04T01:56:50.511464Z","iopub.status.idle":"2023-11-04T01:56:50.520558Z","shell.execute_reply.started":"2023-11-04T01:56:50.511442Z","shell.execute_reply":"2023-11-04T01:56:50.519767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"joined_frame = joined_frame.with_columns(pl.col(\"timestamp\").dt.day().alias(\"day\"))","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:57:52.960475Z","iopub.execute_input":"2023-11-04T01:57:52.960827Z","iopub.status.idle":"2023-11-04T01:57:52.966282Z","shell.execute_reply.started":"2023-11-04T01:57:52.960805Z","shell.execute_reply":"2023-11-04T01:57:52.965286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(joined_frame.select(pl.col(\"event\").value_counts()).collect())","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:58:01.446855Z","iopub.execute_input":"2023-11-04T01:58:01.447218Z","iopub.status.idle":"2023-11-04T01:58:07.761422Z","shell.execute_reply.started":"2023-11-04T01:58:01.447194Z","shell.execute_reply":"2023-11-04T01:58:07.760244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"clearly too much unbalaced ","metadata":{}},{"cell_type":"code","source":"test_series = pl.scan_parquet(\"/kaggle/input/child-mind-institute-detect-sleep-states/test_series.parquet\")\n","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:58:07.763559Z","iopub.execute_input":"2023-11-04T01:58:07.763934Z","iopub.status.idle":"2023-11-04T01:58:07.770511Z","shell.execute_reply.started":"2023-11-04T01:58:07.763895Z","shell.execute_reply":"2023-11-04T01:58:07.769675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_series.head(5).collect())\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:58:07.77158Z","iopub.execute_input":"2023-11-04T01:58:07.771911Z","iopub.status.idle":"2023-11-04T01:58:07.786713Z","shell.execute_reply.started":"2023-11-04T01:58:07.771882Z","shell.execute_reply":"2023-11-04T01:58:07.785695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ctritical_events = joined_frame.filter(pl.col(\"event\") != None ).collect()","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:58:07.788793Z","iopub.execute_input":"2023-11-04T01:58:07.789179Z","iopub.status.idle":"2023-11-04T01:58:58.154419Z","shell.execute_reply.started":"2023-11-04T01:58:07.789157Z","shell.execute_reply":"2023-11-04T01:58:58.153334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_size = 5000\nnon_critical_events = joined_frame.filter(pl.col(\"event\") == None ).collect(streaming = True).sample(n=sample_size)","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:58:58.155594Z","iopub.execute_input":"2023-11-04T01:58:58.155967Z","iopub.status.idle":"2023-11-04T01:59:54.035954Z","shell.execute_reply.started":"2023-11-04T01:58:58.155937Z","shell.execute_reply":"2023-11-04T01:59:54.035021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# non_critical_events[\"event\"] = [3]* len(non_critical_events[\"event\"])\n\nnon_critical_events = non_critical_events.with_columns(pl.col(\"event\").map_batches(lambda x:  3))\n","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:59:54.036947Z","iopub.execute_input":"2023-11-04T01:59:54.037169Z","iopub.status.idle":"2023-11-04T01:59:54.042215Z","shell.execute_reply.started":"2023-11-04T01:59:54.037148Z","shell.execute_reply":"2023-11-04T01:59:54.041191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"non_critical_events = non_critical_events.sort(\"timestamp\")","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:59:54.043472Z","iopub.execute_input":"2023-11-04T01:59:54.044191Z","iopub.status.idle":"2023-11-04T01:59:54.058848Z","shell.execute_reply.started":"2023-11-04T01:59:54.044157Z","shell.execute_reply":"2023-11-04T01:59:54.058098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"non_critical_events = non_critical_events.with_columns(pl.col(\"event\").cast(pl.UInt8).alias(\"event\"))","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:59:54.059793Z","iopub.execute_input":"2023-11-04T01:59:54.060351Z","iopub.status.idle":"2023-11-04T01:59:54.070485Z","shell.execute_reply.started":"2023-11-04T01:59:54.060328Z","shell.execute_reply":"2023-11-04T01:59:54.069755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain_data = pl.concat(\n    [ctritical_events,\n    non_critical_events],\n    how = \"vertical\"\n)","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:59:54.071797Z","iopub.execute_input":"2023-11-04T01:59:54.072012Z","iopub.status.idle":"2023-11-04T01:59:54.081377Z","shell.execute_reply.started":"2023-11-04T01:59:54.071993Z","shell.execute_reply":"2023-11-04T01:59:54.080837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:59:54.084179Z","iopub.execute_input":"2023-11-04T01:59:54.085025Z","iopub.status.idle":"2023-11-04T01:59:54.097766Z","shell.execute_reply.started":"2023-11-04T01:59:54.085001Z","shell.execute_reply":"2023-11-04T01:59:54.097106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = train_data.sort(\"timestamp\")","metadata":{"execution":{"iopub.status.busy":"2023-11-04T02:01:42.287254Z","iopub.execute_input":"2023-11-04T02:01:42.287602Z","iopub.status.idle":"2023-11-04T02:01:42.292253Z","shell.execute_reply.started":"2023-11-04T02:01:42.28758Z","shell.execute_reply":"2023-11-04T02:01:42.291446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-11-04T02:01:43.761651Z","iopub.execute_input":"2023-11-04T02:01:43.762185Z","iopub.status.idle":"2023-11-04T02:01:43.76604Z","shell.execute_reply.started":"2023-11-04T02:01:43.762158Z","shell.execute_reply":"2023-11-04T02:01:43.765142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(style=\"whitegrid\")\n\n\nplt.figure(figsize=(14, 6))\nsns.lineplot(x='timestamp', y='enmo', data=train_data)\n\n\n\nplt.xlabel('Timestamp')\nplt.ylabel('enmo')\nplt.title('enmo vs timestamp')\n\n\nplt.xticks(rotation=45)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-04T01:59:54.244023Z","iopub.execute_input":"2023-11-04T01:59:54.244368Z","iopub.status.idle":"2023-11-04T02:00:05.332472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set(style=\"whitegrid\")\n\n\nplt.figure(figsize=(14, 6))\nsns.lineplot(x='timestamp', y='anglez', data=train_data)\n\n\nplt.xlabel('Timestamp')\nplt.ylabel('anglez')\nplt.title('anglez vs timestamp')\n\n\nplt.xticks(rotation=45)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-04T02:00:05.33378Z","iopub.execute_input":"2023-11-04T02:00:05.335163Z","iopub.status.idle":"2023-11-04T02:00:16.751771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(train_events)","metadata":{"execution":{"iopub.status.busy":"2023-11-04T02:00:16.752613Z","iopub.execute_input":"2023-11-04T02:00:16.752863Z","iopub.status.idle":"2023-11-04T02:00:16.760084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_pl = train_data.select(pl.col(\"anglez\"),pl.col(\"enmo\"),pl.col(\"hour\"),pl.col(\"hour_window\"))\ny_train_pl = train_data.select(pl.col(\"event\"))\n","metadata":{"execution":{"iopub.status.busy":"2023-11-04T02:06:01.274265Z","iopub.execute_input":"2023-11-04T02:06:01.274584Z","iopub.status.idle":"2023-11-04T02:06:01.279861Z","shell.execute_reply.started":"2023-11-04T02:06:01.274561Z","shell.execute_reply":"2023-11-04T02:06:01.278932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_npy = x_train_pl.to_numpy()\ny_npy = y_train_pl.to_numpy()\nx_train, x_test, y_train, y_test = train_test_split(x_npy, y_npy, test_size=0.2, random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-04T02:12:52.168679Z","iopub.execute_input":"2023-11-04T02:12:52.169023Z","iopub.status.idle":"2023-11-04T02:12:52.177064Z","shell.execute_reply.started":"2023-11-04T02:12:52.169001Z","shell.execute_reply":"2023-11-04T02:12:52.175777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape)\nprint(y_train.shape)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-04T02:13:06.454406Z","iopub.execute_input":"2023-11-04T02:13:06.454756Z","iopub.status.idle":"2023-11-04T02:13:06.46002Z","shell.execute_reply.started":"2023-11-04T02:13:06.454732Z","shell.execute_reply":"2023-11-04T02:13:06.458574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sequence_length = 5\nx_train_npy = x_train\ny_train_npy = y_train\nnum_sequences = x_train_npy.shape[0] // sequence_length\n\nx_train_npy = x_train_npy[:num_sequences * sequence_length]\ny_train_npy = y_train_npy[:num_sequences * sequence_length]\n\nx_train_npy = x_train_npy.reshape(-1, sequence_length, x_train_npy.shape[1])\ny_train_npy = y_train_npy.reshape(-1, sequence_length, y_train_npy.shape[1])\nx_train_npy, x_val, y_train_npy, y_val = train_test_split(x_train_npy, y_train_npy, test_size=0.2, random_state=42)\n\ndef train(layerss):\n    # scaler = MinMaxScaler()\n    # x_train_npy = scaler.fit_transform(x_train_npy)\n\n    # Assuming x_train_npy and y_train_npy are your time series data and labels\n    # Reshape x_train_npy to sequences of 500\n    \n    model = tf.keras.Sequential(layerss)\n\n\n    model.compile(optimizer='adam', loss='mean_squared_error')  # Use an appropriate loss function\n\n    model.summary()  # Check the model architecture\n    your_epochs=30\n    your_batch_size=64\n    history = model.fit(x_train_npy, y_train_npy, validation_data=(x_val, y_val), epochs=your_epochs, batch_size=your_batch_size)\n    # Evaluate the model on the validation set\n    loss = model.evaluate(x_val, y_val)\n    print(f'Validation loss: {loss}')\n\n    import matplotlib.pyplot as plt\n\n    # Plot the training loss\n    plt.plot(history.history['loss'], label='Training Loss')\n\n    # Plot the validation loss\n    plt.plot(history.history['val_loss'], label='Validation Loss')\n\n    plt.title('Model Learning Curve')\n    plt.xlabel('Epochs')\n    plt.ylabel('Loss')\n    plt.legend()\n    plt.show()\n    # Evaluate the model on the training set\n    train_pred = model.predict(x_train_npy)\n    train_pred = (train_pred).astype(int)  # Assuming binary classification\n    # printtrain_pred)\n    index=0\n    train_accuracy = (train_pred == y_train_npy[:,index,:])\n    train_accuracy = sum(train_accuracy)/len(train_accuracy)\n    print(f'Training accuracy: {train_accuracy}')\n\n    # Evaluate the model on the validation set\n    val_pred = model.predict(x_val)\n    val_pred = (val_pred ).astype(int)  # Assuming binary classification\n\n    val_accuracy = (val_pred == y_val[:,index,:])\n    val_accuracy = sum(val_accuracy)/len(val_accuracy)\n\n    print(f'Validation accuracy: {val_accuracy}')\n    return val_accuracy, train_accuracy\n","metadata":{"execution":{"iopub.status.busy":"2023-11-04T02:13:33.329683Z","iopub.execute_input":"2023-11-04T02:13:33.33004Z","iopub.status.idle":"2023-11-04T02:13:33.341699Z","shell.execute_reply.started":"2023-11-04T02:13:33.330014Z","shell.execute_reply":"2023-11-04T02:13:33.34076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def simplernn(rnnunits, nnunits):\n    return [\n        tf.keras.layers.Input(shape=(sequence_length, x_train_npy.shape[2])),  # Input layer\n        tf.keras.layers.SimpleRNN(units=rnnunits, return_sequences=True, activation='relu'),\n        tf.keras.layers.SimpleRNN(units=rnnunits, return_sequences=True, activation='relu'),\n        tf.keras.layers.SimpleRNN(units=rnnunits, activation='relu'),\n        tf.keras.layers.Dense(nnunits, activation='relu'),\n        tf.keras.layers.Dense(1)  # Output layer, Modify this layer as needed for your specific task\n    ]\ndef lstm(rnnunits, nnunits):\n    return [\n        tf.keras.layers.Input(shape=(sequence_length, x_train_npy.shape[2])),  # Input layer\n        tf.keras.layers.LSTM(units=rnnunits, return_sequences=True, activation='relu'),\n        tf.keras.layers.LSTM(units=rnnunits, return_sequences=True, activation='relu'),\n        tf.keras.layers.LSTM(units=rnnunits, activation='relu'),\n        tf.keras.layers.Dense(nnunits, activation='relu'),\n        tf.keras.layers.Dense(1)  # Output layer, Modify this layer as needed for your specific task\n    ]\ndef gru(rnn_units, nn_units):\n    return [\n        tf.keras.layers.Input(shape=(sequence_length, x_train_npy.shape[2])),\n        tf.keras.layers.GRU(units=rnn_units, return_sequences=True, activation='relu'),\n        tf.keras.layers.GRU(units=rnn_units, return_sequences=True, activation='relu'),\n        tf.keras.layers.GRU(units=rnn_units, activation='relu'),\n        tf.keras.layers.Dense(nn_units, activation='relu'),\n        tf.keras.layers.Dense(1)\n    ]\n\ndef bidirectional_rnn(rnn_units, nn_units):\n    return [\n        tf.keras.layers.Input(shape=(sequence_length, x_train_npy.shape[2])),\n        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(units=rnn_units, return_sequences=True, activation='relu')),\n        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(units=rnn_units, return_sequences=True, activation='relu')),\n        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(units=rnn_units, activation='relu')),\n        tf.keras.layers.Dense(nn_units, activation='relu'),\n        tf.keras.layers.Dense(1)\n    ]\n\n# def conv_lstm_2d(rnn_units, nn_units):\n#     frame_height=64\n#     frame_width=64\n#     channels=1,\n#     convlstm_filters=32\n#     return [\n#         # The input shape now includes the spatial dimensions (height, width) and channels, along with the sequence length.\n#         tf.keras.layers.Input(shape=(sequence_length, frame_height, frame_width, channels)),\n        \n#         # ConvLSTM2D layer with return_sequences=True to return the full sequence to the next layer\n#         tf.keras.layers.ConvLSTM2D(filters=convlstm_filters, kernel_size=(3, 3), padding='same', return_sequences=True, activation='relu'),\n        \n#         # Typically, you would add some form of down sampling (like MaxPooling) but ConvLSTM2D does not have this built in,\n#         # you need to add separate layers for downsampling if needed, or adjust the strides parameter in ConvLSTM2D.\n        \n#         # Additional ConvLSTM2D layers, adjusting the 'return_sequences' as needed based on whether you want the output to be sequential or not.\n#         tf.keras.layers.ConvLSTM2D(filters=convlstm_filters, kernel_size=(3, 3), padding='same', return_sequences=True, activation='relu'),\n#         tf.keras.layers.ConvLSTM2D(filters=convlstm_filters, kernel_size=(3, 3), padding='same', return_sequences=False, activation='relu'),\n        \n#         # Flatten the output to feed into a Dense layer\n#         tf.keras.layers.Flatten(),\n        \n#         # Fully connected layers after the ConvLSTM2D layers\n#         tf.keras.layers.Dense(nn_units, activation='relu'),\n#         tf.keras.layers.Dense(1)\n#     ]\n\n# def simple_transformer(attention_units, nn_units):\n#     return [\n#         tf.keras.layers.Input(shape=(sequence_length, x_train_npy.shape[2])),\n#         tf.keras.layers.MultiHeadAttention(num_heads=2, key_dim=attention_units),\n#         tf.keras.layers.GlobalAveragePooling1D(),\n#         tf.keras.layers.Dense(nn_units, activation='relu'),\n#         tf.keras.layers.Dense(1)\n#     ]\n\nresults = []\n# conv_lstm_2d,\nfuncs = [simplernn, lstm, gru,bidirectional_rnn]\nrnnunits= [16,32,64,128,256,512]\nhiddenunits= [16,32,64,128,256,512]\n\nfor r in rnnunits:\n    for h in hiddenunits:\n        for f in funcs:\n            m = f(r,h)\n            valacc, trainacc = train(m)\n            # Append the results to the list as a dictionary\n            results.append({\n                'RNN Units': r,\n                'Hidden Units': h,\n                'Function': f.__name__,  # get the function's name\n                'Validation Accuracy': valacc,\n                'Training Accuracy': trainacc\n            })\n# Convert the results list to a pandas DataFrame\ndf = pd.DataFrame(results)\ndf\n          ","metadata":{"execution":{"iopub.status.busy":"2023-11-04T02:13:37.457599Z","iopub.execute_input":"2023-11-04T02:13:37.458087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model, \"model1.png\", show_shapes=True)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_npy = x_train.to_numpy()\ny_train_npy = y_train.to_numpy()\n\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import MinMaxScaler\n\n# Assuming x_train_npy and y_train_npy are your time series data and labels\n\n# Reshape x_train_npy to sequences of 500\nsequence_length = 1\nnum_sequences = x_train_npy.shape[0] // sequence_length\n\nx_train_npy = x_train_npy[:num_sequences * sequence_length]\ny_train_npy = y_train_npy[:num_sequences * sequence_length]\n\nx_train_npy = x_train_npy.reshape(-1, sequence_length, x_train_npy.shape[1])\ny_train_npy = y_train_npy.reshape(-1, sequence_length, y_train_npy.shape[1])\n\nx_train_npy, x_val, y_train_npy, y_val = train_test_split(x_train_npy, y_train_npy, test_size=0.2, random_state=42)\n\nclass SimpleRNNModel(nn.Module):\n    def __init__(self, input_size, hidden_size, num_layers, output_size):\n        super(SimpleRNNModel, self).__init__()\n        self.rnn = nn.RNN(input_size, hidden_size, num_layers, batch_first=True)\n        self.fc1 = nn.Linear(hidden_size, 32)\n        self.fc2 = nn.Linear(32, output_size)\n\n    def forward(self, x):\n        out, _ = self.rnn(x)\n        out = out[:, -1, :]  # Take the output of the last time step\n        out = self.fc1(out)\n        out = self.fc2(out)\n        return out\n\ninput_size = x_train_npy.shape[2]\nhidden_size = 64\nnum_layers = 2\noutput_size = 1\n\nmodel = SimpleRNNModel(input_size, hidden_size, num_layers, output_size)\ncriterion = nn.MSELoss()\noptimizer = optim.Adam(model.parameters())\n\nyour_epochs = 5\nyour_batch_size = 1000\n\nx_train_npy = torch.Tensor(x_train_npy)\ny_train_npy = torch.Tensor(y_train_npy)\nx_val = torch.Tensor(x_val)\ny_val = torch.Tensor(y_val)\n\nfor epoch in range(your_epochs):\n    print(len(x_train_npy))\n    for i in range(0, len(x_train_npy), your_batch_size):\n        x_batch = x_train_npy[i:i + your_batch_size]\n        y_batch = y_train_npy[i:i + your_batch_size]\n        optimizer.zero_grad()\n        outputs = model(x_batch)\n        loss = criterion(outputs, y_batch)\n        loss.backward()\n        optimizer.step()\n    print(\"-\")\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model on the validation set\nval_outputs = model(x_val)\nprint(val_outputs.shape,y_val.shape)\nval_loss = criterion(val_outputs, y_val)\nprint(f'Validation loss: {val_loss.item()}')\n\n# Plot the training loss\nplt.plot(history, label='Training Loss')\n\n# Plot the validation loss\nplt.plot(val_loss, label='Validation Loss')\n\nplt.title('Model Learning Curve')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let see results after normalizing data","metadata":{}},{"cell_type":"code","source":"model2_inp = tf.keras.Input(shape = (6),name = \"input\")\nx = tf.keras.layers.Dense(8,activation = \"tanh\")(model2_inp)\nx = tf.keras.layers.Dense(16,activation = \"tanh\")(x)\nx = tf.keras.layers.Dense(4,activation = \"tanh\")(x)\nmodel2_output = tf.keras.layers.Dense(3,activation = \"sigmoid\")(x)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2 = tf.keras.Model(model2_inp,model2_output,name = \"model2_base\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model2, \"model2.png\", show_shapes=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model2.compile(loss=tf.keras.losses.CategoricalCrossentropy(),\n                optimizer=tf.keras.optimizers.Adam(),\n                metrics=['accuracy'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_model2 = train_data.select(pl.col(\"anglez\"),pl.col(\"enmo\"),pl.col(\"hour_window\"))\nhour_wind = x_train_model2[\"hour_window\"]\nhour_wind_onehot =  tf.one_hot(hour_wind, depth=4)\nhour_wind_onehot = np.array(hour_wind_onehot).reshape(-1,4)\nhour_wind_onehot\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_model2 = pl.concat(\n[\n    x_train,\n    pl.DataFrame(hour_wind_onehot)\n],\nhow = \"horizontal\")\nx_train_model2 = x_train_model2.drop([\"hour_window\",\"hour\"])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history2 = model2.fit(x_train_model2.to_numpy(), np.array(y_train_onehot).reshape(-1,3), batch_size=16, epochs=3, validation_split=0.3)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}