{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c39d4125-fd4e-4200-9dc2-662c1cecf083",
   "metadata": {},
   "source": [
    "## Live loss plotting during training"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "856307a8-e490-4957-9ede-fbfbc1d453f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "if 'google.colab' in str(get_ipython()):\n",
    "    !pip install git+https://github.com/ourownstory/neural_prophet.git['live'] # may take a while\n",
    "    #!pip install neuralprophet # much faster, but may not have the latest upgrades/bugfixes\n",
    "    \n",
    "import pandas as pd\n",
    "from neuralprophet import NeuralProphet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a7b9835d-6ee9-424a-b5dd-a624105cb5e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_location = \"https://raw.githubusercontent.com/ourownstory/neuralprophet-data/main/datasets/\"\n",
    "df = pd.read_csv(data_location + \"retail_sales.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f492482a-d43b-40b2-a958-44cb6d54dfb1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO - (NP.df_utils._infer_frequency) - Major frequency MS corresponds to 91.126% of the data.\n",
      "INFO - (NP.df_utils._infer_frequency) - Dataframe freq automatically defined as MS\n"
     ]
    }
   ],
   "source": [
    "df_train, df_val = NeuralProphet().split_df(df, valid_p=0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "30e3b0ac-9b92-44cc-bac2-c327e9fe2032",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 55228 characters]\n",
      "text/plain": [
       "<Figure size 864x576 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "log-SmoothL1Loss\n",
      "\ttraining         \t (min:   -7.928, max:    0.020, cur:   -7.928)\n",
      "\tvalidation       \t (min:   -6.325, max:   -0.590, cur:   -5.703)\n"
     ]
    }
   ],
   "source": [
    "m = NeuralProphet()\n",
    "metrics = m.fit(df_train, validation_df=df_val, progress=\"plot\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "08c0be09-7d63-4371-9028-61c76e98e219",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 73508 characters]\n",
      "text/plain": [
       "<Figure size 864x864 with 4 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MAE\n",
      "\ttraining         \t (min: 5907.567, max: 323312.405, cur: 5907.567)\n",
      "\tvalidation       \t (min: 6978.492, max: 453438.930, cur: 16642.114)\n",
      "RMSE\n",
      "\ttraining         \t (min: 7487.907, max: 399061.950, cur: 7586.051)\n",
      "\tvalidation       \t (min: 8699.636, max: 519890.633, cur: 19649.841)\n",
      "RegLoss\n",
      "\tRegLoss          \t (min:    0.000, max:    0.000, cur:    0.000)\n",
      "log-SmoothL1Loss\n",
      "\ttraining         \t (min:   -7.806, max:   -0.233, cur:   -7.806)\n",
      "\tvalidation       \t (min:   -7.559, max:    0.195, cur:   -5.929)\n"
     ]
    }
   ],
   "source": [
    "m = NeuralProphet()\n",
    "metrics = m.fit(df_train, validation_df=df_val, progress=\"plot-all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2cc4d0df-0aca-4d3d-bfac-9bc0de21fe20",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "np-dev",
   "language": "python",
   "name": "np-dev"
  },
  "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.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
