From b3052cae9d70e2623ce7088a927bd8889262d24b Mon Sep 17 00:00:00 2001 From: misha-chertushkin Date: Mon, 17 Feb 2025 11:39:33 +0000 Subject: [PATCH 1/3] Fix import in notebook --- notebooks/finetuning_torch.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notebooks/finetuning_torch.ipynb b/notebooks/finetuning_torch.ipynb index c50dca1..545822a 100644 --- a/notebooks/finetuning_torch.ipynb +++ b/notebooks/finetuning_torch.ipynb @@ -32,7 +32,7 @@ "import torch\n", "import torch.multiprocessing as mp\n", "import yfinance as yf\n", - "from timesfm.finetuning_torch import FinetuningConfig, TimesFMFinetuner\n", + "from finetuning.finetuning_torch import FinetuningConfig, TimesFMFinetuner\n", "from huggingface_hub import snapshot_download\n", "from torch.utils.data import Dataset\n", "\n", From 5117a0e33834561deb20fd568fcead5b5449c3d6 Mon Sep 17 00:00:00 2001 From: misha-chertushkin Date: Mon, 17 Feb 2025 11:51:20 +0000 Subject: [PATCH 2/3] Fix the data function --- notebooks/finetuning_torch.ipynb | 234 +++++++++++++++++++++++++++++-- 1 file changed, 225 insertions(+), 9 deletions(-) diff --git a/notebooks/finetuning_torch.ipynb b/notebooks/finetuning_torch.ipynb index 545822a..970fd51 100644 --- a/notebooks/finetuning_torch.ipynb +++ b/notebooks/finetuning_torch.ipynb @@ -20,9 +20,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "TimesFM v1.2.0. See https://github.com/google-research/timesfm/blob/master/README.md for updated APIs.\n", + "Loaded Jax TimesFM.\n", + "Loaded PyTorch TimesFM.\n" + ] + } + ], "source": [ "from os import path\n", "from typing import Optional, Tuple\n", @@ -139,7 +149,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -168,7 +178,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -252,10 +262,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ + "def get_data(context_len: int,\n", + " horizon_len: int,\n", + " freq_type: int = 0) -> Tuple[Dataset, Dataset]:\n", + " df = yf.download(\"AAPL\", start=\"2010-01-01\", end=\"2019-01-01\")\n", + " time_series = df[\"Close\"].values\n", + "\n", + " train_dataset, val_dataset = prepare_datasets(\n", + " series=time_series,\n", + " context_length=context_len,\n", + " horizon_length=horizon_len,\n", + " freq_type=freq_type,\n", + " train_split=0.8,\n", + " )\n", + "\n", + " print(f\"Created datasets:\")\n", + " print(f\"- Training samples: {len(train_dataset)}\")\n", + " print(f\"- Validation samples: {len(val_dataset)}\")\n", + " print(f\"- Using frequency type: {freq_type}\")\n", + " return train_dataset, val_dataset\n", + "\n", + "\n", "\n", "def single_gpu_example():\n", " \"\"\"Basic example of finetuning TimesFM on stock data.\"\"\"\n", @@ -290,17 +321,202 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "ac84aeda3a1749ae8f30b06859067bb1", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Fetching 3 files: 0%| | 0/3 [00:00" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "Run data is saved locally in /home/chertushkin/forks/timesfm/notebooks/wandb/run-20250217_114343-tjs63ml2" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "Syncing run chocolate-eon-50 to Weights & Biases (docs)
" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + " View project at https://wandb.ai/mishacamry/timesfm-finetuning" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + " View run at https://wandb.ai/mishacamry/timesfm-finetuning/runs/tjs63ml2" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Starting finetuning...\n" + ] + }, + { + "data": { + "text/html": [], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "

Run history:


epoch▁▃▅▆█
learning_rate▁▁▁▁▁
train_loss█▃▂▁▁
val_loss█▁▄▁▂

Run summary:


epoch5
learning_rate0.0001
train_loss2.85423
val_loss26.7628

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + " View run chocolate-eon-50 at: https://wandb.ai/mishacamry/timesfm-finetuning/runs/tjs63ml2
View project at: https://wandb.ai/mishacamry/timesfm-finetuning
Synced 5 W&B file(s), 0 media file(s), 0 artifact file(s) and 0 other file(s)" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "Find logs at: ./wandb/run-20250217_114343-tjs63ml2/logs" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Finetuning completed!\n", + "Training history: 5 epochs\n", + "Plot saved to timesfm_predictions.png\n" + ] + } + ], "source": [ "single_gpu_example()" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "timesfm-311", + "display_name": "timesfm-DnAbSweh-py3.11", "language": "python", "name": "python3" }, @@ -314,7 +530,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.11" + "version": "3.11.10" } }, "nbformat": 4, From cb578f30c52797ffe335b0de0e6c0ca6432ad97f Mon Sep 17 00:00:00 2001 From: misha-chertushkin Date: Mon, 17 Feb 2025 12:00:20 +0000 Subject: [PATCH 3/3] Update readme --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 59e0e4e..c29d978 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,10 @@ We recommend at least 32GB RAM to load TimesFM dependencies. - Launched [finetuning support](https://github.com/google-research/timesfm/blob/master/notebooks/finetuning.ipynb) that lets you finetune the weights of the pretrained TimesFM model on your own data. - Launched [~zero-shot covariate support](https://github.com/google-research/timesfm/blob/master/notebooks/covariates.ipynb) with external regressors. More details [here](https://github.com/google-research/timesfm?tab=readme-ov-file#covariates-support). +## Update - Feb. 17, 2024 +- We are providing the option for [finetuning using Pytorch](https://github.com/google-research/timesfm/blob/master/notebooks/finetuning_torch.ipynb), which mimics the previously added functionality from [finetuning support](https://github.com/google-research/timesfm/blob/master/notebooks/finetuning.ipynb). +- We are also providing the Multi-GPU finetuining with Pytorch. We currently support DDP multi-gpu finetuning, other variants of multi-gpu training (pipeline parallelism/model parallelism) might be added later. In order to use it, follow the steps in [finetuning example](https://github.com/google-research/timesfm/blob/master/finetuning/finetuning_example.py) . + ## Checkpoint timesfm-1.0-200m (-pytorch) timesfm-1.0-200m is our first open model checkpoint: