Files
DeepTime/scratch.ipynb
T
2022-11-20 14:29:25 +08:00

482 KiB

In [2]:

import os
from os.path import join
import math
import logging
from typing import Callable, Optional, Union, Dict, Tuple

from matplotlib import pyplot as plt
from pathlib import Path
import matplotlib.colors as mcolors

import gin
from fire import Fire
import numpy as np
import torch
from torch.utils.data import DataLoader
from torch import optim
from torch import nn

from experiments.base import Experiment
from data.datasets import ForecastDataset
from models import get_model
from utils.checkpoint import Checkpoint
from utils.ops import default_device, to_tensor
from utils.losses import get_loss_fn
from utils.metrics import calc_metrics

from experiments.forecast import get_data
gin.enter_interactive_mode()
In [ ]:
In [3]:


gin.clear_config()
gin.parse_config(open("storage/experiments/Exchange/96M2/repeat=0/config.gin"))

train_set, train_loader = get_data(flag='train', batch_size=16)

model = get_model("deeptime",
                    dim_size=train_set.data_x.shape[1],
                    datetime_feats=train_set.timestamps.shape[-1]).to(default_device())
model.load_state_dict(torch.load('storage/experiments/Exchange/96M2/repeat=0/model.pth'))
model = model.eval()


b = train_set[1]
b = [bb[None, :] for bb in b]
x, y, x_time, y_time = map(to_tensor, b)
with torch.no_grad():
    forecast = model(x, x_time, y_time)


plt.title('mlp inr2')
import matplotlib.colors as mcolors
colors = list(mcolors.BASE_COLORS.keys())
l = x.shape[1]
forecast2 = forecast[0].detach().cpu().numpy()
x2 = x[0].cpu()
y2 = y[0].cpu()
l2 = y.shape[1]
i_past = list(range(l))
i_future = list(range(l, l+l2))
for i in range(x.shape[-1]):
    plt.plot(i_past, x2[:, i], c=colors[i])
for i in range(x.shape[-1]):
    plt.plot(i_future, y2[:, i], c=colors[i])
for i in range(x.shape[-1]):
    plt.plot(i_future, forecast2[:, i], c=colors[i], linestyle='--')

auto

In [4]:


def plot(model_name="deeptime", save_path=Path("storage/experiments/Exchange/96M/repeat=0"), i=200, title=None, plot=True):

    gin.clear_config()
    gin.parse_config(open(save_path/"config.gin"))

    train_set, train_loader = get_data(flag='train', batch_size=2)

    model = get_model(model_name,
                        dim_size=train_set.data_x.shape[1],
                        datetime_feats=train_set.timestamps.shape[-1]).to(default_device())
    model.load_state_dict(torch.load(save_path/'model.pth'))
    model = model.eval()


    b = train_set[i]
    b = [bb[None, :] for bb in b]
    x, y, x_time, y_time = map(to_tensor, b)
    with torch.no_grad():
        forecast = model(x, x_time, y_time)

    if title is None:
        title = str(save_path).split('/')[-3:]
        title = "-".join(title)
    
    colors = list(mcolors.BASE_COLORS.keys())
    l = x.shape[1]
    forecast2 = forecast[0].detach().cpu().numpy()
    x2 = x[0].cpu()
    y2 = y[0].cpu()
    l2 = y.shape[1]
    i_past = list(range(l))
    i_future = list(range(l, l+l2))
    
    if plot:
        plt.title(title)
        for i in range(x.shape[-1]):
            plt.plot(i_past, x2[:, i], c=colors[i])
        for i in range(x.shape[-1]):
            plt.plot(i_future, y2[:, i], c=colors[i])
        for i in range(x.shape[-1]):
            plt.plot(i_future, forecast2[:, i], c=colors[i], linestyle='--')
    return x2, y2, forecast2, i_past, i_future
In [54]:


