325 lines
10 KiB
Plaintext
325 lines
10 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import sys\n",
|
|
"sys.path.append('../..')\n",
|
|
"import torch"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from src.data import DataProcessor, DataConfig\n",
|
|
"from src.trainers.quantile_trainer import AutoRegressiveQuantileTrainer, NonAutoRegressiveQuantileRegression\n",
|
|
"from src.trainers.probabilistic_baseline import ProbabilisticBaselineTrainer\n",
|
|
"from src.trainers.autoregressive_trainer import AutoRegressiveTrainer\n",
|
|
"from src.trainers.trainer import Trainer\n",
|
|
"from src.utils.clearml import ClearMLHelper\n",
|
|
"from src.models import *\n",
|
|
"from src.losses import *\n",
|
|
"import torch\n",
|
|
"import numpy as np\n",
|
|
"from torch.nn import MSELoss, L1Loss\n",
|
|
"from datetime import datetime\n",
|
|
"import torch.nn as nn\n",
|
|
"from src.models.time_embedding_layer import TimeEmbedding\n",
|
|
"from src.models.diffusion_model import SimpleDiffusionModel, GRUDiffusionModel\n",
|
|
"from src.trainers.diffusion_trainer import DiffusionTrainer\n",
|
|
"from torchinfo import summary\n",
|
|
"\n",
|
|
"\n",
|
|
"%load_ext autoreload\n",
|
|
"%autoreload 2\n",
|
|
"\n",
|
|
"clearml_helper = ClearMLHelper(project_name=\"Thesis/NrvForecast\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#### Data Processor ####\n",
|
|
"data_config = DataConfig()\n",
|
|
"data_config.NRV_HISTORY = True\n",
|
|
"data_config.LOAD_HISTORY = True\n",
|
|
"data_config.LOAD_FORECAST = True\n",
|
|
"\n",
|
|
"data_config.WIND_FORECAST = True\n",
|
|
"data_config.WIND_HISTORY = True\n",
|
|
"\n",
|
|
"data_config.QUARTER = False\n",
|
|
"data_config.DAY_OF_WEEK = False\n",
|
|
"\n",
|
|
"data_config.NOMINAL_NET_POSITION = True\n",
|
|
"\n",
|
|
"data_processor = DataProcessor(data_config, path=\"../../\", lstm=True)\n",
|
|
"data_processor.set_batch_size(1024)\n",
|
|
"data_processor.set_full_day_skip(True)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"torch.Size([1024, 96, 96])\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/loss.py:536: UserWarning: Using a target size (torch.Size([1024, 96])) that is different to the input size (torch.Size([2, 1024, 96])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.\n",
|
|
" return F.mse_loss(input, target, reduction=self.reduction)\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"torch.Size([556, 96, 96])\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/loss.py:536: UserWarning: Using a target size (torch.Size([556, 96])) that is different to the input size (torch.Size([2, 556, 96])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.\n",
|
|
" return F.mse_loss(input, target, reduction=self.reduction)\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n",
|
|
"torch.Size([1024, 96, 96])\n",
|
|
"torch.Size([556, 96, 96])\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
"KeyboardInterrupt\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"inputDim = data_processor.get_input_size()\n",
|
|
"learningRate = 0.0001\n",
|
|
"epochs=150\n",
|
|
"\n",
|
|
"#### Model ####\n",
|
|
"# model = SimpleDiffusionModel(96, [512, 512, 512], other_inputs_dim=inputDim[1], time_dim=64)\n",
|
|
"model = GRUDiffusionModel(96, [256, 256], other_inputs_dim=inputDim[2], time_dim=64, gru_hidden_size=128)\n",
|
|
"\n",
|
|
"#### ClearML ####\n",
|
|
"# task = clearml_helper.get_task(task_name=\"Diffusion Model\")\n",
|
|
"\n",
|
|
"#### Trainer ####\n",
|
|
"trainer = DiffusionTrainer(model, data_processor, \"cuda\")\n",
|
|
"trainer.train(epochs, learningRate, None)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"<class 'src.models.diffusion_model.SimpleDiffusionModel'>\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"new_model = torch.load(\"checkpoint.pt\")\n",
|
|
"print(type(new_model))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Determine threshold based on predictions\n",
|
|
"from src.models.diffusion_model import DiffusionModel\n",
|
|
"\n",
|
|
"\n",
|
|
"def get_predicted_NRV(date):\n",
|
|
" idx = test_loader.dataset.get_idx_for_date(date.date())\n",
|
|
" initial, _, samples, target = auto_regressive(test_loader.dataset, [idx]*500, 96)\n",
|
|
" samples = samples.cpu().numpy()\n",
|
|
" target = target.cpu().numpy()\n",
|
|
"\n",
|
|
" # inverse using data_processor\n",
|
|
" samples = data_processor.inverse_transform(samples)\n",
|
|
" target = data_processor.inverse_transform(target)\n",
|
|
"\n",
|
|
" return initial.cpu().numpy()[0][-1], samples, target\n",
|
|
"\n",
|
|
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
|
|
"\n",
|
|
"def sample_diffusion(model: DiffusionModel, n: int, inputs: torch.tensor):\n",
|
|
" noise_steps = 1000\n",
|
|
" beta_start = 1e-4\n",
|
|
" beta_end = 0.02\n",
|
|
" ts_length = 96\n",
|
|
" \n",
|
|
" beta = torch.linspace(beta_start, beta_end, noise_steps).to(device)\n",
|
|
" alpha = 1. - beta\n",
|
|
" alpha_hat = torch.cumprod(alpha, dim=0)\n",
|
|
"\n",
|
|
" inputs = inputs.repeat(n, 1).to(device)\n",
|
|
" model.eval()\n",
|
|
" with torch.no_grad():\n",
|
|
" x = torch.randn(inputs.shape[0], ts_length).to(device)\n",
|
|
" for i in reversed(range(1, noise_steps)):\n",
|
|
" t = (torch.ones(inputs.shape[0]) * i).long().to(device)\n",
|
|
" predicted_noise = model(x, t, inputs)\n",
|
|
" _alpha = alpha[t][:, None]\n",
|
|
" _alpha_hat = alpha_hat[t][:, None]\n",
|
|
" _beta = beta[t][:, None]\n",
|
|
"\n",
|
|
" if i > 1:\n",
|
|
" noise = torch.randn_like(x)\n",
|
|
" else:\n",
|
|
" noise = torch.zeros_like(x)\n",
|
|
"\n",
|
|
" x = 1/torch.sqrt(_alpha) * (x-((1-_alpha) / (torch.sqrt(1 - _alpha_hat))) * predicted_noise) + torch.sqrt(_beta) * noise\n",
|
|
" return x\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 27,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"tensor([[-178.8835, -47.2518, -103.9158, -9.8302, 15.9751, 138.9138,\n",
|
|
" -56.8392, -128.0629, -128.3637, -83.1066, 56.6656, -200.4618,\n",
|
|
" 10.8563, -146.4262, 120.4816, -60.1130, -18.7972, -214.0427,\n",
|
|
" 148.1229, 136.0194, 33.7580, 85.7884, -164.5678, 53.8879,\n",
|
|
" 187.6217, -77.5978, 153.7462, -129.1419, -149.8551, 118.4640,\n",
|
|
" -29.4688, -37.3348, -104.4318, -16.1735, -29.9716, -1.4205,\n",
|
|
" -130.6785, 23.8387, 75.6755, 113.8617, -61.4832, -81.3838,\n",
|
|
" -15.3194, -63.5703, 215.4112, 8.0719, 26.4597, 72.4347,\n",
|
|
" -23.1216, 44.8453, -12.2994, 94.7612, -162.2193, 18.0694,\n",
|
|
" 31.2402, 78.6964, 35.1892, -105.0744, 38.7805, -27.5867,\n",
|
|
" 39.5985, 136.5500, -179.8039, 231.9039, 116.1411, -226.0043,\n",
|
|
" -149.2595, -14.5097, 123.5570, 162.4510, -62.9467, -82.3552,\n",
|
|
" 187.5180, 12.3145, -189.3492, -159.3642, -144.8646, 130.9768,\n",
|
|
" -79.4541, 53.5424, 35.7119, 134.5416, -87.5582, 70.4020,\n",
|
|
" -44.0516, 111.3181, 17.0087, -14.9322, -187.4202, -41.7765,\n",
|
|
" 11.2264, 221.0164, -106.3083, -123.9814, -12.2132, -121.7845]],\n",
|
|
" device='cuda:0')"
|
|
]
|
|
},
|
|
"execution_count": 27,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"inputs = torch.randn(1, 672).to(device)\n",
|
|
"sample_diffusion(new_model, 1, inputs)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "base",
|
|
"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.8"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|