{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import time\n", "import numpy as np\n", "from sklearn.metrics import mean_squared_error, mean_absolute_error, mean_absolute_percentage_error, r2_score\n", "from statsmodels.tools.eval_measures import mse as sm_mse, rmse as sm_rmse, meanabs as sm_mae\n", "from sklearn.model_selection import TimeSeriesSplit, GridSearchCV\n", "from neuralforecast import NeuralForecast\n", "from neuralforecast.models import NBEATS, NHITS, TFT, PatchTST, TCN, DLinear, RNN, LSTM, Autoformer, BiTCN, DeepAR, DeepNPTS, DilatedRNN, FEDformer, GRU, HINT, Informer, iTransformer, KAN, MLP, MLPMultivariate, NBEATSx, NLinear, RMoK, SOFTS, StemGNN, TiDE, TimeMixer, TimeLLM, TimesNet, TSMixer, TSMixerx, VanillaTransformer\n", "from neuralforecast.losses.pytorch import MQLoss\n", "from neuralforecast.losses.pytorch import MAE\n", "from neuralforecast.utils import AirPassengersDF\n", "import time\n", "import pandas as pd\n", "import numpy as np\n", "import torch\n", "torch.set_float32_matmul_precision('high') # or 'medium' for more precision\n", "\n", "import plotly.graph_objects as go\n", "from sklearn.utils import check_random_state\n", "import logging\n", "logging.basicConfig(level=logging.INFO)\n", "# Set seed for reproducibility\n", "# Set up logging\n", "logging.basicConfig(level=logging.INFO)\n", "\n", "# Set seed for reproducibility\n", "seed = 42\n", "np.random.seed(seed)\n", "torch.manual_seed(seed)\n", "random_state = check_random_state(seed)\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
yearnaicsemppayprodeprodhprodwvshipmatcostvadd...equipplantpishippimatpiinvpiendtfp5tfp5dtfp4tfp4
01958336111146.1868.4116.2229.7650.65007.93411.31563.8...3291.111718.80.3140.2380.1800.147NaN0.553NaN0.550
11959336111160.61072.6131.0283.2827.26422.24306.72143.4...3457.511415.60.3220.2430.1840.1450.0520.5830.0530.580
21960336111176.11183.7144.7302.8925.77239.04883.52336.2...3673.011375.10.3180.2410.1900.1500.0300.6010.0300.598
31961336111152.41035.7123.2252.9789.56214.24134.92053.6...3794.211179.90.3170.2420.1900.146-0.0070.596-0.0090.592
41962336111168.21223.6138.4297.1954.77855.35187.92671.2...3908.111141.20.3150.2460.1930.1470.0770.6440.0780.641
\n", "

5 rows × 24 columns

\n", "
" ], "text/plain": [ " year naics emp pay prode prodh prodw vship matcost vadd \\\n", "0 1958 336111 146.1 868.4 116.2 229.7 650.6 5007.9 3411.3 1563.8 \n", "1 1959 336111 160.6 1072.6 131.0 283.2 827.2 6422.2 4306.7 2143.4 \n", "2 1960 336111 176.1 1183.7 144.7 302.8 925.7 7239.0 4883.5 2336.2 \n", "3 1961 336111 152.4 1035.7 123.2 252.9 789.5 6214.2 4134.9 2053.6 \n", "4 1962 336111 168.2 1223.6 138.4 297.1 954.7 7855.3 5187.9 2671.2 \n", "\n", " ... equip plant piship pimat piinv pien dtfp5 tfp5 dtfp4 \\\n", "0 ... 3291.1 11718.8 0.314 0.238 0.180 0.147 NaN 0.553 NaN \n", "1 ... 3457.5 11415.6 0.322 0.243 0.184 0.145 0.052 0.583 0.053 \n", "2 ... 3673.0 11375.1 0.318 0.241 0.190 0.150 0.030 0.601 0.030 \n", "3 ... 3794.2 11179.9 0.317 0.242 0.190 0.146 -0.007 0.596 -0.009 \n", "4 ... 3908.1 11141.2 0.315 0.246 0.193 0.147 0.077 0.644 0.078 \n", "\n", " tfp4 \n", "0 0.550 \n", "1 0.580 \n", "2 0.598 \n", "3 0.592 \n", "4 0.641 \n", "\n", "[5 rows x 24 columns]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "req_naics = 336111\n", "df = pd.read_csv(f\"../data/processed_data_{req_naics}.csv\")\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
naicsemppayprodeprodhprodwymatcostvaddinvest...pishippimatpiinvpiendtfp5tfp5dtfp4tfp4dsunique_id
1336111160.61072.6131.0283.2827.26422.24306.72143.493.0...0.3220.2430.1840.1450.0520.5830.0530.5801959-01-01336111
2336111176.11183.7144.7302.8925.77239.04883.52336.2111.2...0.3180.2410.1900.1500.0300.6010.0300.5981960-01-01336111
3336111152.41035.7123.2252.9789.56214.24134.92053.691.8...0.3170.2420.1900.146-0.0070.596-0.0090.5921961-01-01336111
4336111168.21223.6138.4297.1954.77855.35187.92671.2118.3...0.3150.2460.1930.1470.0770.6440.0780.6411962-01-01336111
5336111160.61231.7132.2293.9972.08254.45518.52748.9144.9...0.3130.2450.1950.1470.0140.6530.0140.6491963-01-01336111
\n", "

5 rows × 25 columns

\n", "
" ], "text/plain": [ " naics emp pay prode prodh prodw y matcost vadd \\\n", "1 336111 160.6 1072.6 131.0 283.2 827.2 6422.2 4306.7 2143.4 \n", "2 336111 176.1 1183.7 144.7 302.8 925.7 7239.0 4883.5 2336.2 \n", "3 336111 152.4 1035.7 123.2 252.9 789.5 6214.2 4134.9 2053.6 \n", "4 336111 168.2 1223.6 138.4 297.1 954.7 7855.3 5187.9 2671.2 \n", "5 336111 160.6 1231.7 132.2 293.9 972.0 8254.4 5518.5 2748.9 \n", "\n", " invest ... piship pimat piinv pien dtfp5 tfp5 dtfp4 tfp4 \\\n", "1 93.0 ... 0.322 0.243 0.184 0.145 0.052 0.583 0.053 0.580 \n", "2 111.2 ... 0.318 0.241 0.190 0.150 0.030 0.601 0.030 0.598 \n", "3 91.8 ... 0.317 0.242 0.190 0.146 -0.007 0.596 -0.009 0.592 \n", "4 118.3 ... 0.315 0.246 0.193 0.147 0.077 0.644 0.078 0.641 \n", "5 144.9 ... 0.313 0.245 0.195 0.147 0.014 0.653 0.014 0.649 \n", "\n", " ds unique_id \n", "1 1959-01-01 336111 \n", "2 1960-01-01 336111 \n", "3 1961-01-01 336111 \n", "4 1962-01-01 336111 \n", "5 1963-01-01 336111 \n", "\n", "[5 rows x 25 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# %% Convert 'Year' column to datetime and prepare multivariate dataset\n", "df['ds'] = pd.to_datetime(df['year'], format='%Y')\n", "df['unique_id'] = \"336111\"\n", "df = df.rename(columns={\"vship\": \"y\"}) # Target variable\n", "df.drop(columns=['year'], inplace=True)\n", "feature_columns = [i for i in df.columns if i not in ['ds', 'y', 'unique_id']]\n", "df = df.dropna()\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data split: Total: 58, Train: 47, Test: 11\n", "Training set date range: 1959-01-01 00:00:00 to 2005-01-01 00:00:00\n", "Testing set date range: 2006-01-01 00:00:00 to 2016-01-01 00:00:00\n" ] } ], "source": [ "# %% Train-test split\n", "test_size = int(len(df) * 0.2)\n", "train = df[:len(df) - test_size]\n", "val = df[len(df) - test_size:]\n", "\n", "print(f\"Data split: Total: {len(df)}, Train: {len(train)}, Test: {len(val)}\")\n", "print(\"Training set date range:\", train['ds'].min(), \"to\", train['ds'].max())\n", "print(\"Testing set date range:\", val['ds'].min(), \"to\", val['ds'].max())" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# %% Evaluation Function\n", "\n", "def smape_loss(y_true, y_pred):\n", " y_true = y_true.values\n", " y_pred = y_pred.values\n", " return 100 * np.mean(2 * np.abs(y_pred - y_true) / (np.abs(y_true) + np.abs(y_pred)))\n", "\n", "def evaluate_model(y_true, y_pred, y_train, model_name, start_time):\n", " mse = mean_squared_error(y_true, y_pred)\n", " mae = mean_absolute_error(y_true, y_pred)\n", " mape = mean_absolute_percentage_error(y_true, y_pred)\n", " smape = smape_loss(y_true, y_pred)\n", " r2 = r2_score(y_true, y_pred)\n", " \n", " naive_forecast_errors = np.abs(y_train.diff()).dropna()\n", " mae_naive = sm_mae(naive_forecast_errors, np.zeros_like(naive_forecast_errors))\n", " mase = sm_mae(y_true, y_pred) / mae_naive if mae_naive != 0 else np.nan \n", " runtime = time.time() - start_time\n", "\n", " return {\n", " \"Model\": model_name,\n", " \"MSE\": mse,\n", " \"MAE\": mae,\n", " \"MAPE\": mape,\n", " \"SMAPE\": smape,\n", " \"MASE\": mase,\n", " \"R2 Score\": r2,\n", " \"Time (s)\": runtime\n", " }\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "input_size =len(feature_columns) + 1" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Global seed set to 1\n", "Global seed set to 1\n", "Global seed set to 1\n" ] } ], "source": [ "# %% Define Multivariate Models\n", "models = [\n", " NBEATSx(h=12, input_size=24, loss=MAE(), alias='NBEATSx_12'),\n", " TiDE(h=12, input_size=24, loss=MAE(), alias='TiDE_12'),\n", " TSMixer(h=12, input_size=24, n_series=len(feature_columns) + 1, loss=MAE(), alias='TSMixer_12'),\n", " #Informer(h=12, input_size=24, loss=MAE(), alias='Informer_12'),\n", " #PatchTST(h=12, input_size=24, loss=MAE(), alias='PatchTST_12'),\n", "]\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "GPU available: False, used: False\n", "TPU available: False, using: 0 TPU cores\n", "IPU available: False, using: 0 IPUs\n", "HPU available: False, using: 0 HPUs\n", "\n", " | Name | Type | Params\n", "-----------------------------------------------\n", "0 | loss | MAE | 0 \n", "1 | padder_train | ConstantPad1d | 0 \n", "2 | scaler | TemporalNorm | 0 \n", "3 | blocks | ModuleList | 2.4 M \n", "-----------------------------------------------\n", "2.4 M Trainable params\n", "900 Non-trainable params\n", "2.4 M Total params\n", "9.789 Total estimated model params size (MB)\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "cfbb78240a714aa8b40d9df7e90a3dc4", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Sanity Checking: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6c459c16f78a472bad5f2938f2f3effc", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Training: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "edf9f8fa7ef045638ca690ea3711a72a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "46696bd62ebf4a2c91af7381c185913f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8264f2680fd94dc8943616acbd44ecbf", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "58dae8ce1ae848c3a2a6dd7c8414e60c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "acebc55a8c224a5f9457e2efec85cfaf", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "88ec6dd30ce940178764ffa721ca961d", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "32505da25b254aacaafbe3fa0f809e18", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "066fa67d0a664cd591838c2323d1b497", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a64a5b11e2c74b9a95323e1768edf9a6", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "2d537157bbba4f64a2d17aba8d290f86", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "`Trainer.fit` stopped: `max_steps=1000` reached.\n", "GPU available: False, used: False\n", "TPU available: False, using: 0 TPU cores\n", "IPU available: False, using: 0 IPUs\n", "HPU available: False, using: 0 HPUs\n", "\n", " | Name | Type | Params\n", "---------------------------------------------------\n", "0 | loss | MAE | 0 \n", "1 | padder_train | ConstantPad1d | 0 \n", "2 | scaler | TemporalNorm | 0 \n", "3 | dense_encoder | Sequential | 289 K \n", "4 | dense_decoder | Sequential | 657 K \n", "5 | temporal_decoder | MLPResidual | 4.4 K \n", "6 | global_skip | Linear | 300 \n", "---------------------------------------------------\n", "951 K Trainable params\n", "0 Non-trainable params\n", "951 K Total params\n", "3.806 Total estimated model params size (MB)\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "128bc65645c748d382d55aee060d2fee", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Sanity Checking: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ccd0755dc9fd4828a700378f0e4cbad7", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Training: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f4eac39947074446ba48b9c3545368bc", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a8d655eeaab8491fa37820e612e3cf2e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8a1bb66964624fcd9258430f985fe011", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "bb3e8d4ef4c84397a4f636726829f954", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "deccca8573cc4b75bc44bd3d79121ba4", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "d176f81532b44d7eb5c2e2990080afdd", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f12abb172af341b49be236a53fa0fb6e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "eda1d73267aa4c1aad2b0264c2679d95", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f16d833e89ff46ed9046824b9c0cac6e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "7c506ef0479944cca4d9bc9465c7c795", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "`Trainer.fit` stopped: `max_steps=1000` reached.\n", "GPU available: False, used: False\n", "TPU available: False, using: 0 TPU cores\n", "IPU available: False, using: 0 IPUs\n", "HPU available: False, using: 0 HPUs\n", "\n", " | Name | Type | Params\n", "-----------------------------------------------------------\n", "0 | loss | MAE | 0 \n", "1 | padder | ConstantPad1d | 0 \n", "2 | scaler | TemporalNorm | 0 \n", "3 | norm | ReversibleInstanceNorm1d | 46 \n", "4 | mixing_layers | Sequential | 11.7 K\n", "5 | out | Linear | 300 \n", "-----------------------------------------------------------\n", "12.0 K Trainable params\n", "0 Non-trainable params\n", "12.0 K Total params\n", "0.048 Total estimated model params size (MB)\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "0e8780bbd805406488be176e4d3125a1", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Sanity Checking: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8b805c53a9244c4493c29f3ce09ce28d", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Training: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "21cafe89a4b5456c91cd3a4f2fb1c269", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "172f130bcbde44a992bd30e3721e1c7a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b53e1a8821e34f479dd2b1740ff3c9dd", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "b47325d6cd454425b838ec67102d4782", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "9c5e90b2996d46fbba08e04420837d0a", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "d1f707ea9dbe4f7dacd0032ad9c3d028", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "cbd12ea8ea6e496780e9f5742875c480", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "7316f50f2b0842aaa203a1c6b6239d68", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "7efef58e6da54ba4ba6be376ae136157", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "62fc39b80aa94fa5b449a23b170308d3", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Validation: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "`Trainer.fit` stopped: `max_steps=1000` reached.\n", "/opt/conda/envs/ai_env/lib/python3.10/site-packages/utilsforecast/processing.py:384: FutureWarning: 'Y' is deprecated and will be removed in a future version, please use 'YE' instead.\n", " freq = pd.tseries.frequencies.to_offset(freq)\n", "/opt/conda/envs/ai_env/lib/python3.10/site-packages/utilsforecast/processing.py:440: FutureWarning: 'Y' is deprecated and will be removed in a future version, please use 'YE' instead.\n", " freq = pd.tseries.frequencies.to_offset(freq)\n", "GPU available: False, used: False\n", "TPU available: False, using: 0 TPU cores\n", "IPU available: False, using: 0 IPUs\n", "HPU available: False, using: 0 HPUs\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "aae5673f4a794e1a8121a2d6e2c182a9", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Predicting: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "GPU available: False, used: False\n", "TPU available: False, using: 0 TPU cores\n", "IPU available: False, using: 0 IPUs\n", "HPU available: False, using: 0 HPUs\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ca7201383ddd4d6c8763d4304ff93bfb", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Predicting: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "GPU available: False, used: False\n", "TPU available: False, using: 0 TPU cores\n", "IPU available: False, using: 0 IPUs\n", "HPU available: False, using: 0 HPUs\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c7938d34d45340b9aafe125bcf0ce673", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Predicting: 0it [00:00, ?it/s]" ] }, "metadata": {}, "output_type": "display_data" }, { "ename": "ValueError", "evalue": "could not broadcast input array from shape (276,1) into shape (12,1)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[11], line 9\u001b[0m\n\u001b[1;32m 7\u001b[0m nf \u001b[38;5;241m=\u001b[39m NeuralForecast(models\u001b[38;5;241m=\u001b[39mmodels, freq\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mY\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 8\u001b[0m nf\u001b[38;5;241m.\u001b[39mfit(train)\n\u001b[0;32m----> 9\u001b[0m forecast_df \u001b[38;5;241m=\u001b[39m \u001b[43mnf\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpredict\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m# %% Evaluate Models\u001b[39;00m\n\u001b[1;32m 12\u001b[0m results \u001b[38;5;241m=\u001b[39m []\n", "File \u001b[0;32m/opt/conda/envs/ai_env/lib/python3.10/site-packages/neuralforecast/core.py:926\u001b[0m, in \u001b[0;36mNeuralForecast.predict\u001b[0;34m(self, df, static_df, futr_df, verbose, engine, level, **data_kwargs)\u001b[0m\n\u001b[1;32m 924\u001b[0m \u001b[38;5;66;03m# Append predictions in memory placeholder\u001b[39;00m\n\u001b[1;32m 925\u001b[0m output_length \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlen\u001b[39m(model\u001b[38;5;241m.\u001b[39mloss\u001b[38;5;241m.\u001b[39moutput_names)\n\u001b[0;32m--> 926\u001b[0m fcsts[:, col_idx : col_idx \u001b[38;5;241m+\u001b[39m output_length] \u001b[38;5;241m=\u001b[39m model_fcsts\n\u001b[1;32m 927\u001b[0m col_idx \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m output_length\n\u001b[1;32m 928\u001b[0m model\u001b[38;5;241m.\u001b[39mset_test_size(old_test_size) \u001b[38;5;66;03m# Set back to original value\u001b[39;00m\n", "\u001b[0;31mValueError\u001b[0m: could not broadcast input array from shape (276,1) into shape (12,1)" ] } ], "source": [ "# %% Train & Predict\n", "start_time = time.time()\n", "\n", "for model in models:\n", " model.h = 12 # Adjust if necessary\n", "\n", "nf = NeuralForecast(models=models, freq=\"Y\")\n", "nf.fit(train)\n", "forecast_df = nf.predict()\n", "\n", "# %% Evaluate Models\n", "results = []\n", "for model_name in forecast_df.columns:\n", " if model_name in [\"ds\", \"unique_id\"]:\n", " continue\n", " y_pred = forecast_df[model_name]\n", " y_pred = y_pred[:val[\"y\"].shape[0]]\n", " results.append(evaluate_model(val[\"y\"], y_pred, train[\"y\"], f\"{model_name} (NeuralForecast)\", start_time))\n", "\n", "# Convert results to DataFrame and sort by MAPE\n", "df_results = pd.DataFrame(results).sort_values(by=\"MAPE\")\n", "print(df_results.head())\n", "\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n" ] } ], "source": [ "print(nf.models[0].h)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "ename": "SIGTERMException", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mSIGTERMException\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[85], line 4\u001b[0m\n\u001b[1;32m 2\u001b[0m start_time \u001b[38;5;241m=\u001b[39m time\u001b[38;5;241m.\u001b[39mtime()\n\u001b[1;32m 3\u001b[0m nf \u001b[38;5;241m=\u001b[39m NeuralForecast(models\u001b[38;5;241m=\u001b[39mmodels, freq\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mY\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m----> 4\u001b[0m \u001b[43mnf\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtrain\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 5\u001b[0m forecast_df \u001b[38;5;241m=\u001b[39m nf\u001b[38;5;241m.\u001b[39mpredict()\n\u001b[1;32m 7\u001b[0m \u001b[38;5;66;03m# %% Evaluate Models\u001b[39;00m\n", "File \u001b[0;32m/opt/conda/envs/ai_env/lib/python3.10/site-packages/neuralforecast/core.py:547\u001b[0m, in \u001b[0;36mNeuralForecast.fit\u001b[0;34m(self, df, static_df, val_size, use_init_models, verbose, id_col, time_col, target_col, distributed_config, prediction_intervals)\u001b[0m\n\u001b[1;32m 544\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_reset_models()\n\u001b[1;32m 546\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i, model \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodels):\n\u001b[0;32m--> 547\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmodels[i] \u001b[38;5;241m=\u001b[39m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 548\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdataset\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mval_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mval_size\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdistributed_config\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdistributed_config\u001b[49m\n\u001b[1;32m 549\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 551\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_fitted \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n", "File \u001b[0;32m/opt/conda/envs/ai_env/lib/python3.10/site-packages/neuralforecast/common/_base_windows.py:661\u001b[0m, in \u001b[0;36mBaseWindows.fit\u001b[0;34m(self, dataset, val_size, test_size, random_seed, distributed_config)\u001b[0m\n\u001b[1;32m 632\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mfit\u001b[39m(\n\u001b[1;32m 633\u001b[0m \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m 634\u001b[0m dataset,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 638\u001b[0m distributed_config\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 639\u001b[0m ):\n\u001b[1;32m 640\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Fit.\u001b[39;00m\n\u001b[1;32m 641\u001b[0m \n\u001b[1;32m 642\u001b[0m \u001b[38;5;124;03m The `fit` method, optimizes the neural network's weights using the\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 659\u001b[0m \u001b[38;5;124;03m `test_size`: int, test size for temporal cross-validation.
\u001b[39;00m\n\u001b[1;32m 660\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 661\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_fit\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 662\u001b[0m \u001b[43m \u001b[49m\u001b[43mdataset\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdataset\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 663\u001b[0m \u001b[43m \u001b[49m\u001b[43mbatch_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbatch_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 664\u001b[0m \u001b[43m \u001b[49m\u001b[43mvalid_batch_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalid_batch_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 665\u001b[0m \u001b[43m \u001b[49m\u001b[43mval_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mval_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 666\u001b[0m \u001b[43m \u001b[49m\u001b[43mtest_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtest_size\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 667\u001b[0m \u001b[43m \u001b[49m\u001b[43mrandom_seed\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrandom_seed\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 668\u001b[0m \u001b[43m \u001b[49m\u001b[43mdistributed_config\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdistributed_config\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 669\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/conda/envs/ai_env/lib/python3.10/site-packages/neuralforecast/common/_base_model.py:360\u001b[0m, in \u001b[0;36mBaseModel._fit\u001b[0;34m(self, dataset, batch_size, valid_batch_size, val_size, test_size, random_seed, shuffle_train, distributed_config)\u001b[0m\n\u001b[1;32m 358\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\n\u001b[1;32m 359\u001b[0m trainer \u001b[38;5;241m=\u001b[39m pl\u001b[38;5;241m.\u001b[39mTrainer(\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mmodel\u001b[38;5;241m.\u001b[39mtrainer_kwargs)\n\u001b[0;32m--> 360\u001b[0m \u001b[43mtrainer\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdatamodule\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdatamodule\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 361\u001b[0m model\u001b[38;5;241m.\u001b[39mmetrics \u001b[38;5;241m=\u001b[39m trainer\u001b[38;5;241m.\u001b[39mcallback_metrics\n\u001b[1;32m 362\u001b[0m model\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__dict__\u001b[39m\u001b[38;5;241m.\u001b[39mpop(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m_trainer\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n", "File \u001b[0;32m/opt/conda/envs/ai_env/lib/python3.10/site-packages/pytorch_lightning/trainer/trainer.py:531\u001b[0m, in \u001b[0;36mTrainer.fit\u001b[0;34m(self, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path)\u001b[0m\n\u001b[1;32m 529\u001b[0m model \u001b[38;5;241m=\u001b[39m _maybe_unwrap_optimized(model)\n\u001b[1;32m 530\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstrategy\u001b[38;5;241m.\u001b[39m_lightning_module \u001b[38;5;241m=\u001b[39m model\n\u001b[0;32m--> 531\u001b[0m \u001b[43mcall\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call_and_handle_interrupt\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 532\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_fit_impl\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtrain_dataloaders\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mval_dataloaders\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdatamodule\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mckpt_path\u001b[49m\n\u001b[1;32m 533\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/conda/envs/ai_env/lib/python3.10/site-packages/pytorch_lightning/trainer/call.py:42\u001b[0m, in \u001b[0;36m_call_and_handle_interrupt\u001b[0;34m(trainer, trainer_fn, *args, **kwargs)\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m trainer\u001b[38;5;241m.\u001b[39mstrategy\u001b[38;5;241m.\u001b[39mlauncher \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 41\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m trainer\u001b[38;5;241m.\u001b[39mstrategy\u001b[38;5;241m.\u001b[39mlauncher\u001b[38;5;241m.\u001b[39mlaunch(trainer_fn, \u001b[38;5;241m*\u001b[39margs, trainer\u001b[38;5;241m=\u001b[39mtrainer, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m---> 42\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mtrainer_fn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 44\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m _TunerExitException:\n\u001b[1;32m 45\u001b[0m _call_teardown_hook(trainer)\n", "File \u001b[0;32m/opt/conda/envs/ai_env/lib/python3.10/site-packages/pytorch_lightning/trainer/trainer.py:570\u001b[0m, in \u001b[0;36mTrainer._fit_impl\u001b[0;34m(self, model, train_dataloaders, val_dataloaders, datamodule, ckpt_path)\u001b[0m\n\u001b[1;32m 560\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_data_connector\u001b[38;5;241m.\u001b[39mattach_data(\n\u001b[1;32m 561\u001b[0m model, train_dataloaders\u001b[38;5;241m=\u001b[39mtrain_dataloaders, val_dataloaders\u001b[38;5;241m=\u001b[39mval_dataloaders, datamodule\u001b[38;5;241m=\u001b[39mdatamodule\n\u001b[1;32m 562\u001b[0m )\n\u001b[1;32m 564\u001b[0m ckpt_path \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_checkpoint_connector\u001b[38;5;241m.\u001b[39m_select_ckpt_path(\n\u001b[1;32m 565\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstate\u001b[38;5;241m.\u001b[39mfn,\n\u001b[1;32m 566\u001b[0m ckpt_path,\n\u001b[1;32m 567\u001b[0m model_provided\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[1;32m 568\u001b[0m model_connected\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mlightning_module \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 569\u001b[0m )\n\u001b[0;32m--> 570\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_run\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mckpt_path\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mckpt_path\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 572\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstate\u001b[38;5;241m.\u001b[39mstopped\n\u001b[1;32m 573\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtraining \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n", "File \u001b[0;32m/opt/conda/envs/ai_env/lib/python3.10/site-packages/pytorch_lightning/trainer/trainer.py:975\u001b[0m, in \u001b[0;36mTrainer._run\u001b[0;34m(self, model, ckpt_path)\u001b[0m\n\u001b[1;32m 970\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_signal_connector\u001b[38;5;241m.\u001b[39mregister_signal_handlers()\n\u001b[1;32m 972\u001b[0m \u001b[38;5;66;03m# ----------------------------\u001b[39;00m\n\u001b[1;32m 973\u001b[0m \u001b[38;5;66;03m# RUN THE TRAINER\u001b[39;00m\n\u001b[1;32m 974\u001b[0m \u001b[38;5;66;03m# ----------------------------\u001b[39;00m\n\u001b[0;32m--> 975\u001b[0m results \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_run_stage\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 977\u001b[0m \u001b[38;5;66;03m# ----------------------------\u001b[39;00m\n\u001b[1;32m 978\u001b[0m \u001b[38;5;66;03m# POST-Training CLEAN UP\u001b[39;00m\n\u001b[1;32m 979\u001b[0m \u001b[38;5;66;03m# ----------------------------\u001b[39;00m\n\u001b[1;32m 980\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__class__\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m: trainer tearing down\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", "File \u001b[0;32m/opt/conda/envs/ai_env/lib/python3.10/site-packages/pytorch_lightning/trainer/trainer.py:1018\u001b[0m, in \u001b[0;36mTrainer._run_stage\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1016\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_run_sanity_check()\n\u001b[1;32m 1017\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m torch\u001b[38;5;241m.\u001b[39mautograd\u001b[38;5;241m.\u001b[39mset_detect_anomaly(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_detect_anomaly):\n\u001b[0;32m-> 1018\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit_loop\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1019\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 1020\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUnexpected state \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstate\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n", "File \u001b[0;32m/opt/conda/envs/ai_env/lib/python3.10/site-packages/pytorch_lightning/loops/fit_loop.py:202\u001b[0m, in \u001b[0;36m_FitLoop.run\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 200\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mon_advance_start()\n\u001b[1;32m 201\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39madvance()\n\u001b[0;32m--> 202\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mon_advance_end\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 203\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_restarting \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[1;32m 204\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mStopIteration\u001b[39;00m:\n", "File \u001b[0;32m/opt/conda/envs/ai_env/lib/python3.10/site-packages/pytorch_lightning/loops/fit_loop.py:389\u001b[0m, in \u001b[0;36m_FitLoop.on_advance_end\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 386\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mepoch_progress\u001b[38;5;241m.\u001b[39mincrement_completed()\n\u001b[1;32m 388\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m trainer\u001b[38;5;241m.\u001b[39mreceived_sigterm:\n\u001b[0;32m--> 389\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m SIGTERMException\n", "\u001b[0;31mSIGTERMException\u001b[0m: " ] }, { "ename": "", "evalue": "", "output_type": "error", "traceback": [ "\u001b[1;31mThe Kernel crashed while executing code in the current cell or a previous cell. \n", "\u001b[1;31mPlease review the code in the cell(s) to identify a possible cause of the failure. \n", "\u001b[1;31mClick here for more info. \n", "\u001b[1;31mView Jupyter log for further details." ] } ], "source": [ "%tb" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "ai_env", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.16" } }, "nbformat": 4, "nbformat_minor": 2 }