def plot_multi(model_names=["deeptime"], save_paths=[Path("storage/experiments/Exchange/96M/repeat=0")], i=200, title=None, plot=True):
    for j in range(len(model_names)):
        model_name = model_names[j]
        save_path = save_paths[j]

        gin.clear_config()
        gin.parse_config(open(save_path/"config.gin"))

        train_set, train_loader = get_data(flag='train', batch_size=2)

        model = get_model(model_name,
                            dim_size=train_set.data_x.shape[1],
                            datetime_feats=train_set.timestamps.shape[-1]).to(default_device())
        model.load_state_dict(torch.load(save_path/'model.pth'))
        model = model.eval()


        b = train_set[i]
        b = [bb[None, :] for bb in b]
        x, y, x_time, y_time = map(to_tensor, b)
        with torch.no_grad():
            forecast = model(x, x_time, y_time)

#         if title is None:
#             title = str(save_path).split('/')[-3:]
#             title = "-".join(title)


       
        colors = list(mcolors.BASE_COLORS.keys())
        l = x.shape[1]
        forecast2 = forecast[0].detach().cpu().numpy()
        x2 = x[0].cpu()
        y2 = y[0].cpu()
        l2 = y.shape[1]
        i_past = list(range(l))
        i_future = list(range(l, l+l2))

        linestyles =  [ '--', '-.', '-', ':']
        if plot:
            ls = linestyles[j]
            print(ls)
            if j==0:
                
                
                for k in range(x.shape[-1]):
                    plt.plot(i_past, x2[:, k], c=colors[k], label=f"var {k}")
                for k in range(x.shape[-1]):
                    plt.plot(i_future, y2[:, k], c=colors[k])
            
            mtitle = str(save_path).split('/')[-2:-1]
            mtitle = "-".join(mtitle)
            for k in range(x.shape[-1]):
                plt.plot(i_future, forecast2[:, k], c=colors[k], linestyle=ls, label=f"{mtitle}" if k==0 else None)
    plt.legend()
    plt.title(title)
    return x2, y2, forecast2, i_past, i_future
In [62]:
plot_multi(model_names=["deeptime", "deeptime2"],
           save_paths=[
               Path("storage/experiments/Exchange/96M/repeat=0"),
               Path("storage/experiments/Exchange/96Mplus/repeat=0"),
           ],
           i=100
          )
1
Out [62]:
--
receptive field [690 378 242]=[138  18   2]*[[ 1  1  1]
 [ 1  2  4]
 [ 1  4 16]
 [ 1  6 36]
 [ 1  8 64]]
-.
1
In [64]:
plot_multi(model_names=["deeptime", "deeptime2"],
           save_paths=[
               Path("storage/experiments/Exchange/96M2/repeat=0"),
               Path("storage/experiments/Exchange/96Mplus2/repeat=0"),
           ])
1
Out [64]:
--
receptive field [690 378 242]=[138  18   2]*[[ 1  1  1]
 [ 1  2  4]
 [ 1  4 16]
 [ 1  6 36]
 [ 1  8 64]]
-.
1
In [67]:
plot("deeptime", Path("storage/experiments/Exchange/96S/repeat=0"));
In [57]:
plot("deeptime", Path("storage/experiments/Exchange/96M2/repeat=0"))
Out [57]:
(tensor([[ 0.5292,  1.7510,  0.7053,  ..., -0.8215, -0.8000, -0.0716],
         [ 0.4468,  1.6824,  0.6341,  ..., -0.8088, -0.7295, -0.0506],
         [ 0.5728,  1.7182,  0.6890,  ..., -0.8497, -0.7756,  0.0018],
         ...,
         [ 0.5457,  0.0489,  0.8019,  ..., -1.4872, -0.9826, -0.3539],
         [ 0.5340,  0.0770,  0.7975,  ..., -1.4627, -0.9620, -0.3570],
         [ 0.5195,  0.0113,  0.8092,  ..., -1.4745, -0.9472, -0.3507]]),
 tensor([[ 5.4374e-01,  1.3666e-01,  8.0406e-01, -8.8760e-01,  1.9074e+00,
          -1.4300e+00, -9.5311e-01, -3.3919e-01],
         [ 5.5538e-01,  2.2618e-01,  8.1282e-01, -8.4952e-01,  1.9074e+00,
          -1.4273e+00, -8.8462e-01, -3.0561e-01],
         [ 5.7478e-01,  2.4110e-01,  8.2086e-01, -8.3329e-01,  1.9123e+00,
          -1.3801e+00, -8.4853e-01, -3.2345e-01],
         [ 5.8739e-01,  2.4588e-01,  8.2966e-01, -8.3030e-01,  1.9123e+00,
          -1.3864e+00, -8.5757e-01, -3.2240e-01],
         [ 5.7769e-01,  1.6949e-01,  8.3553e-01, -8.8549e-01,  1.9123e+00,
          -1.4164e+00, -8.3038e-01, -3.2870e-01],
         [ 5.8739e-01,  1.3964e-01,  8.3480e-01, -9.1691e-01,  1.9057e+00,
          -1.4373e+00, -8.2736e-01, -3.5388e-01],
         [ 5.9127e-01,  1.5934e-01,  8.3406e-01, -8.9600e-01,  1.9057e+00,
          -1.4200e+00, -8.3341e-01, -3.4759e-01],
         [ 5.7769e-01,  1.3069e-01,  8.4067e-01, -9.1274e-01,  1.9057e+00,
          -1.4191e+00, -8.2736e-01, -3.2555e-01],
         [ 5.7381e-01,  9.4885e-02,  8.5245e-01, -9.3146e-01,  1.9057e+00,
          -1.4236e+00, -8.2736e-01, -3.3395e-01],
         [ 5.3889e-01,  9.7869e-02,  8.5984e-01, -9.5007e-01,  1.9057e+00,
          -1.4164e+00, -8.2736e-01, -3.3290e-01],
         [ 5.3017e-01, -5.7301e-02,  8.5320e-01, -1.0214e+00,  1.8953e+00,
          -1.4545e+00, -8.5457e-01, -3.3185e-01],
         [ 5.4374e-01, -2.4828e-01,  8.5320e-01, -1.0635e+00,  1.8835e+00,
          -1.5045e+00, -9.4125e-01, -3.6018e-01],
         [ 5.6799e-01, -2.0232e-01,  8.6945e-01, -1.0535e+00,  1.8636e+00,
          -1.4645e+00, -9.3532e-01, -3.1296e-01],
         [ 5.9224e-01,  2.9236e-02,  8.5689e-01, -9.5007e-01,  1.8699e+00,
          -1.4327e+00, -8.4853e-01, -3.0981e-01],
         [ 6.2230e-01,  1.5158e-01,  8.5910e-01, -8.7495e-01,  1.8734e+00,
          -1.4146e+00, -8.3643e-01, -3.0771e-01],
         [ 6.0678e-01, -6.0420e-04,  8.5541e-01, -9.8697e-01,  1.8734e+00,
          -1.4473e+00, -8.3643e-01, -3.2135e-01],
         [ 5.9709e-01,  4.7140e-02,  8.5172e-01, -9.6858e-01,  1.8734e+00,
          -1.4427e+00, -8.3825e-01, -3.2870e-01],
         [ 5.9321e-01,  5.0124e-02,  8.7242e-01, -9.4884e-01,  1.8734e+00,
          -1.4427e+00, -8.3643e-01, -3.3395e-01],
         [ 5.9709e-01,  1.0980e-01,  8.7909e-01, -9.2648e-01,  1.8734e+00,
          -1.4227e+00, -8.3341e-01, -3.2240e-01],
         [ 5.9418e-01,  5.3108e-02,  8.8058e-01, -9.6036e-01,  1.8734e+00,
          -1.4427e+00, -8.1219e-01, -3.1296e-01],
         [ 6.0388e-01,  5.0124e-02,  8.6871e-01, -9.7268e-01,  1.8734e+00,
          -1.4354e+00, -8.1219e-01, -3.1926e-01],
         [ 5.3501e-01,  1.1577e-01,  8.7984e-01, -9.6448e-01,  1.8734e+00,
          -1.3946e+00, -8.0609e-01, -3.4129e-01],
         [ 6.0000e-01,  1.3069e-01,  8.7242e-01, -9.5337e-01,  1.8734e+00,
          -1.3592e+00, -8.0609e-01, -3.1821e-01],
         [ 6.0775e-01,  1.2174e-01,  8.7761e-01, -9.6776e-01,  1.8734e+00,
          -1.3873e+00, -8.0913e-01, -2.6259e-01]]),
 array([[ 0.52890754,  0.04601173,  0.818106  , -0.9959081 ,  1.9366491 ,
         -1.5069696 , -0.9808905 , -0.36548147],
        [ 0.528438  ,  0.03981169,  0.8184106 , -0.9991272 ,  1.937193  ,
         -1.5084361 , -0.9828327 , -0.36484447],
        [ 0.5307723 ,  0.04257247,  0.8204612 , -1.0004759 ,  1.9423875 ,
         -1.5103797 , -0.9852797 , -0.36599845],
        [ 0.5303809 ,  0.04465088,  0.8191587 , -0.9979849 ,  1.9372461 ,
         -1.505586  , -0.9807858 , -0.36503443],
        [ 0.5305283 ,  0.04264332,  0.8198152 , -1.0008417 ,  1.9394246 ,
         -1.5105733 , -0.9837851 , -0.3655746 ],
        [ 0.5307227 ,  0.04202083,  0.8191189 , -0.99933463,  1.9369458 ,
         -1.506729  , -0.98293924, -0.36503708],
        [ 0.5283215 ,  0.04141821,  0.81586105, -0.9971103 ,  1.9277518 ,
         -1.5040027 , -0.978884  , -0.36278751],
        [ 0.5252694 ,  0.04255188,  0.8136384 , -0.9928628 ,  1.9213691 ,
         -1.4981959 , -0.9740302 , -0.36309323],
        [ 0.5275271 ,  0.04352546,  0.814939  , -0.9949809 ,  1.9263753 ,
         -1.5013223 , -0.9757192 , -0.3638668 ],
        [ 0.5276257 ,  0.04390374,  0.8143943 , -0.9922678 ,  1.9258269 ,
         -1.5007619 , -0.97613275, -0.36400792],
        [ 0.52339524,  0.04113823,  0.81086344, -0.9905842 ,  1.9132272 ,
         -1.4942402 , -0.9708606 , -0.36080262],
        [ 0.5248644 ,  0.042987  ,  0.81305283, -0.9912248 ,  1.9210671 ,
         -1.4969778 , -0.9736485 , -0.3629068 ],
        [ 0.52518415,  0.04741649,  0.81146616, -0.98756695,  1.9176267 ,
         -1.4939798 , -0.97169137, -0.36096278],
        [ 0.5220071 ,  0.04414371,  0.81153566, -0.98984647,  1.9137857 ,
         -1.4954246 , -0.97107446, -0.36085606],
        [ 0.52268267,  0.0460408 ,  0.8109568 , -0.9874458 ,  1.9127601 ,
         -1.4930701 , -0.9692728 , -0.3606753 ],
        [ 0.5223365 ,  0.04568218,  0.8103212 , -0.98693126,  1.9110385 ,
         -1.4933776 , -0.96929896, -0.3607128 ],
        [ 0.52153313,  0.04434089,  0.8080093 , -0.9862203 ,  1.9069732 ,
         -1.4901737 , -0.96673113, -0.3586038 ],
        [ 0.5211224 ,  0.04329763,  0.80827016, -0.9870948 ,  1.9067513 ,
         -1.4907483 , -0.96722066, -0.35898498],
        [ 0.51958317,  0.0411768 ,  0.80564517, -0.9852966 ,  1.90139   ,
         -1.4851948 , -0.96316063, -0.35826832],
        [ 0.52038646,  0.04568981,  0.80709517, -0.9835636 ,  1.9032961 ,
         -1.4881977 , -0.9650438 , -0.35861585],
        [ 0.5194442 ,  0.04407027,  0.80520827, -0.98266006,  1.8993053 ,
         -1.4852973 , -0.9634341 , -0.35824707],
        [ 0.51898205,  0.04415236,  0.8051576 , -0.9825941 ,  1.897731  ,
         -1.4861436 , -0.9634547 , -0.3574056 ],
        [ 0.5183343 ,  0.04260498,  0.80339235, -0.9823522 ,  1.893536  ,
         -1.4827589 , -0.9612396 , -0.3563275 ],
        [ 0.5182621 ,  0.03967791,  0.8052563 , -0.9854379 ,  1.8969235 ,
         -1.4861064 , -0.9637438 , -0.3569042 ]], dtype=float32),
 [0,
  1,
  2,
  3,
  4,
  5,
  6,
  7,
  8,
  9,
  10,
  11,
  12,
  13,
  14,
  15,
  16,
  17,
  18,
  19,
  20,
  21,
  22,
  23,
  24,
  25,
  26,
  27,
  28,
  29,
  30,
  31,
  32,
  33,
  34,
  35,
  36,
  37,
  38,
  39,
  40,
  41,
  42,
  43,
  44,
  45,
  46,
  47,
  48,
  49,
  50,
  51,
  52,
  53,
  54,
  55,
  56,
  57,
  58,
  59,
  60,
  61,
  62,
  63,
  64,
  65,
  66,
  67,
  68,
  69,
  70,
  71,
  72,
  73,
  74,
  75,
  76,
  77,
  78,
  79,
  80,
  81,
  82,
  83,
  84,
  85,
  86,
  87,
  88,
  89,
  90,
  91,
  92,
  93,
  94,
  95,
  96,
  97,
  98,
  99,
  100,
  101,
  102,
  103,
  104,
  105,
  106,
  107,
  108,
  109,
  110,
  111,
  112,
  113,
  114,
  115,
  116,
  117,
  118,
  119,
  120,
  121,
  122,
  123,
  124,
  125,
  126,
  127,
  128,
  129,
  130,
  131,
  132,
  133,
  134,
  135,
  136,
  137,
  138,
  139,
  140,
  141,
  142,
  143,
  144,
  145,
  146,
  147,
  148,
  149,
  150,
  151,
  152,
  153,
  154,
  155,
  156,
  157,
  158,
  159,
  160,
  161,
  162,
  163,
  164,
  165,
  166,
  167,
  168,
  169,
  170,
  171,
  172,
  173,
  174,
  175,
  176,
  177,
  178,
  179,
  180,
  181,
  182,
  183,
  184,
  185,
  186,
  187,
  188,
  189,
  190,
  191],
 [192,
  193,
  194,
  195,
  196,
  197,
  198,
  199,
  200,
  201,
  202,
  203,
  204,
  205,
  206,
  207,
  208,
  209,
  210,
  211,
  212,
  213,
  214,
  215])
In [42]:
plot("deeptime2", Path("storage/experiments/Exchange/96Mplus2/repeat=0"))
receptive field [690 378 242]=[138  18   2]*[[ 1  1  1]
 [ 1  2  4]
 [ 1  4 16]
 [ 1  6 36]
 [ 1  8 64]]
  • try just one predictor
  • try logp? nah
  • mae?
  • make my own csv with 5m data (maybe 10k rows)
  • backtest?
In [ ]:
In [ ]:
In [ ]: